代码改变世界

比较下 下面的代码 隐式等待

2016-03-04 14:30  xlmig  阅读(197)  评论(0编辑  收藏  举报
    /**
         * getCurrentURL:(get the current page URL address). 

         * @author huchan
         * @param driver  --- the web driver instance
         * @return String ---the url of current page
         * @since JDK 1.6
         */
        public String getCurrentPageURL(){
            String pageurl="";
            JavascriptExecutor je=(JavascriptExecutor) driver;
            final String docstate=(String) je.executeScript("return document.readyState");
            logger.info("Current loading page state is:"+docstate);
            WebDriverWait wait=new WebDriverWait(driver,120);
            ExpectedCondition<Boolean> ec = new ExpectedCondition<Boolean>() {
                  @Override
                public Boolean apply(WebDriver d) {
                    return (docstate.equals("complete"));
                  }
                };
            try{
               logger.info("We just wait for the current page load correctly now...");
               wait.until(ec);           
               pageurl=driver.getCurrentUrl();
               logger.info("we found the current url is:"+pageurl);
            }
            catch(TimeoutException e){
                pageurl="time out:120 seconds";
                logger.error("Sorry the page cannot be loaded correctly:"+e.getMessage()+driver.getCurrentUrl());
            }
            return pageurl;
        }

 

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(new ExpectedCondition<WebElement>(){
         @Override
         public WebElement apply(WebDriver d) {
                   return d.findElement(By.id("myDynamicElement"));
         }});



WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));