How to add a wait condition in capybara script?

I use capybara to test my rails application to test integration. There are many Lightbox and Ajax and js calls in my application.

@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 the lnkTerms2 code above, there is an id that will help open the lightbox when calling the js function. And I get an error like

  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/ fxdriver@googlecode.com /components/nsCommandProcessor.js:256:in `unknown' [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/ fxdriver@googlecode.com /components/nsCommandProcessor.js:305:in `unknown' [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/ fxdriver@googlecode.com /components/nsCommandProcessor.js:320:in `unknown' [remote server] file:///tmp/webdriver-profile20111117-6876-18cfcfp/extensions/ fxdriver@googlecode.com /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 that this function is called in webdriver, it does not manage to load javascript and ajax calls. And the lightbox does not open. So please offer me some solution.

Also if I write a line

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

after 4-5 instructions, then it works fine as it gets time to load js.

+6
source share
3 answers

To pause after the step to wait for an ajax attempt:

 And I wait 5 seconds 

Your code should add the following code to web_steps.rb:

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

Usually, fixed dreams / expectations are a bad thing. This is a brute force approach that either leads to fragile scripts, slow scripts, or often both. If you don’t install them long enough, and sometimes experience interruptions, if you install them too long, the tests are never interrupted, but they are SLOW due to all the fixed times of flexion of the thumb.

Most automation tools either automatically take care of the wait, or provide more graceful ways to synchronize your scripts with your application.

A recent JNicklas blog post explains some recent changes to Capybara in this regard, provides some examples of some ways to make a specific type of wait code for state for some special cases, and generally recommends learning more about this tool and how it relates to waiting things, ajax actions and synchronization.

+12
source

Waiting for a fixed number of seconds in the hope that your script will be fast enough is not a good strategy because it can lead to random test errors. I recommend that you wait until some condition is met:

 And I wait until '#meow' is visible #... When /^I wait until '([^']+)' is visible$/ do |selector| wait_until do # you can also specify timeout here find(selector).visible? end end 
+1
source

Source: https://habr.com/ru/post/901687/


All Articles