selenium之ExpectedConditions类
API中对于该类的介绍:Canned ExpectedCondition
s which are generally useful within webdriver tests。很笼统,大概意思就是在webdriver的测试中会有用,那到底有什么用呢,下面我们就来一探究竟。
该类没有构造函数,所有的方法都是静态的,所以我们可以直接用类名调用,我只介绍里面几个方法,其它方法的用法都类似,具体查api
- 1.alertIsPresent()判断alert弹框出现了没,返回值是ExpectedCondition<Alert>,我们可以这样用
-
public void testalertIsPresent() { dr.findElement(By.id("alert")).findElement(By.className("alert")).click(); ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent(); Alert alert = e.apply(dr); alert.accept(); }
也可以这样用
public void testalertIsPresentW() { WebDriverWait wait = new WebDriverWait(dr,10); dr.findElement(By.id("alert")).findElement(By.className("alert")).click(); ExpectedCondition<Alert> e = ExpectedConditions.alertIsPresent(); Alert alert = wait.until(e); alert.accept(); }
apply用了com.google.common.base.Function的方法,而until用了FluentWait的方法。那么这两者有和区别呢,通过下面这个例子介绍,
- 2. textToBePresentInElementLocated():文本是否出现在所定位的元素中
-
public void testtextToBePresentInElementLocated() { WebDriverWait wait = new WebDriverWait(dr,10); dr.findElement(By.className("wait")).click(); ExpectedCondition<Boolean> e1 = ExpectedConditions.textToBePresentInElementLocated(By.className("red"),"wait for display"); System.out.println(e1.apply(dr));//false
//System.out.println(wait.until(e1)); //报true }apply不会去等待所找元素是否存在,直接false报错no such elment。而until会在等待10秒,如果出现则返回true,否则超时。
-
3.visibilityOfElementLocated(),该方法是判断定位的元素是否存在,使用的方法时(),apply也会去等待
public void testvisibilityOfElementLocated() { WebDriverWait wait = new WebDriverWait(dr,10); dr.findElement(By.className("wait")).click(); ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red")); System.out.println(wait.until(e1).getText()); //wait for display System.out.println(e1.apply(dr).getText()); //wait for display }
-
4.visibilityOf(),判断元素是否可见
public void testvisibilityOf() { WebDriverWait wait = new WebDriverWait(dr,10); ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOf(dr.findElement(By.className("wait"))); System.out.println(e1.apply(dr).getAttribute("value")); //wait //System.out.println(wait.until(e1).getAttribute("value"));//wait }
5.visibilityOfElementLocated(),定位的元素是否可见
public void testvisibilityOfElementLocated() { WebDriverWait wait = new WebDriverWait(dr,10); dr.findElement(By.className("wait")).click(); ExpectedCondition<WebElement> e1 = ExpectedConditions.visibilityOfElementLocated(By.className("red")); System.out.println(wait.until(e1).getText()); //wait for display System.out.println(e1.apply(dr).getText()); //wait for display }
以上列举了TestExpectedConditions的用法,鉴于apply会存在失败的情况,建议使用until。
PS:小插曲
下面的例子是api中关于FluentWait的用法
Sample usage: // Waiting 30 seconds for an element to be present on the page, checking // for its presence once every 5 seconds. Wait<WebDriver> wait = new FluentWait<WebDriver>(driver) .withTimeout(30, SECONDS) .pollingEvery(5, SECONDS) .ignoring(NoSuchElementException.class); WebElement foo = wait.until(new Function<WebDriver, WebElement>() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); } });
详细用法:
@Test(enabled=false) public void testuntilWebElement() { WebDriverWait wait = new WebDriverWait(dr,10); WebElement we = wait.until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver arg0) { // TODO Auto-generated method stub return arg0.findElement(By.id("user")); }}) ; we.sendKeys("test"); } @Test(enabled=false) public void testuntilBoolean() { WebDriverWait wait = new WebDriverWait(dr,10); boolean we = wait.until(new ExpectedCondition<WebElement>(){ @Override public WebElement apply(WebDriver arg0) { // TODO Auto-generated method stub return arg0.findElement(By.id("user")); }}).isDisplayed(); System.out.println(we); }
两者用法目的差不多,可以根据具体情况选择使用哪种方式。
文中所用到的html例子:
http://pan.baidu.com/s/1i3jjo3b
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************
作者:乔叶叶
博客地址:http://www.cnblogs.com/qiaoyeye/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
******************************************************************************************************************************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?