• 自动化测试---等待


    1. 显示等待——WebDriverWait()

    WebDriverWait,配合该类的until()和until_not()方法,就能够根据判断条件而进行灵活地等待了。

    程序每隔xx秒看一眼,如果条件成立了,则执行下一步,否则继续等待,直到超过设置的最长时间,然后抛出TimeoutException。

    //页面元素是否在页面上可用和可被点击
    ExpectedConditions.elementToBeClickable(By locator);
    //页面元素是否处于被选中状态
    ExpectedConditions.elementToBeSelected(By locator);
    //页面元素在页面是否存在
    ExpectedConditions.presenceOfElementLocated(By locator);
    //是否包含特定的文本
    ExpectedConditions.textToBePresentInElement(locator, text)
    //页面元素值
    ExpectedConditions.textToBePresentInElementValue(locator, text);
    //标题
    ExpectedConditions.titleContains(title);
    public static void sendKeysByXPath(WebDriver driver, String path, String key) {
            WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
            WebElement element = wait.until(new ExpectedCondition<WebElement>() {
                @Override
                public WebElement apply(WebDriver d) {
                    return d.findElement(By.xpath(path));
                }
            });
            highLightElement(driver,element);
            element.clear();
            element.sendKeys(key);
        }

    FluentWait:可以动态设置巡检时间

    //FluentWait
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)  
                    
                    .withTimeout(60, TimeUnit.SECONDS)  
                
                    .pollingEvery(2, TimeUnit.SECONDS)  
                
                    .ignoring(NoSuchElementException.class);  
                
         WebElement ele1 = wait.until(new Function<WebDriver, WebElement>() {  
                
              public WebElement apply(WebDriver driver) {  
            
                return driver.findElement(By.id("xxxxxxx"));  
            
              }  
            
            }); 

    2.  隐式等待——implicitly_wait() 对象识别超时时间

    设置超时时间,等待页面加载,如果在规定时间加载完成就执行下一步;

    弊端:需要等整个页面加载完成,才能执行下一步

    周期:对driver整个周期都适用,执行一次即可

    pageLoadTimeout.页面加载超时时间

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

    3. 强制等待——sleep()

         Thread.sleep(2000);
  • 相关阅读:
    实战mysql导出中文乱码及phpmyadmin导入中文乱码
    Flex4:利用HttpService与ASP.NET传输JSON数据(登录为例)
    HTML和CSS的关键—盒子模型(Box model)(转载)
    ul,ol,dl区别
    Server Application Error详细解决办法
    信息系统规划(ISP)之BSP
    使用WebDevHelper辅助ASP.NET AJAX程序开发
    RMS 1.0 SP2
    使用IIS Request Viewer查看当前IIS连接
    Multidimensional Analysis 24 Principle
  • 原文地址:https://www.cnblogs.com/liu-Gray/p/7826321.html
Copyright © 2020-2023  润新知