自动化测试中,等待时间的运用占据了举足轻重的地位,日常咱们须要处理不少和时间息息相关的场景,例如:java
打开新页面,只要特定元素出现而不用等待页面所有加载完成就对其进行操做web
设置等待某元素出现的时间,超时则抛出异常chrome
设置页面加载的时间app
…..异步
webdriver类中有三个和时间相关的方法:
1.pageLoadTimeout 设置页面彻底加载async
2.setScriptTimeout 设置异步脚本的超时时间
测试
3.implicitlyWait 识别对象的超时时间网站
咱们就从这里开始,慢慢揭开他神秘的面纱。ui
pageLoadTimeout方法用来设置页面彻底加载的超时时间,彻底加载即页面所有渲染,异步同步脚本都执行完成。前面的文章都是使用get 方法登陆安居客网站,你们应该能感受到每次打开网页后要等很长一段时间才会进行下一步的操做,那是由于没有设置超时时间而get方法默认是等待页面所有加 载完成才会进入下一步骤,加入将超时时间设置为3S就会中断操做抛出异常。spa
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); input.sendKeys("selenium"); }catch(Exception e){ e.printStackTrace(); }finally{ Thread.sleep(3000); driver.quit(); } }
ps:若是时间参数为负数,效果没有使用这个方法是同样的,接口注释中有相关说明:”If the timeout is negative, page loads can be indefinite”.
若是想在抛出异常后并不中断而是继续执行下面的操做那该怎么办呢?可使用try,catch的组合来实现这个功能
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ }finally{ WebElement input=driver.findElement(By.xpath("//input[@id='glb_search0']")); if(input.isDisplayed()) input.sendKeys("selenium"); } }
这样,当页面加载3S后就会执行下面的操做了。
设置异步脚本的超时时间,用法同pageLoadTimeout同样就再也不写了,异步脚本也就是有async属性的JS脚本,能够在页面解析的同时执行。
识别对象的超时时间,若是在设置的时间类没有找到就抛出一个NoSuchElement异常,用法参数也是和pageLoadTimeout同样,你们能够本身试验试验。
上文中介绍了如何才能在使用pageLoadTimeout抛出异常的同时继续执行,可是现有的方法仍是不完美,若是输入框在3S后没有出现仍是会 报错,怎么办呢?机智的同窗能够想到用sleep方法来实现,为了方便演示换个更明显的需求来讲明。安居客的首页上有个切换城市的连接,当点击城市的时候 就会显示所有的城市以供选择这时display属性就会变化
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" ,"C:\\Program Files(x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3,TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ }finally{ WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']")); WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']")); Actions actions=new Actions(driver); actions.clickAndHold(city).perform(); while(citys.isDisplayed()){ System.out.println("sleep"); Thread.sleep(3000); } Thread.sleep(3000); driver.quit(); } }
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ }finally{ WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']")); WebElement citys=driver.findElement(By.xpath("//div[@id='city-panel']")); Actions actions=new Actions(driver); actions.clickAndHold(city).perform(); while(citys.isDisplayed()){ System.out.println("sleep"); Thread.sleep(3000); } Thread.sleep(3000); driver.quit(); } }
执行后会每过3S就会输出一次sleep,可是这样的代码显然不够高大上,并且没有限制会一直无限的等待下去,下面介绍一种高大上的方法,就是until,先看代码
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedCondition; public class NewTest{ public static void main(String[] args) throws InterruptedException { System.setProperty ( "webdriver.chrome.driver" , "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe" ); WebDriver driver = new ChromeDriver(); try{ //设置超时时间为3S driver.manage().timeouts().pageLoadTimeout(3, TimeUnit.SECONDS); driver.get("http://shanghai.anjuke.com"); }catch(Exception e){ }finally{ WebElement city=driver.findElement(By.xpath("//a[@id='switch_apf_id_8']")); Actions actions=new Actions(driver); actions.clickAndHold(city).perform(); //最多等待10S,每2S检查一次 WebDriverWait wait=new WebDriverWait(driver,10,2000); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { System.out.println("sleep"); return !driver.findElement(By.xpath("//div[@id='city-panel']")).isDisplayed(); } }); Thread.sleep(3000); driver.quit(); } }