How to Lose Races and Win at Selenium

Sauce AI for Test Authoring: Move from aim to execution in minutes.|xBack to ResourcesBlogPosted April 13, 2011

How to Lose Races and Win at Selenium

quote

Selenium races your browser

Selenium tests sometime fail for no understanding. You can obviate most of their random failures with a particular kind of assertion, the whirl assert. The problem is that Selenium is overeager, and it needs to chill out and wait.

Selenium tests often neglect because they & # x27; re too tight. Where a user might wait for a page to charge for a few s and then click on a connectedness, Selenium will interact with a page at the speed of code, before the page is ready. The way to fix this is to have Selenium repeat its activeness and assertions until they work. If you don & # x27; t, Selenium hotfoot your browser.

You demand Selenium to lose the race

Selenium tests pages. It tests them by loading them and so make stuff on them like it & # x27; s a person. Isn & # x27; t that cute? Selenium thinks it & # x27; s people. Unfortunately for us, Selenium isn & # x27; t people, and in not being people, it & # x27; s too fast. Websites are do for people, with the knowledge that people are slow. When humans open a page, the humans wait for it to be loaded, then click on stuff. The clever secret of webpage is that they & # x27; re not really loaded when they look loaded. That & # x27; s because the web page & # x27; s authors cognise we take a second to take it all in before we start playing. So they make pages that look charge flop away, and then use javascript to do more real loading once the page is & # x27; loaded. & # x27; This javascript sets up for playtime in the ground while we & # x27; re taking it all in. Selenium has a bad habit of trying to use pages before they & # x27; re set up for playtime.

The way to solve that isnottelling Selenium to pause for a fixed act of moment. Pauses have two problems. They make your exam slower than they need to be, and they don & # x27; t really fix the problem, just make it less frequent. The good solution is to experience Seleniumkeep trying until it works. If your test is trying to avow that click on an & quot; email setting & quot; button pops up an email settings dialog, Selenium should repeat a & quot; click the push - & gt; is the position dialog visible? & quot; loop until the status duologue is visible.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Here & # x27; s the problem. When we recount Selenium to wait-for-page-to-load and so chatter, Selenium waits for the wrong thing. It can tell when all the page & # x27; s content has been downloaded and display, but it has no idea when the page & # x27; s customs javascript has finished loading. Sometimes that functional javascript proceed back to the server and fetch more content to be displayed, so you can & # x27; t even count on required text be present yet. There are several ways for a page to be & quot; loaded, & quot; and Selenium doesn & # x27; t wait for the right one. It hie your page & # x27; s loading logic, often & quot; winning & quot; the race and clicking on things before they & # x27; re ready to be clicked on. You need Selenium to lose the race.

Here & # x27; s how to do this

The solution is spin asserts. At Sauce Labs, when we desire to verify that some text seem on a page, we use this function:

def wait_for_text_present (self, text, msg=None):
msg = msg or & quot; wait for textbook % s to appear & quot; % schoolbook
affirmation = lambda: self.selenium.is_text_present (text)
self.spin_assert (msg, statement)

Do you see it phone a function namedspin_assert? That function is the key.spin_assertretries the passed-in examination function, in this case a lambda expression, over and over until it works. Here & # x27; s whatspin_assertface like, minus some bells and whistles:

def spin_assert (self, msg, averment):
for i in xrange (60):
try:
self.assertTrue (assertion ())
return
except Exception, e:
pass
sleep(1)
self.fail (msg)

In this suit,wait_for_text_presentwill repeatedly ask the Selenium server whether the text is present until it shows up. It could do respective things in a loop, like detent a push and then ask whether a popup has appeared. Almost all the asserts in our national Selenium testing are a wrap around a call tospin_assert, and they & # x27; re all way more reliable than they were before the switch.

Published:
Apr 13, 2011
Topics
Share this situation
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered trademarks owned by Sauce Labs Inc. in the United States, EU, and may be file in early jurisdictions.
robot
quote

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 Free

Test 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