capybara +rspec: Post "/api/albums" not working - ruby

So I am trying to create an item using POST method. But it doesn't seem to post the item.
When I do get /api/albums seems to respond fine and my test passes.
/album_spec.rb:
describe AlbumController do
before :each do
#user =FactoryGirl.create(:person)
#album = FactoryGirl.create(:album)
end
it "creates first story on login", :js => true do
login(#user)
post "/api/albums/", :format => :api_v1,:album =>#album
response.should be_success
end
it "gets album", :js=> true do
get "api/albums/"
response.should be_success
end
end
spec_helper.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require "capybara-screenshot"
require "factory_girl_rails"
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
#Capybara.defualt_wait_time = 10
if ENV['BROWSER_TEST']
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
else
Capybara.javascript_driver = :selenium
end
RSpec.configure do |config|
config.include Capybara::DSL
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
config.include RequestHelpers, :type => :request
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.infer_base_class_for_anonymous_controllers = false
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end

I found the solution for myself. In the album factory, I had to add more attributes to make sure that the #user and album.user match.
#album = FactoryGirl.create(:album,person_id=>#user.id)
And then my test case looked like this
it "creates first story on login"do
login_as #user
post "/api/albums/", :format =>":api_v1",:album =>#album.attributes
response.should be_success
a = Album.last
a.person_id.should == #user.id #verifying user_ids match end
end

Related

Getting Frozen Error-Can't modify frozen string

Getting frozen error while running below mentioned cod. Also find the helpers files. Below is the main file:
require 'spec_helper.rb'
require 'rspec_capybara_assignment_helper.rb'
describe 'Open the automationpractices site' , :type => :request do
it 'Test: Page Content', :page do
visit "http://automationpractice.com/index.php"
expect(page).to have_xpath(".//img[contains(#src,'automationpractice.com/img/logo')]")
expect(page).to have_xpath(".//input[#id='search_query_top']")
expect(page).to have_link("Contact us")
expect(page).to have_link("Sign in")
within('div#block_top_menu') do
expect(page).to have_link("Women")
expect(page).to have_link("Dresses")
expect(page).to have_link("T-shirts")
end
within('div#center_column') do
expect(page).to have_link("Popular")
expect(page).to have_link("Best Sellers")
end
expect(page).to have_content("Automation Practice Website")
end
end
Spec_Helper file:-spec_helper.rb
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require "selenium-webdriver"
require "capybara"
require 'capybara/rspec/matchers'
RSpec.configure do |config|
config.include Capybara::DSL , :type => :request # to include capybara DSL methods
config.include Capybara::RSpecMatchers,:type => :request # to include Rspec matchers available.
end
Capybara.configure do |config|
config.run_server = false
config.default_driver = :selenium # which driver to use (selenium / chrome /internet explorer)
config.default_selector = :css #(by default css selector) # if user want to use xpath, he has to write find(:xpath, <xpath>).click
config.app_host = "http://automationpractice.com/index.php" # host app
config.default_max_wait_time = 5 # wait for 4 seconds.
config.app_host = #url
config.ignore_hidden_elements = true # will ignore hidden elements on page.
config.match =:prefer_exact # check for exact match.
end
Rspec Capybara Assignmnet Helper File:
rspec_capybara_assignment_helper.rb
def click_on(value)
find(value).click
end
def link_click(value)
click_link(value)
end
def button_click(value)
click_button(value)
end
def login_application
link_click('Sign in')
fill_in('email', :with => 'test.test#testing.com')
fill_in('passwd', :with => 'testtest')
button_click('SubmitLogin')
end
def sign_out
link_click('Sign out')
end
def search_product(value)
fill_in('search_query_top', :with => value)
button_click('Search')
sleep(2)
end
def add_product(value)
page.find(:css,'.product-image-container').hover
sleep(2)
first("a", :text => "More").click
sleep(4)
fill_in('quantity_wanted', :with => '2', visible: false)
select('M', :from => 'group_1')
find('button.exclusive').click
sleep(2)
first("span", :text => "Continue shopping", wait: 3).click
sleep(2)
end
def check_locator(value)
expect(page).to have_selector(value)
end
Error as below:
Open the automationpractices site
Test: Page Content (FAILED - 1)
Failures:
Open the automationpractices site Test: Page Content
Failure/Error: visit "http://automationpractice.com/index.php"
FrozenError:
can't modify frozen String
./spec/tests/rspec_capybara_assignment.rb:6:in `block (2 levels) in <top (required)>'
Finished in 0.7789 seconds (files took 3.61 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/tests/rspec_capybara_assignment.rb:5 # Open the automationpractices site Test: Page Content
This appears to be related to selenium-webdriver, not capybara itself.
There's an issue about this at https://github.com/SeleniumHQ/selenium/issues/7365
To fix, specify in your gemfile that you want 3.142.4 as a minimum: gem 'selenium-webdriver', '~> 3.142.4'
And then run bundle update selenium-webdriver
That should get this specific issue resolved.

“including Capybara::DSL in the global scope is not recommended!” want to remove it. console warning

To resolve the error visit is not found. I have included include Capybara::DSL in one of my helper module like this:
I am using ruby 2.7.0
include Capybara::DSL
module LoginHelper
def self.login_user
visit 'https://staging.have2have.it/login'
within(".container-fluid") do
fill_in("email", with: 'shinsaurab#gmail.com', :match => :prefer_exact)
fill_in("password", with: '123', :match => :prefer_exact)
end
click_button('Log In')
end
end
spec_helper.rb
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require './spec/helpers/login_helper'
Capybara.default_driver = :selenium
RSpec.configure do |config|
config.include Capybara::DSL
config.include LoginHelper
end
Can anyone please suggest if I am doing something wrong. I have tried some suggestion but didn't work for me
I was having a similar issue tried many things but what worked for me is to remove config.include Capybara::DSL from spec_helper and include the LoginHelper in a Helpers module. In your case, they may look like this:
login_helper.rb
module Helpers
module LoginHelper
def login_user
visit 'https://staging.have2have.it/login'
within(".container-fluid") do
fill_in("email", with: 'shinsaurab#gmail.com', :match => :prefer_exact)
fill_in("password", with: '123', :match => :prefer_exact)
end
click_button('Log In')
end
end
end
And spec_helper will look like this:
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require './spec/helpers/login_helper.rb'
Capybara.default_driver = :selenium
RSpec.configure do |config|
config.include Helpers::LoginHelper
end
Thanks!
Please let me know if you have any doubts

How do I reference the Capybara driver for creating my pageobjects/specs.

I am attempting to use pageobjects along with with my Capybara specs but can't seem to properly reference the driver. Basically I want to be able to use the PageObjects to define the fields on the page (this is login_page.rb), but when I try to create the object in the spec, it is throwing errors with saying that the object is nil.
spec_helper.rb:
# frozen-string-literal: true
require 'rspec'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'page-object'
# loading page object files
page_paths = File.join(Dir.pwd, 'spec', 'pages', '**', '*.rb')
puts 'foo'
Dir.glob(page_paths).each { |file| puts file}
Dir.glob(page_paths).each { |file| require file }
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.default_driver = :firefox
Capybara.app_host = *********** #redacted
Capybara.default_max_wait_time = 5
RSpec.configure do |config|
config.before(:all) do
#browser = Capybara::Selenium::Driver
end
config.before(:each) do
config.include Capybara::DSL
end
end
login_page.rb
class LoginPage
include Capybara::DSL
include PageObject
text_field(:username, id: 'username')
text_field(:password, id: 'password')
button(:login, id: 'loginButton')
def initialize(driver)
#driver = driver
end
end
login_spec.rb
require 'spec_helper'
describe 'On Login page' do
context 'using proper password' do
before(:each) do
visit('/')
end
it 'logs in as foo' do
login_page = LoginPage.new(#browser)
login_page.username = 'foo'
login_page.password = 'bar'
login_page.login
end
end
end
Assuming you're talking about the page-object gem - https://github.com/cheezy/page-object - it doesn't support Capybara, it supports watir-webdriver/watir and selenium-webdriver. Additionally Capybara::Selenium::Driver is a class not an object instance. As shown in the page-object readme you need to pass an object instance into your page objects constructor
#browser = Selenium::WebDriver.for :firefox
If you want a page object framework that supports Capybara you may want to look at something like site-prism instead.

Javascript trigger when using poltergiest/capybara outside of rails

I'm using Capybara, rspec and poltergeist, outside of rails, to run some headless integrations tests. The scenario is that, there are 2 select fields. If I select a value in the first select field, the 2nd select field is populated based on the value of the first select field. If I run my spec using poltergeist in Mac OSX, the spec works. But in ubuntu, it fails, it seems that the 2nd select field is not populated. I also have js: true on my specs.
Here's my spec_helper.rb:
require 'capybara/poltergeist'
require 'capybara'
require 'capybara/rspec'
require 'pry'
require 'support/session_helper'
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::Poltergeist
config.include SessionHelper
Capybara.run_server = false
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = "http://vps-staging.dropmysite.com"
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
EDIT Adding failing spec
require 'spec_helper'
feature 'vps-staging', js: true do
background do
visit '/'
end
let(:timestamp) { Time.now.strftime('%Y-%m-%d_%H-%M-%S') }
feature 'create private server' do
background do
sign_in 'blahblah#blahblah.com'
end
it 'successfully creates server' do
find(:xpath, "//a[#href='/en/private_servers/new']").click
directory_field = all(".tagit-new")[0].find("input")
select 'Ubuntu', from: 'distribution'
select '15.04', from: 'version'
fill_in 'private_server_profile_name', with: "Auto Test #{timestamp}"
directory_field.set "/home"
click_button 'Save'
visit '/en/dashboard'
expect(page.text).to have_content "Auto Test #{timestamp}"
end
end
end
EDIT another thing I found out is that, this bug only happens on phantomjs version 1.9.8 and below. 2.0 works fine.
As Tom Walpole pointed out, updating poltergeist from 1.6.0 to 1.7.0 fixed most of my problem but I still need to tweak a bit to fully solve my problem. What I did is to create a js file to hold the polyfill and edit my spec_helper.rb and tell poltergeist to use the options, extensions and include the polyfill.js thus changing this code from
options = { js_errors: false }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end
to
options = { js_errors: false, extensions: ["spec/support/polyfill.js"] }
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, options)
end

Setting Sessions Using Rspec/ Rack::Test in Sinatra Tests

I am trying to test a Sinatra application that is using oauth that has the following code being run before every route for the callback:
before do
unless session.has_key?(:oauth_token) || request.path == '/auth/callback'
access_url = oauth_client.auth_code.authorize_url(redirect_uri: ENV['CALLBACK'])
puts "Redirecting to #{access_url}"
redirect "#{access_url}"
end
end
For my tests, I simply just want to set the session[:oauth_token] to anything so that I get past this block and move onto the test. However, after hours of searches and experimentation, I haven't been able to figure it out.
I've tried Rack::Test to try and set it this way:
describe "Visit home page", js: true do
before { get '/', {}, { 'rack.session' => { oauth_token: 'blahblahblah' } } }
it "has a list of products" do
get "/"
expect(page).to have_link("Clear & Mild Foam Handwash Refill, Fragrance-Free, 1250mL Refill, 3/Carton")
expect(page).to have_link("Coffee Portion Packs, 1.5oz Packs, Hazelnut Crème, 24/Carton")
end
end
end
and my spec_helper.rb looks like this:
require File.expand_path '../../server.rb', __FILE__
require 'rspec'
require 'capybara/rspec'
require 'rack/test'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
set :environment, :test
Capybara.app = Sinatra::Application
ENV['RACK_ENV'] = 'test'
module RSpecMixin
include Rack::Test::Methods
def app() Sinatra::Application end
def setup_session(session = {})
Rack::Session::Abstract::SessionHash.stub(:new).and_return(session)
end
end
RSpec.configure do |c|
c.include RSpecMixin
end
What is the best way to go about actually setting a session before every route?
Try this:
env "rack.session", { oauth_token: 'blahblahblah' }
get '/'
Taken from here.

Resources