Java + Selenium Test framework (the wait mechanism) version Evolution II

Source: Internet
Author: User

With Selenium-ide recording, and writing test cases directly with Selenium-java.jar, you will find that it executes very fast. Much more than the speed of manual operation, and may even exceed the speed of browser loading (faster than the browser?) The result is no element found).

If there is an element on the page but is prompted during the test, NoSuchElementException there are two reasons: 1, your strategy for fetching the element is wrong, 2, the element is not loaded at execution time.

After the call driver.get(url) , in order to ensure that the element can be properly initialized, you need to add a wait to load the completion of the function, to ensure that the page is loaded.

Throw the first question how to confirm page load complete?

How do I confirm that page loading is complete?

In selenium, a wait mechanism is provided in the Waitforpagetoload, which class WaitForPageToLoad provides two methods: one is to use JS to execute document[‘readyState‘]; . If this method is not supported, the second method is used, and if the length of the page does not change in 1s, it is considered to have been loaded.

If you wait for a setting that is not completed for a long time, then terminate.

Call Waitforpagetoload when page loads

The function is not provided in Webdriver and is encapsulated in the selenium. In order to use the Webdriver program, the ability to use selenium RC requires the introduction of forward-compatible webdriverbackedselenium.

public class baidumainpagewaittest{protected webdriver driver = null;    protected Selenium Selenium = null;    protected Baidumainpage page = null;        @BeforeClass public void init () {System.setproperty ("Webdriver.ie.driver", "Lib\\iedriverserver.exe");        Desiredcapabilities iecapabilities = Desiredcapabilities.internetexplorer ();        Iecapabilities.setcapability (internetexplorerdriver.introduce_flakiness_by_ignoring_security_domains,true); /** * <b>webdriverbackedselenium </b> * allows for Webdriver and Selenium to live side-by-side .<br/> * Provides a simple mechanism for a managed migration from the * existing Selenium API to We        Bdriver ' s.<br/> * Does not require the standalone Selenium RC server to be run.<br/> */    Driver = new Internetexplorerdriver (iecapabilities);        } @BeforeMethod public void Initpage () {String path = "http://www.baidu.com/"; PAge = new Baidumainpage (driver);    Selenium = new Webdriverbackedselenium (Driver,path);        Selenium.open (path);        Driver.get (path);        Selenium.waitforpagetoload ("5000");    Pagefactory.initelements (Driver, page); }/** * After creating a Webdriverbackedselenium instance with a given Driver, * one does not having to call STA     RT ()-As the creation of the Driver already started the session.     * At the end of the test, stop () should is called instead of the Driver ' s Quit () method.      * * @see <a href= "https://code.google.com/p/selenium/wiki/SeleniumEmulation" >selenium Emulation wiki</a>            */@AfterClass public void Quit () {try{selenium.stop ();        if (driver! = null) driver.quit ();        } finally {driver = null; }    }}
Adds a wait mechanism to asynchronous loaded elements
//如下这段摘自网上在Selenium中,提交会引起页面加载的情况下,需要调用waitforpagetoload。   但是在我们的测试中,我发现就算waitforpagetoload成功返回后,也不意味着页面所有元素加载成功。如果此时针对某一个元素进行断言/操作,就会失败。   原来Selenium中的waitforpagetoload方法成功返回后,并未包括页面onload部分的javascript。这就造成测试脚本概率性失败,并且调试十分困难。   

Because the page may have a lazy load situation. Although the page has been rendered, there may be ajx lazy loading. Some elements need to wait until the AJAX call is complete before they can be displayed. At this point, you need to add a wait mechanism on the page.

/** * 用于等待某个元素出现,软等待 * @param by */public boolean wait(final By by){    new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {        public Boolean apply(WebDriver arg0) {            return arg0.findElement(by).isDisplayed();        }    });    return true;}

For example, you want to find an element that id= "name":

if (wait(By.id("name")))     xxx;else    throw new ElementNotFoundException("xxx not found");
The ultimate big strokes hard waiting

Programme one:

driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.SECONDS);   

Scenario Two:

public static void sleep(int seconds) {    try {        TimeUnit.SECONDS.sleep(seconds);    } catch (InterruptedException e) {        e.printStackTrace();    }}

Java + Selenium Test framework (the wait mechanism) version Evolution II

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.