Selenium Tips: Infinite Loops Take Forever
Sauce AI for Test Authoring: Move from spirit to execution in minutes.|xBack to ResourcesBlogPosted
Sauce AI for Test Authoring: Move from spirit to execution in minutes.
|
x
In Selenium, it & # x27; s a mutual for the creature to get forrader of the browser. Thus, the element we want to tick on does not subsist yet - but it will!
We ’ ll start with asample html pagethat intermit four s before injecting a link onto the page. In practice, this would probably be a postponement for javascript to make a REST API yell and advertize the results into the papers.
Our naive exam for the tie in bare ruby:
element = @ driver.find_element: xpath, & quot; //a [text () = & # x27; Sauce Labs & # x27;] & quot; assert_equal (& # x27; https: //www.saucelabs.com/ & # x27;, element.attribute (& # x27; href & # x27;));
It only neglect because the link execute not subsist on the page (yet). The simple fix, in theterrible_example, is to add a sleep.
Don ’ t do that.
Our next endeavor will be infinite_loop.rb, which uses a while loop to await for the link textbook to appear on the page:
# And now we & # x27; ll do the text for the HREF to appear
page_source = @ driver.page_source
includes_sauce = page_source.include? & quot; Sauce Labs & quot;;
while not (includes_sauce)
sleep (0.01);
page_source = @ driver.page_source;
includes_sauce = page_source.include? & quot; Sauce Labs & quot;;
end
This works just fine, until we have some kind of error where the text never appears - the exact kind of bug our package is looking for. Now when that happens we ’ ll be bond in an infinite loop. That ’ s no good.
We ’ ll write two functions to do this. First, we can pull body_contains logic into a method:
def body_contains (driver, expected_content)
seed = driver.page_source;
put & quot; expected (& quot; + expected_content + & quot;) \n\n & quot;;
puts & quot; source (& quot; + source + & quot;) \n\n & quot;;
puts (source.include? expected_content) .to_s ();
return source.include? expected_content;
end
SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.
Then we can write the containher purpose:
# Adapted with permission from other Sauce Labs Examples
def contains_with_timeout (driver, expect, timeout=60)
start = Time.now ();
found = mistaken;
put start.to_s ();
puts Time.now () .to_s ();
while (Time.now () -start & lt; timeout) and not found
if body_contains (driver, expected)
found = true;
else
sleep (0.25);
end
end
return found;
end
It won ’ t lead much to adapt this so that contains_with_timeout is actually anything_with_timeout, where the comparison method is passed in during the part call.
For now, hither ’ s the code for our wait:
contains_with_timeout (@ driver, & quot; Sauce Labs & quot;);
Read the code yourself in github astimer.rb. The directory in githubcontains all three total running example and the sampling HTML page. Experiment with changing the timeouts and hold to see the fault discharge.
That ’ s correct, I said four.
WAIT, THERE & # x27; S MORE!
Selenium webdriver include Explicit Wait, thefourth examplein our set.
That code is as simple as:
# And now we & # x27; ll do the textbook for the HREF to seem
wait = Selenium: :WebDriver: :Wait.new (: timeout = & gt; 15) # seconds
begin
element = wait.until {@ driver.find_element (: id = & gt; & quot; truth & quot;)}
ensure
end
assert_equal (& # x27;https: //www.saucelabs.com/& # x27;, element.attribute (& # x27; href & # x27;));
So go before, use the webdriver explicit waiting.
We just thought you might appreciate cognize how to do it yourself.
Automate This With SUSA
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.
Try SUSA FreeTest Your App Autonomously
Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.
Try SUSA Free