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

被测试网页的网址:

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. Java输入输出流(2)

    6. Java.IO流类库 1. io流的四个基本类 java.io包中包括了流式I/O所须要的全部类. 在java.io包中有四个基本类:InputStream.OutputStream及Reade ...

  2. 【渗透测试学习平台】 web for pentester -5.代码执行

    Example 1 http://192.168.106.154/codeexec/example1.php?name=".system('uname -a');// Example 2 h ...

  3. 转的:burp suite小例子

    Web安全测试时经常会遇到一些蹩脚的注射点,而因各种原因利用注射又无法获取网站管理账号或拥有网站管理权限却迟迟不能upload一个shell的时候,可能会权衡一下web权限与数据库信息,哪个是我们所需 ...

  4. ORA-20000的解决过程

    今天在用impdp导数据时出现卡住的现象,开始我以为是数据量过大导致的,过了两个小时还是卡住,看警告日志得到下面的错误信息: GATHER_STATS_JOB encountered errors.  ...

  5. 关于新塘 M0 M4添加库文件的说明

  6. COM组件技术名称解释

    GUID:全局唯一标识. CLSID 或 ProgID :唯一地表示一个组件服务程序,那么根据这些ID,就可以加载运行组件,并为客户端程序提供服务了. IID :唯一的表示接口ID. COM 组件是运 ...

  7. 在SQL数据库中怎么去掉某一列的中的一写特殊字符

    用REPLACE函数,把空格替换成 ''. 例:去除 表table 中 col 列的空字符去除空格符:update table set col = REPLACE(col,' ','') 还有tab制 ...

  8. 【MySQL】查询时强制区分大小写的方法

    MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名重复,又会浪费很多资源.再者,李逵.李鬼的多起来,侦辨起来很困难.要做到这一点,要么在建表时,明确大小写敏感(字段明确大小写敏感) sql ...

  9. axios请求本地json

    在vux的项目中 1,首先,json文件的位置: 原因: 访问服务器文件,应该把 json文件放在最外层的static文件夹,这个文件夹是vue-cli内置服务器向外暴露的静态文件夹   2,一定要用 ...

  10. 【黑金ZYNQ7000系列原创视频教程】05.FPGA和ARM的初次结合——LED实验

    黑金论坛地址: http://www.heijin.org/forum.php?mod=viewthread&tid=36639&extra=page%3D1 爱奇艺地址: http: ...