Search code examples
ruby-on-railsrubycucumbercapybarasaucelabs

Unable to locate element with cucumber step_definition, but able to locate it with capybara stand-alone


I am testing a website that has both a vertical login form on the right, and a horizontal form on the bottom. Both have identically named "email" and "password" fields. So I am employing capybara's within to scope for the fields I'm interested in. The interesting bits of the web form look like this:

enter image description here

I have two separate projects, in which I am experimenting with sauce labs for automation. The first project is the capybara-only example, modified to test the page shown above. The second project is a cucumber implementation of the exact same tests. These are very simple, one-time hard-coded tests, just to get a proof of concept of the two techniques.

Here is the interesting bit of the capybara-only example:

within(:css, ".right-container.login-form") do
    fill_in 'email', :with => "greg.gauthier+#{generate_email_suffix}@website.com"
    fill_in 'password', :with => 'l33tP@$$w0rd'
    click_button 'Submit'
end

Here is the interesting bit of the cucumber step_definition:

When(/^The user enters his information$/) do
    within(:css, ".right-container.login-form") do #the page has duplicate forms
        fill_in 'email', :with => "greg.gauthier+#{generate_email_suffix}@website.com"
        fill_in 'password', :with => 'l33tP@$$w0rd'
        click_button 'Submit'
    end
end

When I run the capybara-only version, everything works great. The form gets filled in, and the email confirmation gets sent.

However, when I run the cucumber version, I get this error:

Unable to find css ".right-container.login-form" (Capybara::ElementNotFound)

How can this be? It's the exact same page, the exact same capybara method (within, using the :css selector), and the exact same test code. What am I not getting (aside from the fact that I'm probably cuking it wrong)?

Oh, here's what the require list looks like in the sauce_helper:

Capybara-only version:

require "sauce"
require "sauce/capybara"
require 'capybara/rails'
require 'capybara/rspec'

Cucumber version:

require "sauce"
require "sauce/capybara"
require "sauce/cucumber"

Do I maybe need to include the extra capybara gems in the cucumber version?


Solution

  • Ok, I'm embarrassed to admit this, but the reason for this question was ignorance of webdriver behavior, at least as it works with Capybara/Cucumber.

    The test in question is the second scenario in a set of scenarios. Apparently, selenium resets the browser to blank, between each scenario. So, test one works perfectly, but test two, three, and so forth fail on ElementNotFound because, of course... the page in question was not even loaded.

    I added a Before hook to a hooks.rb file in my support path, and now it's working.

    Apologies for cluttering the question stream...