Question

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?

Was it helpful?

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...

OTHER TIPS

Try adding require 'capybara' prior to require 'sauce' in your "Cumcumber Version"

From sauce GitHub

## In your test or spec helper
require "capybara"
require "sauce/capybara"

Reasoning within calls find. Sauce checks to see if a find method is defined an if so then it creates it's own alias called base_find if not it uses it's own find method which is not the same as capybara's. I think they may have something to do with your issue since it is using sauce's find method.

Not positive this is the answer but you can always give it a shot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top