Selenium Tips – Wait with WaitForCondition
Hello Readers, Welcome Back !!
Consider the following example where there are two drop-downs, one for the states and the other for counties.
The county drop-down is populated based on the state selection.
(Example courtesy of bitrepository.com)
selenium.open(“http://www.bitrepository.com/apps/viewDemo/?originalPost=http://www.bitrepository.com/dynamic-dependant-dropdown-list-us-states-counties.html&demoPage=http://www.bitrepository.com/demo/dynamic-dependent-dropdown-list/”); selenium.selectFrame(“mainFrame”); selenium.select(“state”, “label=California”); selenium.waitForCondition(“var value = selenium.browserbot.findElementOrNull(‘loading_county_drop_down’); value.style.display == ‘none’”, “10000″); selenium.select(“county”, “label=Amador County”);
You may also use logical operators to add additional logic.
selenium.waitForCondition(“var value = selenium.browserbot.findElementOrNull(‘loading_county_drop_down’); value.style.display == ‘none’ || selenium.browserbot.getUserWindow().document.form.county.options.text == ‘Select a county of California’;”, “10000″);
The same can also be accomplished by simply using waitForNotVisible and/or waitForSelectOptions.
selenium.waitForNotVisible(“loading_county_drop_down”); selenium.waitForSelectOptions(“county”,”regexpi:Amador County”);
Other useful variations:
You may have seen many AJAX intensive sites displaying a ‘Loading..’ or ‘Saving..’ or ‘Processing…’ message when a button or a link is clicked. In such cases you may find this to be helpful to wait for the status text to change or disappear.
Assuming that a ‘Saving’ message is displayed within a div tag when a fictional ‘Save’ button is clicked in the snippet below:
selenium.waitForCondition(“var value = selenium.getText(”//div[@class='save-text']”);value != ”Saving””, “20000″);
In general, .Net sites that use the web form controls, panel controls etc, trigger an asynchronous postback everytime a form control is filled or a .Net tab or panel control is clicked etc. In such cases, I’ve found the following to be very helpful in waiting for the postback to complete.
selenium.waitForCondition(“selenium.browserbot.getUserWindow().Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack() == false;”, “10000″);
Now for the killer part, for sites that use jQuery, if all you need is to confirm there aren’t any active asynchronous requests, then the following does the trick:
selenium.waitForCondition(“selenium.browserbot.getUserWindow().$.active == 0″, “10000″);
As you can see, there are several possible variations to a single command. The possibilities are endless with Selenium.
Happy Testing !!