Search code examples
ruby-on-railscucumbercapybara

How to add wait condition in capybara scenarios?


I am using capybara for testing my rails application for integration testing. In my application there are many Lightbox and Ajax and js calls.

 @javascript   
  Scenario: I agree functionatilty
   Given I go to the create account page
   When I click on button which is given as image "lnkTerms2"
   And I follow "i_agree"
   Then I go to the create account page

Here in above code lnkTerms2 is and id which will help in calling js function to open a lightbox. And i am getting an error as

   Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotDisplayedError)
      [remote server] resource://fxdriver/modules/atoms.js:9519:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:256:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:305:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:320:in `unknown'
      [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/[email protected]/components/nsCommandProcessor.js:197:in `unknown'
      (eval):2:in `send'
      (eval):2:in `click_link'
      ./features/step_definitions/web_steps.rb:300:in `/^I click on button which is given as image "([^"]*)"$/'
      features/Sign_up_process.feature:61:in `When I click on button which is given as image "lnkTerms2"'

The problem is as this function called in webdriver, it is not getting time to load javascript and ajax calls. And lightbox is not opening. So Please suggest me any solution.

Also if suppose i write the line

When I click on button which is given as image "lnkTerms2"

after 4 to 5 statements then it is working fine as it gets time to load js.


Solution

  • For a pause after step to wait ajax try:

    And I wait 5 seconds
    

    Your must add to web_steps.rb next code:

    When /^I wait (\d+) seconds?$/ do |seconds|
      sleep seconds.to_i
    end