zoukankan      html  css  js  c++  java
  • Selenium自动化测试脚本中三种等待时间简介

    为了提高脚本的稳定性,我们需要在脚本中增加等待时间

    第一种:强制等待

    Thread.sleep():固定休眠时间设置,Java的Thread类里提供了休眠方法sleep,导入包后就能使用

    sleep()方法以毫秒为单位  

    Thread.sleep(3000);

    ----执行到此时不管什么就固定的等待三秒之后再接着执行后面的操作

    第二种:隐式等待方法

    implicitlyWait()方法比sleep()方法智能,sleep()方法只能在一个固定的时间等待,而implicitlyWait()可以在一个时间范围内等待,称为隐式等待

    隐式等待采用全部设置,也就是说,你所有的findElement方法都会隐式等待10s

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    ----此方法针对执行脚本的所有对象,等待10秒

    第三种:显示等待方法

    WebDriverWait()

    就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception

    1、每500毫秒扫描界面是否出现元素

    2、针对单一元素

    3、可以设置超时时间

    WebDriverWait wait=new WebDriverWait(driver, 10);

    WebElement wl = wait.until(new ExpectedCondition<WebElement>() {  

                @Override  

                public WebElement apply(WebDriver d) {  

                    return d.findElement(By.cssSelector(".red_box"));  

                }  

            });

    ----在规定时间内等待 在10秒的范围内 出现.red_box元素就往下执行,如果10秒过后还没出现就跳出

    转自:http://www.cnblogs.com/xu-jia-li/p/6566709.html

  • 相关阅读:
    Count and Say leetcode
    Find Minimum in Rotated Sorted Array II leetcode
    Find Minimum in Rotated Sorted Array leetcode
    Search in Rotated Sorted Array II leetcode
    search in rotated sorted array leetcode
    Substring with Concatenation of All Words
    Subsets 子集系列问题 leetcode
    Sudoku Solver Backtracking
    Valid Sudoku leetcode
    《如何求解问题》-现代启发式方法
  • 原文地址:https://www.cnblogs.com/mrjade/p/7800826.html
Copyright © 2011-2022 走看看