WebDriver API 实例详解(三)
二十一、模拟鼠标右键事件
被测试网页的网址:
http://www.sogou.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.sogou.com"; @Test
public void opentest() {
driver.get(url);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Actions action = new Actions(driver);
action.contextClick(driver.findElement(By.id("query"))).perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十二、在指定元素上方进行鼠标悬浮
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<script type="text/javascript">
function showNone(){
document.getElementById('div1').style.display = "none";
}
function showBlock(){
document.getElementById('div1').style.display = "block";
}
</script>
<style type="text/css">
#div1{
position:absolute;
width:200px;
height:125px;
z-index:1;
left:28px;
top:34px;
background-color:#0033CC;
}
</style>
</head>
<body onload="showNone()">
<div id="div1"></div>
<a onmouseover="showBlock()" id="link1">鼠标指过来</a>
<a onmouseover="showNone()" id="link2">鼠标指过来</a>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file9.html";
driver.get(url);
WebElement link1 = driver.findElement(By.xpath("//a[@id='link1']"));
WebElement link2 = driver.findElement(By.xpath("//a[@id='link2']"));
Actions action = new Actions(driver);
action.moveToElement(link1).perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
action.moveToElement(link2).perform();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十三、在指定元素上进行鼠标单击左键和释放的操作
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<script type="text/javascript">
function mouseDownFun(){
document.getElementById('div1').innerHTML += '鼠标左键被按下<br/>';
}
function mouseUpFun(){
document.getElementById('div1').innerHTML += '已经被按下的鼠标左键被释放抬起<br/>';
}
function clickFun(){
document.getElementById('div1').innerHTML += '单击动作发生<br/>';
}
</script> </head>
<body>
<div id="div1" onmousedown="mouseDownFun();" onmouseup="mouseUpFun();" onclick="clickFun();"
style="background: #CCC;border: 3px solid #999;width: 200px;height: 200px;padding: 10px"></div>
<input style="margin-top:10px;" type="button" onclick="document.getElementById('div1').innerHTML='';" value="清除信息">
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file10.html";
driver.get(url);
WebElement div = driver.findElement(By.xpath("//div[@id='div1']"));
Actions action = new Actions(driver);
action.clickAndHold(div).perform();//单击不释放
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
action.release(div).perform();//释放
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十四、查看页面元素的属性
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void opentest() {
driver.get(url);
WebElement input = driver.findElement(By.id("kw"));
input.sendKeys("百度一下");
//
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text = input.getAttribute("value");
Assert.assertEquals(text, "百度一下");
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十五、获取页面元素的CSS属性值
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void opentest() {
driver.get(url);
WebElement input = driver.findElement(By.id("kw"));
String inputWidth = input.getCssValue("width");
System.out.println(inputWidth);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十六、隐匿等待
被测试网页的网址:
http://www.baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.baidu.com";
@Test
public void opentest() {
driver.get(url);
//隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
try {
WebElement input = driver.findElement(By.id("kw"));
WebElement button = driver.findElement(By.id("su"));
input.sendKeys("三生三世");
button.click();
} catch (NoSuchElementException e) {
// TODO: handle exception
Assert.fail("没有找到元素");
e.printStackTrace();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十七、常用的显式等待
显式等待比隐式等待更节约测试脚本执行的时间,推荐尽量使用显式等待方式来判断页面元素是否存在。使用ExpectedConditions类中自带的方法,可以进行显式等待的判断。显式等待可以自定义等待的条件,用于更加复杂的页面元素状态判断。常用的显式等待条件如下表所示:
等待的条件 | 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>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<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实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file11.html";
driver.get(url);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.titleContains("水果"));// //
WebElement select = driver.findElement(By.xpath("//option[@id='peach']"));
wait.until(ExpectedConditions.elementToBeSelected(select)); //
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='checkbox']"))); //
WebElement p = driver.findElement(By.xpath("//p"));
wait.until(ExpectedConditions.textToBePresentInElement(p, "爱吃的水果")); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十八、自定义显式等待
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<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实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file11.html";
driver.get(url);
try {
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement textInputbox = wait.until(new ExpectedCondition<WebElement>() { @Override
public WebElement apply(WebDriver d) {
// TODO Auto-generated method stub
return d.findElement(By.xpath("//*[@type='text']"));
}
});
Assert.assertEquals("今年夏天西瓜相当甜!", textInputbox.getAttribute("value")); //
Boolean containTextFlag = wait.until(new ExpectedCondition<Boolean>() { @Override
public Boolean apply(WebDriver d) {
// TODO Auto-generated method stub
return d.findElement(By.xpath("//p")).getText().contains("爱吃");
}
});
Assert.assertTrue(containTextFlag); //
Boolean inputTextVisibleFlag = wait.until(new ExpectedCondition<Boolean>() { @Override
public Boolean apply(WebDriver d) {
// TODO Auto-generated method stub
return d.findElement(By.xpath("//*[@type='text']")).isDisplayed();
}
});
Assert.assertTrue(inputTextVisibleFlag);
} catch (NoSuchElementException e) {
// TODO: handle exception
} try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
二十九、判断页面元素是否存在
被测试网页的网址:
http://www.sogou.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://www.sogou.com"; private Boolean IsElementPresent(By by){
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
// TODO: handle exception
return false;
}
}
@Test
public void opentest() {
driver.get(url);
if(IsElementPresent(By.id("query"))){
WebElement searchInputbox = driver.findElement(By.id("query"));
if(searchInputbox.isEnabled()==true){
searchInputbox.sendKeys("sogou找到了");
}
}else{
Assert.fail("元素未被找到");
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
三十、使用Title属性识别和操作新弹出的浏览器窗口
被测试网页的HTML源码:
<html>
<head>
<meta charset="UTF-8" content="text/html">
<title>你喜欢的水果</title>
</head>
<body>
<p id="p1">你爱吃的水果么?</p>
<br><br>
<a href="http://www.sogou.com" target="_blank">sogou搜索</a>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver; @Test
public void opentest() {
File file = new File("");
String url = file.getAbsolutePath() + "/html/" + "file12.html";
driver.get(url);
String parentWindowHandle = driver.getWindowHandle();
WebElement sogouLink = driver.findElement(By.xpath("//a"));
sogouLink.click();
java.util.Set<String> allWindowsHandles = driver.getWindowHandles();
if(!allWindowsHandles.isEmpty()){
for(String windowHandle:allWindowsHandles){
try {
if(driver.switchTo().window(windowHandle).getTitle().equals("搜狗搜索引擎 - 上网从搜狗开始")){
driver.findElement(By.id("query")).sendKeys("sogou");
}
} catch (NoSuchWindowException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
driver.switchTo().window(parentWindowHandle);
Assert.assertEquals(driver.getTitle(), "你喜欢的水果");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} @BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
WebDriver API 实例详解(三)的更多相关文章
- WebDriver API 实例详解(四)
三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...
- WebDriver API 实例详解(二)
十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...
- WebDriver API 实例详解(一)
一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(三)DOCTYPE和字符集
在2.1.2节中通过新老DOCTYPE的对比,读者可以清晰地看到HTML 5在精简旧有结构上做出的努力.DOCTYPE在出现之初主要用于XML中,用作描述XML允许使用的元素.属性和排列方式.起初HT ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- 【python3+request】python3+requests接口自动化测试框架实例详解教程
转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...
- Vue 实例详解与生命周期
Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...
- python+requests接口自动化测试框架实例详解
python+requests接口自动化测试框架实例详解 转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
随机推荐
- HE算法与Scaler算法
HE算法:图像直方图均衡化 Scaler算法:图像缩放 基于matab的scaler实现_图文_百度文库 https://wenku.baidu.com/view/016f5e4002768e9951 ...
- day08<面向对象+>
面向对象(代码块的概述和分类) 面向对象(代码块的面试题) 面向对象(继承案例演示) 面向对象(继承的好处和弊端) 面向对象(Java中类的继承特点) 面向对象(继承的注意事项和什么时候使用继承) 面 ...
- Extjs学习笔记--(二)
1.配置实用Extjs <link href="Extjs/resources/css/ext-all.css" rel="stylesheet" /&g ...
- docker images 详解
docker images 用于查看本地已下载的镜像 [root@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ce ...
- select下拉框不能赋值
前言: 需要用到类似于下面的下拉选择框,按照官方文档写,始终实现不了下拉框赋上值的情况. 过程: 认认真真的看了好几遍文档,但是还没找到原因,不过还是应该感谢自己加入的vue-admin的微信群,大家 ...
- php学习十四:抽象,接口和多态
多态为面向对象编程的精华所在,js等面向过程的语言虽然可以模拟面向对象,但是毕竟模仿的永远比不上真的,所以了解而且会使用面向对象的多态是必不可少的 在了解多态之前,我们必须要了解接口,但是接口又是在抽 ...
- iPad UIPopoverController弹出窗口的位置和坐标
本文转载至:http://blog.csdn.net/chang6520/article/details/7921181 TodoViewController *contentViewControll ...
- c++11 处理时间和日期
c++11提供了日期时间相关的库 chrono,通过chrono库可以很方便的处理日期和时间. 1. 记录时间长度的duration template<class Rep, class Peri ...
- Angular基础---->AngularJS的使用(一)
AngularJS主要用于构建单页面的Web应用.它通过增加开发人员和常见Web应用开发任务之间的抽象级别,使构建交互式的现代Web应用变得更加简单.今天,我们就开始Angular环境的搭建和第一个实 ...
- webpack之跨域
前后端分离开发中,本地前端开发调用接口会有跨域问题,一般有以下几种解决方法: 直接启动服务端项目,再将项目中的资源url指向到前端服务中的静态资源地址,好处在于因为始终在服务端的环境中进行资源调试,不 ...