Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 667
user895993 Avatar asked Nov 17 '11 06:11

user895993


1 Answers

Generally fixed sleeps/waits are a bad thing.. They are a brute force approach that either results in brittle scripts, slow scripts, or often both. If you don't set them long enough then occasionally tests break, if you set them too long, then tests never break but they are SLOW because of all the fixed thumb twiddling time.

Most automation tools either take care of the waiting automagically, or they provide more graceful ways to synchronize your scripts with your app

A recent blog posting by JNicklas explanes some recent changes to Capybara in this regard, provides some examples of some ways to do specific wait-for-condition type of code for a few special cases, and in general recommends to learn more about the tool and how it deals with waiting for stuff, ajax actions, and synchronizing.

like image 113
Chuck van der Linden Avatar answered Sep 23 '22 17:09

Chuck van der Linden