WebDriver的显示等待

       最近在学习自动化测试,之前同事写的代码充斥着大量的Thread.sleep(),今天对相关代码进行了替换,在此总结下WebDriver的显示等待的一些经验。

     WebDriver的显示等待大概脉络如下:

         

     通过研读源码可以知道,WebDriverWait是FluentWait的子类(设置poll的sleeptime为500ms且忽略NotFoundException)

,在使用中,通常给它们的实例的until方法传入ExpectedConditions的静态方法或者一个Function的实例来调用,常见代码如下:

此处以FluentWait类作为示范:

1.  配合ExpectedConditions类的静态方法

 Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(300,TimeUnit.SECONDS)
                    .pollingEvery(50,TimeUnit.MILLISECONDS)
                    .ignoring(NoSuchElementException.class)
                    .ignoring(InvalidSelectorException.class);
            List<WebElement> webElements = wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath(exceptStrTemp)));
            if (webElements != null && webElements.size() > 0)
            {
                status = true;
            }

2.  搭配New Function,在使用中发现如果返回对象为List<WebElement>时,apply方法只会被执行一次,导致失效。

  Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                    .withTimeout(300,TimeUnit.SECONDS)
                    .pollingEvery(50,TimeUnit.MILLISECONDS)
                    .ignoring(NoSuchElementException.class);
            //只能尝试定位一个对象,不可定位List,否则不生效
           WebElement webElement = wait.until(new Function<WebDriver,WebElement>() {
                @Nullable
                @Override
                public WebElement apply(@Nullable WebDriver webDriver) {
                    log.debug("WebDriverWait的 apply方法被调用!");
                    return driver.findElement(By.xpath(exceptStrTemp));
                }
            });

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值