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

被测试网页的网址:

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. mac 开发环境安装使用 记录

    常用命令 1 查找文件位置: $locate php-fpm 软件变动的话更新locate本地库,可能比较耗时. $sudo /usr/libexec/locate.updatedb 2 查找运行文件 ...

  2. brew 中的时间格式转换

    char * pACNowStr = NULL; JulianType jtNow; ISHELL_GetJulianDate(pIShell, , &jtNow); pACNowStr = ...

  3. 分布式缓存设计:一致性Hash算法

    缓存作为数据库前的一道屏障,它的可用性与缓存命中率都会直接影响到数据库,所以除了配置主从保证高可用之外还需要设计分布式缓存来扩充缓存的容量,将数据分布在多台机器上如果有一台不可用了对整体影响也比较小. ...

  4. swift学习笔记之--类的计算属性

    1,Swift中类可以使用计算属性,即使用get和set来间接获取/改变其他属性的值,代码如下 class Calcuator{ ; ; var sum:Int{ get{ return a + b ...

  5. GIS-001-gdal软件下载地址

    http://www.gisinternals.com/ http://download.gisinternals.com/sdk/downloads/release-1600-x64-gdal-1- ...

  6. linux系统socket通信编程详解函数

    linux socket编程之TCP与UDP   TCP与UDP区别 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当客户和服务器彼此交换数据前,必须先在双方之间建立一个TCP连接,之 ...

  7. vsftpd配置教程

    原文:http://www.cnblogs.com/hhuai/archive/2011/02/12/1952647.html 可能会遇到的问题: http://www.cnblogs.com/wea ...

  8. VC++ 6.0创建MFC工程时的初级备要点(二)

        在看到一个MFC工程时,可能根据上一篇文章已经知道了哪些是重要的,那些是在创建工程的时候已经被初始化的!!下面介绍一下在看代码的过程的策略!!这里假设创建的基于多对话框的MFC工程名为test ...

  9. sencha touch 组件选择器getCmp和ComponentQuery.query()的效率解析

    昨天无意中在网上看到一篇讲解sencha touch组件选择器的文章,名为 Sencha touch 2通过Ext.ComponentQuery.query查找组件. 里面对组件选择器的效率讲解完全反 ...

  10. PHP后门的eval类和system类 函数到底有哪些区别

    一. 一直以来对PHP的eval这一类函数和system这一类存在疑惑的地方,今天彻底研究了一下,写查PHP一句话的时候可以更有把握一些.其实都是一些满基础的知识,大佬别喷.干安全的基础很重要. 二. ...