Selenium common waiting mechanisms and their characteristics and usage methods

Table of contents

1. Forced waiting 

2. Implicit waiting 

3. Display waiting 


1. Forced waiting 

Forced waiting is accomplished by calling Thread.sleep(timeout) directly in the program. The advantage of this method is that it is easy to use and the syntax is relatively simple. The disadvantage is that it needs to be forced to wait for a fixed time, which may cause the test to take too long. .

    private static void test02() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#kw")).clear();
        sleep(3000);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("前端vue");
        webDriver.findElement(By.cssSelector("#su")).click();

    }

The reason for introducing waiting is that in many cases, the running speed of the program is greater than the rendering speed of the web page, so a waiting mechanism needs to be introduced. 

2. Implicit waiting 

If the waiting time is 1 day, using forced waiting will wait for 1 day. For implicit waiting, it will wait for up to 1 day. Because if we obtain the elements on the page within this day, then the implicit wait will no longer wait, but will directly start executing the subsequent code. If the page element has not been obtained for more than 1 day, an error will be reported.

Statements used:

webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.DAYS);

 To use implicit waiting, you need to use the manage method in the driver, and then call the implicitlyWait method of the timeouts method.

The more important one is implicitlyWait. You can take a look at its source code:

You can see that one parameter of the implicitlyWait method is of type long, which is the specified number, and the latter is of type TimeUnit, which is the specified unit.

Here we test it with actual code:

private static void test02() throws InterruptedException {
        ChromeOptions options=new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver=new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("软件测试");
        webDriver.findElement(By.cssSelector("#su")).click();
        webDriver.findElement(By.cssSelector("#kw")).clear();
        //sleep(3000);
        //隐式等待1天
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.DAYS);
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("前端vue");
        webDriver.findElement(By.cssSelector("#su")).click();

    }

We set up to wait for one day, but for implicit waiting, the program is completed in one go. Because the waiting elements are obtained directly from the page, there is no need to wait for 1 day to complete the test.

3. Display waiting 

For example, on the homepage of Baidu search, we need to determine whether the bottom element is a hyperlink and whether it can be clicked. 

Click on this method and you can see that there are many methods for judging elements. What we want to use is to judge whether the element can be clicked, so we use the elementToBeClickable method.

    private static void test07() {
        //创建驱动
        WebDriver webDriver=new ChromeDriver();
        //打开百度
        webDriver.get("https://www.baidu.com/");
        //判断元素是否可以被点击
        WebDriverWait webDriverWait=new WebDriverWait(webDriver,3000);
    webDriverWait.until(ExpectedConditions.elementToBeClickable
                (By.cssSelector("#bottom_layer > div > p:nth-child(7) > a")));

    }

No error is reported, indicating that the conditions are met. If we set an element that does not exist, and wait until the set waiting time has passed, an error NoSuchElementException will be reported :

in conclusion:

The syntax for display waiting is relatively complicated, but you can specify a certain element to wait for, which reduces the waiting time of the entire test.

Guess you like

Origin blog.csdn.net/m0_67995737/article/details/132166503