[selenium webdriver Java]使用自定义条件同步测试

Selenium WebDriver可以结合ExpectedCondition类来定义自己期望的条件

创建一个新的ExpectedCondition接口,必须实现apply方法

等待元素出现

 1 public void testWithImplicitWait(){
 2     System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
 3     WebDriver driver = new ChromeDriver();
 4     driver.get("http://map.baidu.com");
 5
 6         //点击展开当前城市
 7         WebElement curCity = driver.findElement(By.id("curCity"));
 8     curCity.click();
 9
10         //设置等待时间10秒
11     WebDriverWait wait = new WebDriverWait(driver,10);
12     //创建一个新的ExpecctedCondition接口,就必须实现apply方法
13     WebElement message = wait.until(
14             new ExpectedCondition<WebElement>(){
15                 public WebElement apply(WebDriver d){
16                     return d.findElement(By.id("selCityHotCityId"));
17                 }
18             }
19             );
20
21         driver.quit();
22     }    

示例代码

等待元素属性值改变

基于某些事件的操作后,元素的属性值可能会改变。例如,一个不可输入的文本框变为可输入状态。自定义的等待可以在元素的属性上实现。

在这个例子中,ExpectedCondition类将等待返回Boolean值

1 (new WebDriverWait(driver, 10).util(new ExpectedCondition<Boolean>(){
2     public Boolean apply(WebDriver d){
3       return d.findElement(By.id("username")).
4 getAttribute("readonly").contains("true");
5     }
6 }));

等待元素变为可见

开发人员会隐藏或是在一系列操作后显示某元素。指定的元素一开始已经存在于DOM中,但是为隐藏状态,当用户经过指定的操作后变为可见。那么这样的自定义期望条件应该如下:

1 (new WebDriverWait(driver, 10).util(new ExpectedCondition<Boolean>(){
2     public Boolean apply(WebDriver d){
3       return d.findElement(By.id("xxx")).isDisplayed();
4     }
5 }));

等待DOM事件

自定义的等待可以通过执行一段javascript代码并检查返回值来完成

1 (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>(){
2 public Boolean apply(WebDriver d){
3 JavascriptExecutor js = (JavascriptExecutor) d;
4 return (Boolean)js.executeScript("return jQuery.active == 0");
5 }
6 });
时间: 2024-08-03 08:16:08

[selenium webdriver Java]使用自定义条件同步测试的相关文章

[selenium webdriver Java]常用api

1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 1 public class GetText { 2 @Test 3 public void testGetText(){ 4 //启动driver,打开被测页面 5 System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); 6 WebDri

[selenium webdriver Java]隐式的等待同步

Selenium WebDriver提供了隐式等待来同步测试.当使用了隐式等待执行测试的时候,如果WebDriver没有在DOM中找到元素,将继续等待,超出设定时间后,抛出找不到元素异常 即,当元素没有立即出现的时候,隐式等待将等待一段时间后,再查找DOM.默认时间是0. 一旦设置了隐式等待,它将存在在整个webdriver对象实例的生命周期中,隐式等待会让一个正常响应的应用的测试变慢,会在寻找每个元素时进行等待,这样增加了整个测试执行的时间. 应该避免或减少使用隐式等待 //等待60s dri

[selenium webdriver Java]元素定位——findElement/findElements

策略 语法 语法 描述 By id driver.findElement(By.id()) driver.findElements(By.id()) 通过id属性定位元素 By name driver.findElement(By.name()) driver.findElements(By.name()) 通过name属性定位元素 By class name driver.findElement(By.className()) driver.findElements(By.className(

[selenium webdriver Java]显示的等待同步

显示等待可以在执行下一次操作时,自定义等待条件 显示的等待只需要执行在需要同步的地方而不影响脚本的其他地方 Selenium WebDriver提供了WebDriverWait和ExpectedCondition类来执行显示等待 ExpectedCondition类提供了一系列预定义好的条件来等待.下面是一些常用的条件 预定义条件 方法名 元素可见可点击 elementToBeClickable(By locator) 元素可被选中 elementToBeSelected(WebElement

Selenium WebDriver java 简单实例

开发环境 JDK 下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html Eclipse: 下载地址:http://www.eclipse.org/downloads/ Selenium jar包 (这里用的是:selenium-Java-2.45.0.zip ,selenium-server-standalone-2.45.0.jar) 下载地址:http://code.google.com/p/sele

[selenium webdriver Java]处理弹出窗口

Selenium WebDriver测试弹出窗口,包括识别弹出窗口,将driver转到新的窗口,在新的串钩中执行而是步骤,然后再转换到最初的窗口. 通过名称(name)识别和处理: Selenium WebDriver允许我们通过name属性或窗口的句柄来识别窗口,然后通过WebDriver.switchTo().window()方法在不同的窗口之间进行切换. window name属性的定义 name 属性可设置或得到窗口的名称,其值为字符串.语法如下: 1 window.name = [na

[selenium webdriver Java]检查元素是否存在

Selenium WebDriver没有实现Selenium RC的isElementPresent()方法来检查页面上的元素是否存在. 在WebDriver中封装一个类似的方法,如下: 1 public boolean isElementPresent(WebDriver driver, By by){ 2 try{ 3 driver.findElement(by); 4 return ture; 5 }catch(Exception e){ 6 return false; 7 } 8 }

selenium+webdriver+java基本知识点

1.基本小程序例子: 通过谷歌浏览器实现一个基本点登陆操作 String key="webdriver.chrome.driver"; String value="C:/Users/gmsd11/Desktop/selenium/chromedriver.exe"; System.setProperty(key, value);//系统自动配置相应的参数 WebDriver driver=new ChromeDriver();//实例化对象时对应的浏览器已经打开了(

[selenium webdriver Java]检查元素状态

许多测试失败是因为点击一个元素失败或者在一个不可见的字段中输入文字,或者是在不可输入的文本中输入文字. 我们可以在具体操作之前,检查一下元素的状态.WebElement类提供了这样的方法. 方法 目的 isEnabled() 检查元素是否启用(只有在input元素设为disabled时,返回false) isSelected() 检查元素是否被选中(单选.多选.下拉框) isDisplayed() 检查元素是否可见