It is more challenging to implement automated tests for websites using dynamic AJAX requests which load data or elements on the fly. With Watir test automation framework there are several different approaches. We can insert some fixed sleeps before the checkpoints. However this is a bad solution in many cases because quite often we don't know the exact time it takes, also fixed sleeps can make our tests very slow, especially when we have a large test automation suite.
Usage examples
This is my solution for Watir test automation framework. It retries any operation with the element until success. If it fails for the first time, it retries again in 3 seconds, repeating 5 times until success. We can configure the maximum number of retries and the interval between each retry.
Implementation of the method in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ajax_handler(&block) | |
tries = 5 | |
begin | |
return yield | |
rescue | |
sleep(3) | |
@browser.wait | |
tries -= 1 | |
if tries > 0 | |
retry | |
end | |
end | |
yield | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ajax_handler { @browser.text_field(:xpath, "//input[@name='userID']").set("superuser") } | |
ajax_handler { @browser.text.include?("sometext").should == true } |