WebDriverAPI(7)
查看页面元素的属性
测试网址
http://www.baidu.com
Java语言版本API实例
@Test
public void getWebElementAttribute() {
driver.manage().window().maximize();
driver.navigate().to(url);
String inptString = "seleium测试内容";
WebElement input = driver.findElement(By.id("kw"));
input.sendKeys(inptString);
//取输入框中的值
String inputText = input.getAttribute("value");
Assert.assertEquals(inputText, "seleium测试内容");
}
获取页面元素的Css值
测试网址
http://www.baidu.com
Java语言版本API实例
@Test
public void getElenmentCssValue() {
driver.manage().window().maximize();
driver.get(url);
WebElement input = driver.findElement(By.id("kw"));
//获取元素宽度
String inputwidth = input.getCssValue("width");
//断言判断
Assert.assertEquals("500px", inputwidth);
}
隐式等待
测试网址
http://www.baidu.com
Java语言版本API实例
@Test
public void testImplictWait() {
driver.manage().window().maximize();
driver.navigate().to(url);
/*使用implicitlyWait方法设定查找页面元素的等待时间,调用findElement方法时没有立刻找到
* 定位元素会等待设定的等待时长10秒,如果还没找到则抛出NoSuchElementException
* */
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
WebElement seartchInputBox = driver.findElement(By.id("kw"));
WebElement seartchButton = driver.findElement(By.id("su"));
seartchInputBox.sendKeys("找到搜索框元素");
seartchButton.click();
} catch (NoSuchElementException e) {
Assert.fail("没有找到搜索框");
e.printStackTrace();
}
}
显示等待
等待条件 | WebDriver方法 |
页面元素是否在在页面上可用(enabled)和可被单击 | elementToBeClickable(By locator) |
页面元素处于被选中状态 | elementToBeSelected(WebElement element) |
页面元素在页面中存在 | presenceOfElementLocated(By locator) |
在页面元素中是否包含特定的文本 | textToBePresentInElement(By locator) |
页面元素值 | textToBePresentInElementValue(By locator,java.lang.String text) |
标题(title) | titleContains(java.lang.String title) |
测试HTML代码
<html>
<title>你喜欢的水果</title>
<body>
<p>请选择你爱吃的水果</p>
<br>
<select name='fruit'>
<option id='peach' value='taozi'>桃子</option>
<option id='watermelon' value='xigua'>西瓜</option>
</select>
<br>
<input type='checkbox'>是否喜欢吃水果?</input>
<br><br>
<input type="text" id="text" value="今年夏天西瓜相当甜">文本框</input>
</body>
</html>
Java语言版本API实例
@Test
public void testExplicitWait() {
driver.manage().window().maximize();
driver.get(url);
//声明一个WebDriverWait对象,设定触发条件的最长等待时间为10秒
WebDriverWait wait = new WebDriverWait(driver,10);
//调用ExpectedConditions的titleContains方法判断标题中是否包含水果两字
wait.until(ExpectedConditions.titleContains("水果"));
System.out.println("网页标题出现了“水果的关键字”");
//获取桃子选项对象
WebElement select = driver.findElement(By.xpath("//option[@id='peach']"));
//调用ExpectedConditions的elementToBeSelected方法判断桃子是否处于选中状态
wait.until(ExpectedConditions.elementToBeSelected(select));
System.out.println("下拉列表框“桃子属于选中状态”");
//调用ExpectedConditions的elementToBeClickable方法判断复选框是否处于可见及是否可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='checkbox']")));
System.out.println("页面复选框处于显示和可被单击状态");
//调用ExpectedConditions的presenceOfElementLocated方法判断p是否存在页面中
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//p")));
System.out.println("页面的p元素标签已显示");
//获取页面p标签元素
WebElement p = driver.findElement(By.xpath("//p"));
//调用ExpectedConditions的textToBePresentInElement方法判断p标签中是否包含爱吃的水果这几个字
wait.until(ExpectedConditions.textToBePresentInElement(p, "爱吃的水果"));
System.out.println("页面p标签元素包含爱吃的水果");
}
自定义的显示等待
被测试HTML代码
同上一个
Java语言版本API实例
@Test
public void testExplicitWait() {
driver.manage().window().maximize();
driver.navigate().to(url);
try {
//显示等待判断是否可以从页面获取文字输入框对象,如果可以获取则执行后面测试用例
WebElement textInputBox = (new WebDriverWait(driver,10)).until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d){
return d.findElement(By.xpath("//*[@type='text']"));
}
});
//断言判断输入框中是否包含这几个字
Assert.assertEquals("今年夏天西瓜相当甜!", textInputBox.getAttribute("value"));
//显示等待判断p标签中是否包含爱吃两个字,若包含则继续执行后面的测试同理
Boolean containTextFlag = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver d){
return d.findElement(By.xpath("//p")).getText().contains("爱吃");
}
});
//断言判断是否包含爱吃关键字
Assert.assertTrue(containTextFlag);
//显示等待判断文本框是否可见,若可见继续执行后面的测试用例
Boolean inputTextVisibleFlag = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver d){
return d.findElement(By.xpath("//*[@type='text']")).isDisplayed();
}
});
//断言判断文本框是否可见
Assert.assertTrue(inputTextVisibleFlag);
} catch (NoSuchElementException e) {
//若显示等待条件未被满足则执行
Assert.fail("页面上的输入框元素未未找到!");
e.printStackTrace();
}
}
WebDriverAPI(7)的更多相关文章
- WebDriverAPI(10)
操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...
- WebDriverAPI(9)
操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...
- WebDriverAPI(4)
单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...
- WebDriverAPI(2)
操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...
- WebDriverAPI(8)
判断页面元素是否存在 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void testIsElementPresent(){ driver. ...
- WebDriverAPI(6)
在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...
- WebDriverAPI(5)
将当前浏览器截屏 测试网址 http://www.baidu.com Java语言版本实例 @Test public void captureScreenInCurrentWindows() { dr ...
- WebDriverAPI(3)
获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...
- WebDriverAPI(1)
访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...
随机推荐
- 如何优化Mysql数据库
1.添加主键ID 2.尽量避免使用select * form table 3.创建索引 对于查询占主要的应用来说,索引显得尤为重要.很多时候性能问题很简单的就是因为我们忘了添加索引而造成的,或 ...
- 2018.09.11 loj#10216.五指山(exgcd)
传送门 就是一个exgcd的板子. 但注意算距离差的时候是在一个环上面算. 还有,答案要开long long233... 注意这两点之后就是exgcd板子了. 代码: #include<bits ...
- vs2010 EF4.0 访问mysql
需要安装mysql-connector-net-6.3.5 6.8.9的安装完后在dbfirst里找不到对应的提供程序 链接字符串里需要 指定下编码(如果不是gbk的话) <add name=& ...
- IntelliJ IDEA 2017版 编译器使用学习笔记(二) (图文详尽版);IDE快捷键使用
补充介绍IntellJ 介绍主菜单功能及相关用途: File -------------> 对文件进行操作 Edit ------------> 对文本进行操作 View -------- ...
- struts2 的特征
web.xml <filter> <filter-name>struts2</filter-name> <filter-class>org.apache ...
- Tensorflow从源代码编译2
https://blog.csdn.net/qq_37674858/article/details/81095101 https://blog.csdn.net/yhily2008/article/d ...
- 关闭父类弹出的ifream窗口
parent.document.getElementById('zhuce').style.display = 'none';
- POJ2676 – Sudoku(数独)—DFS
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24081 Accepted: 11242 Specia ...
- uva 579 ClockHands 几何初接触 求时针与分针的夹角
貌似是第一次接触几何题... 求时针与分针的夹角,这不是小学生的奥数题么.我小时候也想过这问题的. 每过一小时时针走1/12*360=30度,每过一分钟时针走1/60*30=0.5度,分针走1/60* ...
- Android-天气预报Demo-JSON数据解析
在上两篇博客,Android-解析JSON数据(JSON对象/JSON数组),Android-Gson解析JSON数据(JSON对象/JSON数组),是介绍了解析本地文件里面的JSON数据: Andr ...