Capybara and poltergeist test failing - ruby-on-rails

I been using :firefox drivers for capyabara and this test was passing but when I switch to poltergeist driver the test been failing now with the following error:
Minitest::UnexpectedError: Capybara::ElementNotFound: Unable to find field "email"
Here is the capybara and poltergeist setup:
def setup
# FactoryGirl.lint
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
Capybara.run_server = true
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :js_errors => false)
end
Capybara.default_driver = :poltergeist
Capybara.current_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = 'http://localhost:4200'
Capybara.server_port = 3000
Capybara.default_max_wait_time = 5
end
Here is the test:
test "User should be able to signin" do
visit '/'
wait_for_ajax
fill_in 'email', with: #user.email
fill_in 'password', with: #user.password
assert true
end
So, when I changed the driver to :selenium the test pass with no error.
How do I setup/fix poltergeist to pass this test?
I took screenshot and it shows loading indicator which is a div that is removed in afterModel Ember hook using the following code:
_ember['default'].$('.ember-load-indicator').remove();
For Ajax calls we have the following function as test helper to wait for ajax calls:
def wait_for_ajax
Timeout.timeout(Capybara.default_wait_time) do
loop until finished_all_ajax_requests?
end
end
def finished_all_ajax_requests?
page.evaluate_script('jQuery.active').zero?
end

I suspect it's a timing issue... When using poltergeist vs a browser, you can get these weird behaviours, where an element is present, but not really loaded hence the error... An easy way to confirm this is by putting a sleep prior to fill_in email... put sleep 10 just to be safe...
Additional tip:
To help debug headless tests, consider adding screenshots on failures -
Add this to the end of your Capybara setup:
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
"screenshot_#{example.description.gsub(' ', '-').gsub(/^.*\/spec\//,'')}"
end
screenshot_path = "#{PROJECT_ROOT}/screenshot/"
Capybara.save_and_open_page_path = screenshot_path

Related

RSpec/Capybara does not commit to DB within test

I'm quite desperate since moving our test suite from Minitest to RSpec. All the controller and model tests run fine so far, but since trying to port (formerly passing/working) feature tests like the following I ran into trouble...
feature 'Create place' do
scenario 'create valid place as user' do
login_as_user
visit '/places/new'
fill_in_valid_place_information
click_button('Create Place')
visit '/places'
expect(page).to have_content('Any place', count: 1)
end
...
def fill_in_valid_place_information
fill_in('place_name', with: 'Any place')
fill_in('place_street', with: 'Magdalenenstr.')
fill_in('place_house_number', with: '19')
fill_in('place_postal_code', with: '10963')
fill_in('place_city', with: 'Berlin')
fill_in('place_email', with: 'schnipp#schnapp.com')
fill_in('place_homepage', with: 'http://schnapp.com')
fill_in('place_phone', with: '03081763253')
end
end
Unfortunately this does not lead to a DB commit which makes the test fail. It does not fail if i pry into the test and manually create the requested place. I tried different methods in order to trigger the button but nothing worked so far.
This is how my rails_helper.rb looks like:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'capybara/poltergeist'
require 'pry'
def validate_captcha
fill_in 'captcha', with: SimpleCaptcha::SimpleCaptchaData.first.value
end
def login_as_user
user = create :user, email: 'user#example.com'
visit 'login/'
fill_in 'sessions_email', with: 'user#example.com'
fill_in 'sessions_password', with: 'secret'
click_on 'Login'
end
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, phantomjs_options: ['--ignore-ssl-errors=true'])
end
Capybara.javascript_driver = :poltergeist
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, no_transaction: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include Capybara::DSL
config.include Rails.application.routes.url_helpers
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
end
Does anyone have a clue about a possible cause? Gem versions:
capybara 2.12.0
rspec 3.5.0
rails 4.2.7.1
best and thanks,
Andi
--- Update
I added fill_in_valid_place_information method
This is how the test fails with or without a Capybara JS driver enabled (shouldn't matter in case of this test as the feature does not use any JS). Unfortunately it doesn't give any real hints to work with...
1) Create place create valid place as user
Failure/Error: expect(page).to have_content('Any place', count: 1)
expected to find text "Any place" 1 time but found 0 times in "KIEZ KARTE Find places Here comes a list of all POIs currently available in our database. If you are looking for a specific location please enter parts of its descriptive features into the 'Search' field. Search: Name Postal code Categories No data available in table"
Timeout reached while running a *waiting* Capybara finder...perhaps you wanted to return immediately? Use a non-waiting Capybara finder. More info: http://blog.codeship.com/faster-rails-tests?utm_source=gem_exception
--- Update 2
I found the issue which is not capyara-related. Actually I forgot to transfer a stub response for an API we're calling. Thanks everybody for participating in my struggle!
There are a number of potential issues in your test that could be causing what you are seeing, it would be easier to narrow down in the future if you included the actual error message(s) your test produces.
Your scenarion/feature isn't tagged with :js metadata to activate using the Capybara driver. It's possible you've specified Capybara.default_driver somewhere, but if so then your DatabaseCleaner config is wrong
Use the recommended DatabaseCleaner configuration from https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example . The driver name detection will work if you have specified Capybara.default_driver as mentioned in #1 and also with the :js/:driver metadata usage pattern. Additionally, the append_after/after difference is important to reduce test flakiness
Your login_as_user method needs to verify the login has completed before returning. This is because click_on 'Login' can trigger asynchronously and return before the login actually occurs. This leads to the visit you call immediately following aborting the login, preventing the session cookie from being sent, and ending up with a non logged in user when you expected the user to be logged in. To fix this you need something like
def login_as_user
...
click_on 'Login'
expect(page).to have_text('You are now logged in!') #whatever message is shown on successful login, or use have_css with some element on the page that only exists when a user is logged in (user menu, etc)
end
The same issue exists between click_button('Create Place') and visit '/places' where the visit can effectively cancel the effects of the button click

Capybara error with Poltergeist and RSpec

I am trying to write a simple spec with Capybara using the Poltergeist driver in RSpec. There doesn't seem to be a problem when tests should fail , however when I am expecting a passing test I get the following error:
~/.rbenv/versions/2.2.3/lib/
ruby/gems/2.2.0/gems/poltergeist-1.6.0/lib/capybara/poltergeist/errors.rb:17:in
`initialize': wrong number of arguments (0 for 2) (ArgumentError)
I navigated to where the the line of code the error was indicating:
class JSErrorItem
attr_reader :message, :stack
def initialize(message, stack)
#message = message
#stack = stack
end
def to_s
[message, stack].join("\n")
end
end
But I was unable to find anywhere that I should be interacting with this constructor.
This is the spec that I am writing
describe 'Sign Up', type: :feature do
it 'should allow user creation through the signup form' do
visit new_user_url host: 'http://localhost:3000'
within('.form') do
fill_in('user[username]', with: 'Example')
fill_in('user[password]', with: 'Password')
fill_in('user[password_confirmation]', with: 'Password')
find(".submit-button").click
puts page.body
expect(page).to have_content('Welcome')
User.last.destroy!
end
end
end
The puts page prints the content of the page as expected but after the error occurs and the remainder of the lines in the spec are not run. Oddly enough the error only occurs when I am expecting the spec to pass. When I am expecting a failing test the entire spec runs without error.
My spec helper was set up as below:
RSpec.configure do |config|
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'capybara/dsl'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, time_out: 120, phantomjs_options: ['--ignore-ssl-errors=yes'], js_errors: false)
end
Capybara.configure do |c|
c.javascript_driver = :poltergeist
c.default_driver = :poltergeist
c.app_host = 'http://localhost:3000'
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
I upgraded Poltergeist to 1.9 and PhantomJS to 2.1 as suggested in the comments and it fixed the issue.

Capybara rspec test takes a long time to execute - why?

I have a rails project using rspec 3.4.0 capybara 2.6.2 and capybara-webkit 1.8.0.
I have a simple feature test which is as follows:
require 'rails_helper'
RSpec.feature "Seller Features", type: :feature do
let!(:sub_category) { FactoryGirl.create(:sub_category) }
#all tests will create a user - sign them in and land them on the homepage
background do
sign_in_as
end
scenario "Buyer creates a seller profile", :js => true do
click_link("SELL ON SITE",match: :first)
expect(page).to have_text("Reach thousands of customers in your area")
click_link("Create an Activity",match: :first)
expect(current_path).to eql (new_seller_profile_path)
fill_in "seller_profile[business_name]", :with => "Test company"
fill_in "seller_profile[business_email]", :with => "test#email.com"
fill_in "seller_profile[business_phone_number]", :with => "07771330510"
fill_in "seller_profile[business_description]", :with => "This is a test company"
find('label[for="social"]').click
find("#facebook-placeholder").click
fill_in "seller_profile[business_facebook_url]", :with => "https://www.facebook.com/test"
click_button("CREATE AN ACTIVITY")
fill_in "seller_profile[requested_postcode]", :with => "EH21 8PB"
click_button("Submit")
click_link("Continue")
expect(page).to have_text("Choose the type of activity that you want to create")
end
end
The test passes successfully. The problem is it takes this length of time to run:
Finished in 4 minutes 26.2 seconds (files took 7.19 seconds to load)
This seems ridiculously long! During the execution my cpu is almost idle so I am not sure what is causing the length of execution time? Is this a reasonable normal amount of time for such a simple feature test?! Please help!
I don't know if this will help but this is my spec_helper.rb file:
ENV["RAILS_ENV"] ||= "test"
ENV['SERVER_NAME'] = "user.myapp.com"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"
Capybara::Webkit.configure do |config|
# Enable debug mode. Prints a log of everything the driver is doing.
config.debug = false
config.allow_unknown_urls
# Allow pages to make requests to any URL without issuing a warning.
# Allow a specifc domain without issuing a warning.
config.allow_url("https://checkout.stripe.com")
config.allow_url("https://checkout.stripe.com/v3/data/languages/en.json")
# Timeout if requests take longer than 5 seconds
config.timeout = 60
# Don't raise errors when SSL certificates can't be validated
config.ignore_ssl_errors
end
Capybara.javascript_driver = :webkit
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy= example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include SignInHelpers, type: :feature
config.mock_with :rspec
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
end
It was waiting for 3rd party javascripts - debug mode turned on was the key to finding out what was hanging up capybara webkit. Thanks Tom Walpole.

Log in test fails with RSpec / Capybara / Poltergeist

I'm writing test code for Rails application with React.
I'm using RSpec, Capybara, Poltergeist(PhantomJS 2.0) and Devise for authentication.
Because this is a javascript test, I'm also using database_cleaner gem as suggested here.
When test runs I can see user data goes into database(mysql) and Devise method user.valid_password?("password") returns true, however, test fails.
spec_helper.rb
RSpec.configure do |config|
config.use_transactional_fixtures = false
Capybara.register_driver :poltergeist do |app|
options = {
inspector: true,
js_errors: false,
#debug: true,
phantomjs_options: %w[
--web-security=no
--ignore-ssl-errors=yes
--ssl-protocol=any
]
}
Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.javascript_driver = :poltergeist
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
The spec looks like this.
require 'spec_helper'
feature 'Registration', js: true do
let!(:email) { "email#test.com" }
scenario "user can sign up and log in" do
visit 'sign_up'
find('input').set(email)
find('.submit').click
expect(page).to have_content('welcome!')
visit '/sign_out'
visit '/sign_in'
expect(page).to have_content('Login')
find('input[name="email"]').set(email)
find('input[name="password"]').set('password')
find('.submit').click
wait_for_ajax
expect(page).to have_content('Test User')
end
end
API looks like this.
post do
email = params[:email]
password = params[:password]
if email.nil? or password.nil?
return error!(['this is error'], 401)
end
user = User.where(email: email.downcase).first
if user.nil?
return error!(['this is error'], 401)
end
if !user.valid_password?(password)
return error!(['this is error'], 401)
else
user.ensure_authentication_token
user.save
return { status: 'ok', results: { access_token: user.authentication_token } }
end
end
I get log like below with PhantomJS 2.0.
API.ajax:POST /sessions
API.ajax:POST /sessions
API.ajax:POST /sessions :failed
httpStatus:401
error
[object Object]
API.ajax:POST /sessions :failed
httpStatus:401
error
[object Object]
So something went wrong in authentication process but it only happens when I run test. (I can sign up and log in with Chrome or Firefox.)
Appreciate any idea.
Thanks!
It turned out some functionality refers HARD CODED port 3000 when I run test..
rspec - Running rails server changes test result? - Stack Overflow
I don't know why it's built like that but at least for now I can run my test by adding Capybara setting:
Capybara.server_port = 3000
Thanks!

Rails/RSpec/Capybara - transactionless database cleaning works with Selenium but not Webkit

So I've setup my RSpec environment to use a truncation cleaning strategy for my RSpec Capybara tests but I still find that something is still wrapping my test in a transaction when I use Webkit as my Javascript driver.
I don't have this problem with Selenium, which has got me stumped.
Here's the relevant RSpec config with webkit:
Capybara.javascript_driver = :webkit
Capybara.register_driver :webkit do |app|
Capybara::Webkit::Driver.new(app).tap do |driver|
driver.allow_url "fonts.googleapis.com"
driver.allow_url "dl.dropboxusercontent.com"
end
end
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.clean_with :transaction
end
config.after(:each) do
ActionMailer::Base.deliveries.clear
end
config.around(:each, type: :feature, js: true) do |ex|
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.start
self.use_transactional_fixtures = false
ex.run
self.use_transactional_fixtures = true
DatabaseCleaner.clean
end
And my feature test looks like this:
feature "profile", js: true do
describe "a confirmed user with a valid profile" do
before(:each) do
#user = FactoryGirl.create :user
signin(#user.email, #user.password)
end
scenario 'can edit name' do
visit edit_user_profile_path
fill_in :user_name, with: 'New name'
click_button :Submit
#user.reload
expect(#user.name).to eq('New name')
expect(current_path).to eq show_user_path
end
end
end
If I run this test with Webkit it fails, but with Selenium it passes.
I've experimented with some debugging. If I put a debugger statement in the #update action I see that it updates the database correctly. If I connect to the test database at that time I can see the new information in the database, which means that this update cannot be wrapped in a transaction. However, but in the debugger in the .spec #user still see the original name as generated by FFaker in factory_girl. This leads me to believe that the test is ran inside a transaction.
When I change my JavaScript driver to Selenium it all works fine.
Any ideas?
Wow. I found the problem almost immediately after posting the question. No transactions were involved.
It was a race issue between the backend and webkit/selenium front end. With Webkit the test was executing the #user.reload and the expect statements before the controller had a chance to update the database. With Selenium it was the other way around.
The trick is to make Capybara wait for the page reload. I changed my test to this:
scenario 'can edit name' do
visit edit_user_profile_path
fill_in :user_name, with: 'New name'
click_button :Submit
expect(current_path).to eq show_user_path
#user.reload
expect(#user.name).to eq('New name')
end

Resources