1

I am new to Capybara testing and am having a few issues.

I have a scenario I am trying to run, and this is the step implementation:

When /^I select the signin link$/ do
  click_link 'Sign in'
end

I have tried to access this link with xpath, css, and tried the within implementation as well. Capybara cannot seem to find it, and returns a Capybara::ElementNotFound exception in all cases.

When I load the webpage without JavaScript, the link is not visible, and I'm wondering if this is why Capybara cannot find it. I found a trigger method, but am unsure how it works. Does anyone have a working example of trigger, or any other ideas for what I should do?

1 Answer 1

3

Are you using the selenium webdriver to run this test? It sounds like you are trying to run a scenario that requires javascript to see certain elements, without using a driver that supports javascript.

In your .feature file all you have to do is add this line before the scenario:

@javascript
Scenario:  My Scenario
  When blah blah blah
  ...

The @javascript tag tells capybara to use selenium-webdriver to run the test. It'll fire up firefox and go through the test - allowing all javascript functionality to work. This slows tests down considerably so only use it when absolutely necessary to test ajax-y and javascript-y behavior.

If that still doesn't work you can use this step:

Then show me the page
When I select the signin link

Which will open the page up for you in a new browser in that page's current state for your inspecting pleasure.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. In my head (for some reason) I assumed Capybara would magically know to open a browser. I have installed firefox, and the selenium-webdriver gem (jruby -S gem install selenium-webdriver) and have altered my env.rb file to include (require 'selenium-webdriver').
I think Capybara requires it itself. When installing cucumber I just used the --capybara command and everything for it "just worked," and I see no selenium-webdriver required but I definitely have tests that use it. Feel free to accept this answer as the correct one if it was satisfactory :)
@Rebecca you can also add this you your features/support/env.rb if all your tests depend on javascript, obviating the need for @javascript tags everywhere: Capybara.default_driver = :selenium

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.