web的自动化测试中,我们经常会遇到这样一种情况:当我们的程序执行时需要页面某个元素,而此时这个元素还未加载完成,这时我们的程序就会报错。怎么办?等待。等待元素出现后再进行对这个元素的操作。
在selenium-webdriver中我们用两种方式进行等待:明确的等待和隐性的等待。
明确的等待
明确的等待是指在代码进行下一步操作之前等待某一个条件的发生。最不好的情况是使用Thread.sleep()去设置一段确认的时间去等待。但为什么说最不好呢?因为一个元素的加载时间有长有短,你在设置sleep的时间之前要自己把握长短,太短容易超时,太长浪费时间。selenium webdriver提供了一些方法帮助我们等待正好需要等待的时间。利用WebDriverWait类和ExpectedCondition接口就能实现这一点。
下面的html代码实现了这样的一种效果:点击click按钮5秒钟后,页面上会出现一个红色的div块。我们需要写一段自动化脚本去捕获这个出现的div,然后高亮之。
Wait.html: <html> <head> <title>Set Timeout</title> <style> .red_box {background-color: red; width = 20%; height: 100px; border: none;} </style> <script> function show_div(){ setTimeout("create_div()", 5000); } function create_div(){ d = document.createElement('div'); d.className = "red_box"; document.body.appendChild(d); } </script> </head> <body> <button id = "b" onclick = "show_div()">click</button> </body> </html>
下面的代码实现了高亮动态生成的div块的功能:
import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class TestWait { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); String url = "file:/C:/Users/jgong/Desktop/wait.html"; driver.get(url); WebDriverWait wait = new WebDriverWait(driver, 10); driver.findElement(By.id("b")).click(); WebElement wl = wait.until(new ExpectedCondition<WebElement>() { @Override public WebElement apply(WebDriver d) { return d.findElement(By.cssSelector(".red_box")); } }); ((JavascriptExecutor) driver).executeScript( "arguments[0].style.border='5px solid yellow'", wl); } }
隐性的等待
隐性的等待其实就相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class TestWait { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); // 设置10秒 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); String url = "file:/C:/Users/jgong/Desktop/wait.html"; driver.get(url); driver.findElement(By.id("b")).click(); /* * WebDriverWait wait = new WebDriverWait(driver, 10); WebElement wl = * wait.until(new ExpectedCondition<WebElement>() { * * @Override public WebElement apply(WebDriver d) { return * d.findElement(By.cssSelector(".red_box")); } }); */ WebElement wl = driver.findElement(By.cssSelector(".red_box")); ((JavascriptExecutor) driver).executeScript( "arguments[0].style.border='5px solid yellow'", wl); } }
两者选其一,第二种看起来一劳永逸呀。哈哈
相关推荐
博客“ruby+selenium-webdriver测试--第一个例子源代码”
博客“ruby+selenium-webdriver测试--多个测试用例”源代码
1.11 selenium webdriver学习(十一)------------如何等待页面元素加载完成 . . . . . . . . . . . . . . . . . .33 1.12 selenium webdriver学习(十二)------------如何利用selenium-webdriver截图 . . . . . . ...
本资源是selenium-webdriver-practical-guide-2014.pdf文档的源代码
selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创selenium-webdriver原创
注意selenium-webdriver只支持1.8.7以上的ruby版本; 使用gem安装selenium-webdriver;打开命令行,输入下列代码完成安装。注意,如果你的开发环境需要http proxy的话,请注意在gem命令中加入--http_proxy参数; ...
Selenium.WebDriver.4.0.0-alpha02.nupkg,Selenium.WebDriver 4.0,Selenium is a set of different software tools each with a different approach to supporting browser automation. These tools are highly ...
WebtestRecorder插件---Selenium Webdriver脚本录制工具--For Java
selenium-webdriver-2.5.0.gem
ruby2.0.0+selenium-webdriver+watir-webdriver+rails离线备份,设置环境变量后可使用(未测试)
Selenium WebDriver 学习笔记 API大全 中文API 元素 操作大全
selenium webdriverAPI-python
selenium-webdriver-2.45.0.gem
全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用实例,第2部分实战应用篇基于丰富的实战案例讲解页面元素的定位方法以及...
博客“ruby+selenium-webdriver测试-测试用例源代码”
博客“ruby+selenium-webdriver测试-逻辑与数据分离”对应源代码
Selenium-Webdriver系列教程 Selenium 1.x时代已经远去,它理应躺在历史的角落里,靠着壁炉烤着火,抽着旱烟,在袅袅的升起的青烟中回忆那曾经属于自己的美好时代。 不过事实却并非如此,现今原本早应退役的...
selenium-webdriver-2.40.0.gem
自动化测试:Selenium webdriver学习笔记C#版
Selenium WebDriver Practical Guide will guide you through the various APIs of WebDriver which should be used in automation tests, followed by a discussion of the various WebDriver implementations ...