WebDriver API 实例详解(二)
十一、双击某个元素
被测试网页的html源码:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<input type="text" id="inputBox"
ondblclick="javascript:this.style.background='red'">请双击</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.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file3.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement inputBox = driver.findElement(By.id("inputBox"));
//声明Action对象
Actions builder = new Actions(driver);
//双击
builder.doubleClick(inputBox).build().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">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</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.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//根据Index,下标从0开始
droplist.selectByIndex(3);
Assert.assertEquals("猕猴桃", droplist.getFirstSelectedOption().getText());
//根据value属性值
droplist.selectByValue("shanzha");
Assert.assertEquals("山楂", droplist.getFirstSelectedOption().getText());
//通过显示的文字
droplist.selectByVisibleText("荔枝");
Assert.assertEquals("荔枝", droplist.getFirstSelectedOption().getText());
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">
</head>
<body>
<select name="fruit" size="1">
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; 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.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file4.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//
List<String> exp_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"}));
List<String> act_option = new ArrayList<String>();
for(WebElement option:droplist.getOptions()){
act_option.add(option.getText());
}
//断言
Assert.assertEquals(exp_options.toArray(), act_option.toArray());
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">
</head>
<body>
<select name="fruit" size="6" multiple=true>
<option id="peach" value="taozi">桃子</option>
<option id="watermelon" value="xigua">西瓜</option>
<option id="orange" value="juzi">橘子</option>
<option id="kiwifruit" value="mihoutao">猕猴桃</option>
<option id="maybush" value="shanzha">山楂</option>
<option id="litchi" value="lizhi">荔枝</option>
</select>
</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.Select;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file5.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement fruit = driver.findElement(By.name("fruit"));
Select droplist = new Select(fruit);
//选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子");
droplist.deselectAll();//取消全部选择 //再次选择
droplist.selectByIndex(3);
droplist.selectByValue("shanzha");
droplist.selectByVisibleText("桃子"); //逐个取消
droplist.deselectByIndex(3);
droplist.deselectByValue("shanzha");
droplist.deselectByVisibleText("桃子"); 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">
</head>
<body>
<form>
<input type="radio" name="fruit" value="berry">草莓</input>
<br/>
<input type="radio" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="radio" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.List; 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;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file6.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement radioOption = driver.findElement(By.xpath("//input[@value='berry']"));
if(!radioOption.isSelected()){
radioOption.click();
}
Assert.assertTrue(radioOption.isSelected());
//
List<WebElement> fruits = driver.findElements(By.name("fruit"));
for(WebElement fruit:fruits){
if(fruit.getAttribute("value").equals("watermelon")){
if(!fruit.isSelected()){
fruit.click();
}
Assert.assertTrue(fruit.isSelected());
break;
}
}
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">
</head>
<body>
<form>
<input type="checkbox" name="fruit" value="berry">草莓</input>
<br/>
<input type="checkbox" name="fruit" value="watermelon">西瓜</input>
<br/>
<input type="checkbox" name="fruit" value="orange">橙子</input>
</form>
</body>
</html>
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File;
import java.util.List; 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;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file7.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement orangcheckbox = driver.findElement(By.xpath("//input[@value='orange']"));
if(!orangcheckbox.isSelected()){
orangcheckbox.click();
}
Assert.assertTrue(orangcheckbox.isSelected());
//
List<WebElement> checkboxs = driver.findElements(By.name("fruit"));
for(WebElement checkbox:checkboxs){
checkbox.click();
}
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">
</head>
<body>
<p>《三生三世十里桃花》这个电影真的很棒!</p>
<p>主要是杨洋不错!</p>
</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.testng.Assert;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
@Test
public void opentest() {
File file = new File("");
System.out.println(file.getAbsolutePath());
String url = file.getAbsolutePath() + "/html/" + "file8.html";
driver.get(url);
System.out.println(driver.getCurrentUrl());
//
WebElement text = driver.findElement(By.xpath("//p[1]"));
String contentText = text.getText();
Assert.assertEquals("《三生三世十里桃花》这个电影真的很棒!", contentText);
Assert.assertTrue(contentText.contains("三生三世"));
Assert.assertTrue(contentText.startsWith("《三"));
Assert.assertTrue(contentText.endsWith("很棒!"));
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();
} }
十八、执行javaScript脚本
被测试网页的网址:
http://baidu.com
Java语言版本的API实例代码:
package test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
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);
JavascriptExecutor js = (JavascriptExecutor) driver;
String title = (String) js.executeScript("return document.title");
Assert.assertEquals("百度一下,你就知道", title); try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String searchText = (String) js.executeScript("var button= document.getElementById('su');return button.value");
System.out.println(searchText);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "F:\\selenium\\chromedriver.exe");
driver = new ChromeDriver();
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
十九、拖曳页面元素
被测试网页的网址:
http://jqueryui.com/resources/demos/draggable/scroll.html
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.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterMethod; public class ChormeOpen {
WebDriver driver;
String url = "http://jqueryui.com/resources/demos/draggable/scroll.html"; @Test
public void opentest() {
driver.get(url);
WebElement draggable = driver.findElement(By.id("draggable"));
//向下拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
}
//向右拖动10个像素、共拖动5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 10, 0).build().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.Keys;
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.baidu.com"; @Test
public void opentest() {
driver.get(url);
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL);//按下Ctrl键
action.keyDown(Keys.SHIFT);//按下Shift键
action.keyDown(Keys.ALT);//按下Alt键
action.keyUp(Keys.CONTROL);//释放Ctrl键
action.keyUp(Keys.SHIFT);//释放Shift键
action.keyUp(Keys.ALT);//释放ALT键
//模拟键盘在搜索输入框输入大写的字符“ABCD”
action.keyDown(Keys.SHIFT).sendKeys("abcd").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();
} }
WebDriver API 实例详解(二)的更多相关文章
- WebDriver API 实例详解(四)
三十一.使用页面的文字内容识别和处理新弹出的浏览器窗口 被测试网页的HTML源码: <html> <head> <meta charset="UTF-8&quo ...
- WebDriver API 实例详解(三)
二十一.模拟鼠标右键事件 被测试网页的网址: http://www.sogou.com Java语言版本的API实例代码: package test; import org.testng.annota ...
- WebDriver API 实例详解(一)
一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...
- JavaScript学习笔记-实例详解-类(二)
实例详解-类(二) //===给Object.prototype添加只读\不可枚举\不可配置的属性objectId(function(){ Object.defineProperty(Object ...
- 免费的HTML5连载来了《HTML5网页开发实例详解》连载(二)
最近新浪.百度.腾讯.京东.大众点评.淘宝等流行的网站都加大了招聘HTML5的力度,HTML5开发人员成了抢手货,本次连载的是由大众点评前端工程师和一淘网前端工程师基情奉献的<HTML5网页开发 ...
- Entity Framework实例详解
Entity Framework Code First的默认行为是使用一系列约定将POCO类映射到表.然而,有时候,不能也不想遵循这些约定,那就需要重写它们.重写默认约定有两种方式:Data Anno ...
- 【eclipse插件开发实战】 Eclipse插件开发6——eclipse在线翻译插件Translator开发实例详解
Eclipse插件开发6--eclipse在线翻译插件Translator开发实例详解 在上一篇文章中讲到了一个简单的eclipse插件开发实例,主要是对插件工程的基本创建步骤进行了讲解,这篇文章当中 ...
- (转载)实例详解Android快速开发工具类总结
实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...
- Cocos2d-x 3.X手游开发实例详解
Cocos2d-x 3.X手游开发实例详解(最新最简Cocos2d-x手机游戏开发学习方法,以热门游戏2048.卡牌为例,完整再现手游的开发过程,实例丰富,代码完备,Cocos2d-x作者之一林顺和泰 ...
随机推荐
- Java类的设计----关键字super
关键字super 在Java类中使用super来引用父类的成分 super可用于访问父类中定义的属性 super可用于调用父类中定义的成员方法 super可用于在子类构造方法中调用父类的构造方法 su ...
- GIS-ArcGIS 数据库备份还原
Create directory sdebak as 'E:\10_DataFile'; alter system set deferred_segment_creation=false; ALTER ...
- VC++第三方库配置-OpenSpirit 4.2.0 二次开发
在VS中右击项目,点击属性 1.配置属性--常规--输出目录:Windows\VS2010\debug\ 2.配置属性--常规--中间目录:Windows\VS2010\debug\ 3.配置属性-- ...
- MySQL性能优化(四)-- MySQL explain详解
前言 MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 一.格式 explain + ...
- Linux mysqladmin 命令
mysqladmin命令可以用来设置或修改 MySQL 密码,常见用法如下: [root@localhost ~]$ mysqladmin -uroot password 'newPass' # 在无 ...
- oracle非归档模式下的冷备份和恢复
查看归档的相关信息 SQL> archive log list数据库日志模式 非存档模式自动存档 禁用存档终点 USE_DB ...
- timerWithTimeInterval 方法详用
1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...
- iOS性能调优系列(全)
总结: 三类工具 基础工具 (NSLog的方式记录运行时间.) 性能工具.检测各个部分的性能表现,找出性能瓶颈 内存工具.检查内存正确性和内存使用效率 性能工具: 可以衡量CPU的使用,时间的消耗,电 ...
- JS AJAX传递List数组到后台(对象)
今天在写代码的时候,碰到的问题,百度了一下,发现原来AJAX传递List数据是可以的,之前还一直用JSON序列化(new Array()数组设置)进行传值的. var _list = {}; //等价 ...
- LeetCode——Largest Number
Description: Given a list of non negative integers, arrange them such that they form the largest num ...