自动化测试---等待

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);

 

posted on 2017-11-13 14:52  彩屏黑白  阅读(143)  评论(0编辑  收藏  举报

导航