二十一、模拟鼠标右键事件

被测试网页的网址:

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 实例详解(三)的更多相关文章

  1. WebDriver API 实例详解(四)

    三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...

  2. WebDriver API 实例详解(二)

    十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...

  3. WebDriver API 实例详解(一)

    一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...

  4. 免费的HTML5连载来了《HTML5网页开发实例详解》连载(三)DOCTYPE和字符集

    在2.1.2节中通过新老DOCTYPE的对比,读者可以清晰地看到HTML 5在精简旧有结构上做出的努力.DOCTYPE在出现之初主要用于XML中,用作描述XML允许使用的元素.属性和排列方式.起初HT ...

  5. Entity Framework实例详解

    Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...

  6. 【python3+request】python3+requests接口自动化测试框架实例详解教程

    转自:https://my.oschina.net/u/3041656/blog/820023 [python3+request]python3+requests接口自动化测试框架实例详解教程 前段时 ...

  7. Vue 实例详解与生命周期

    Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...

  8. python+requests接口自动化测试框架实例详解

    python+requests接口自动化测试框架实例详解   转自https://my.oschina.net/u/3041656/blog/820023 摘要: python + requests实 ...

  9. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

随机推荐

  1. 在你开发完brew应用之后 ,你又如果将brew应用由编译成可以部署到brew真机上的程序包呢

    参考自:http://blog.csdn.net/feimor/article/details/6239281 一.准备工作(安装工具) 先安装Visual C++ 6.0,再安装BREW SDK v ...

  2. gevent动态随时添加任务

    关于爬虫,有scrapy框架,也有requests加协程 协程 进程的方法. 相关的包很多,比如threading .threadpool.multiprocessing,还有threadpoolex ...

  3. 基于pyteseract google ocr的图形验证码识别

    先灰化图片,把图片二值化,利用pytesseract包的pytesseract.image_to_string转换出文字.

  4. Extjs不错的博客

    http://www.cnblogs.com/fangsui/category/372751.html http://www.cnblogs.com/WangJinYang/tag/EXT.NET/ ...

  5. laravel 社会化(联合)登录扩展包(QQ、微信、微博等)

    laravel的官方包只支付国外网站的联合登录:http://laravelacademy.org/post/6288.html 国内用户的话,可以用这个:https://github.com/ove ...

  6. union和union all的并集(相加)区别

    Union因为要进行重复值扫描,所以效率低.如果合并没有刻意要删除重复行,那么就使用Union All  两个要联合的SQL语句 字段个数必须一样,而且字段类型要“相容”(一致): 如果我们需要将两个 ...

  7. Python 入门(八)切片

    对list进行切片 取一个list的部分元素是非常常见的操作.比如,一个list如下: >>> L = ['Adam', 'Lisa', 'Bart', 'Paul'] 取前3个元素 ...

  8. hex()

    hex() 用于将十进制数字转换成十六进制 In [1]: hex(10) Out[1]: '0xa' In [2]: hex(11) Out[2]: '0xb'

  9. C语言中的文本流与二进制流的区别

    近期看到了文本流和二进制流的区别,书上讲的比较含糊,理解不透彻,于是细细琢磨了下,把心得跟大家分享一下: 一.首先回答,什么是文件,流 一个文件通常就是磁盘上的一段命名的存储区.比如 stdio.h ...

  10. JDK1.8在LINUX下安装步骤

    JDK1.8在LINUX下安装步骤: 在/usr/lib/目录下新建jvm文件夹,如果已有jvm文件夹,则将之前的JDK版本删除,即在jvm目录下执行命令:rm –rf * 将JDK文件jdk-8u4 ...