5.6 WebDriver API实例讲解(16-30)
16.操作单选框
被测试的网页为Demo1。
Java语言版本的API实例代码:
public static void operateRadio(){
driver.get("file:///D:/%E6%95%99%E5%AD%A6/2015-2016-2/%E8%AF%BE%E4%BB%B6/w9/SeleniumDemo/files/Demo1/demo.html");
driver.manage().window().maximize();
WebElement radioOption=driver.findElement(By.xpath("//input[@value='Volvo']"));
if(!radioOption.isSelected())
radioOption.click();
sleep();
List<WebElement> cars=driver.findElements(By.name("identity"));
for(WebElement car:cars)
if(car.getAttribute("value").equals("Audi")){
if(!car.isSelected())
car.click();
break;
}
}
17.操作复选框
被测试的网页为Demo1。
Java语言版本的API实例代码:
public static void operateCheckBox(){
driver.get("file:///D:/%E6%95%99%E5%AD%A6/2015-2016-2/%E8%AF%BE%E4%BB%B6/w9/SeleniumDemo/files/Demo1/demo.html");
driver.manage().window().maximize();
sleep();
WebElement element=driver.findElement(By.xpath(".//*[@id='checkbox']/input[3]"));
if(!element.isSelected())
element.click();
sleep();
if(element.isSelected())
element.click();
sleep();
System.out.println("end1");
List<WebElement> checkboxs=driver.findElements(By.xpath(".//*[@id='checkbox']/input"));
for(WebElement checkbox:checkboxs)
checkbox.click();
System.out.println("end2");
}
18.杀掉Windows的浏览器进程
测试前准备条件:
(1)打开一个Firefox浏览器
(2)打开一个IE浏览器
(3)打开一个Chrome浏览器
Java语言版本的API实例代码:
public static void operateWindowsProcess(){
WindowsUtils.KillByName("firefox.exe");
sleep();
WindowsUtils.KillByName("iexplore.exe");
sleep();
WindowsUtils.KillByName("chrome.exe");
sleep();
}
19.将当前浏览器的窗口截屏
public static void captureScreenInCurrentWindow(){
driver.get("http://www.sogou.com");
File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(scrFile, new File("d:\\test.png"));
}catch(IOException e){
e.printStackTrace();
}
}
20.执行JavaScript脚本
被测试的网页为http://www.sogou.com。
Java语言版本的API实例代码:
public static void executeJavaScript(){
driver.get("http://www.sogou.com");
//声明一个JavaScript执行器对象
JavascriptExecutor js=(JavascriptExecutor)driver;
//调用执行器对象的executeScript方法来执行JavaScript脚本返回当前浏览器窗口的 Title值
String title=(String)js.executeScript("return document.title");
System.out.println(title);
}
21.拖拽页面元素
被测试网页的网址:http://jqueryui.com/resources/demos/draggable/scroll.html
此页面的3个方框元素可以被拖动。
Java语言版本的API实例代码:
public static void dragPageElement(){
driver.get("http://jqueryui.com/resources/demos/draggable/scroll.html");
WebElement draggable=driver.findElement(By.id("draggable"));
//向下拖动10个像素,共拖动5次
for(int i=;i<;i++){
//10表示元素的纵坐标向下移动10个像素,0表示元素的横坐标不变
new Actions(driver).dragAndDropBy(draggable, , ).build().perform();
}
sleep();
//向右拖动10个像素,共拖动5次
for(int i=;i<;i++){
new Actions(driver).dragAndDropBy(draggable, , ).build().perform();
}
}
22.模拟键盘的操作
被测试网页的网址:http://www.sogou.com。
Java语言版本的API实例代码:
public static void clickKeys(){
driver.get("http://www.sogou.com/");
WebElement element=driver.findElement(By.id("query"));
element.click();
Actions action=new Actions(driver);
action.keyDown(Keys.CONTROL); //按下Ctrl键
action.keyDown(Keys.SHIFT);
action.keyDown(Keys.ALT);
action.keyDown(Keys.CONTROL); //按下Ctrl键
action.keyDown(Keys.SHIFT);
action.keyDown(Keys.ALT);
action.keyDown(Keys.SHIFT).sendKeys("abcdefg").perform();
}
23.模拟鼠标右键事件
public static void rightClickMOuse(){
driver.get("http://www.sogou.com/");
Actions action=new Actions(driver);
action.contextClick(driver.findElement(By.id("query"))).perform();
}
24.在指定元素上方进行鼠标悬浮
public static void roverOnElement(){
driver.get("file:///D:/%E6%95%99%E5%AD%A6/2015-2016-2/%E8%AF%BE%E4%BB%B6/w9/SeleniumDemo/files/Demo1/demo.html");
driver.manage().window().maximize();
sleep();
WebElement element=driver.findElement(By.xpath(".//*[@id='action']/input"));
Actions action=new Actions(driver);
action.moveToElement(element).perform();
}
25.在指定元素上进行鼠标单击左键和释放的操作
action.clickAndHold(element).perform();
action.release(element).perform();
26.查看页面元素的属性
element.getAttribute("value");
27.获取页面元素的CSS属性值
element.getCssValue("width");
28.隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
另外一种,笨方法等待,使当前线程进入等待,Thread.sleep();这种等待属于死等,很容易让线程挂掉,使程序抛异常,所以要慎用此方法。
29.常用的显式等待
显式等待比隐式等待更节约测试脚本执行的时间,推荐尽量使用显式等待方式来判断页面元素是否存在。使用ExpectedConditions类中自带的方法,可以进行显式等待的判断。显式等待可以自定义等待的条件,用于更加复杂的页面元素状态判断。常用的显式等待条件如下表所示。
| 等待的条件 | WebDriver方法 |
| 页面元素是否在页面上可用和可被单击 | elementToBeClickable(By locator) |
| 页面元素处于被选中状态 | elementToBeSelected(WebElement element) |
| 页面元素在页面中存在 | presenceOfElementLocated(By locator) |
| 在页面元素中是否包含特定的文本 | textToBePresentInElement(By locator) |
| 页面元素值 | textToBePresentInElementValue(By locator, java.lang.String text) |
| 标题 | titleContains(java.lang.String title) |
只有满足显式等待的条件要求,测试代码才会继续向后执行后续的测试逻辑。如果超过设定的最大显式等待时间阈值,则测试程序会抛出异常,测试用例被认为执行失败。
public static void testExplictWait1(){
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
sleep();
WebElement element=driver.findElement(By.id("kw"));
element.sendKeys("elon musk");
element=driver.findElement(By.id("su"));
element.click();
//声明一个WebDriverWait对象,设定触发条件的最长等待时间为10秒
WebDriverWait wait=new WebDriverWait(driver,);
wait.until(ExpectedConditions.titleContains("elon"));
System.out.println("end");
}
30.自定义的显式等待
public static void testExplictWait2()
{
driver.get("file:///D:/%E6%95%99%E5%AD%A6/2015-2016-2/%E8%AF%BE%E4%BB%B6/w9/SeleniumDemo/files/Demo1/demo.html");
driver.manage().window().maximize();
sleep(); WebElement element=driver.findElement(By.className("wait"));
element.click();
WebElement redDiv=(new WebDriverWait(driver,))
.until(new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver d){
return d.findElement(By.className("red"));
}
});
System.out.println(redDiv.getText());
}
5.6 WebDriver API实例讲解(16-30)的更多相关文章
- 5.7 WebDriver API实例讲解
本节主要详细描述WebDriver的常用API使用方法. 1.访问某网页地址 被测试网页的网址:http://www.sogou.com. Java语言版本的API实例代码: 方法1: public ...
- 5.6 WebDriver API实例讲解(31-35)
31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...
- 5.6 WebDriver API实例讲解(31-40)
31.判断页面元素是否存在 public static void testElementExist(){ driver.get("http://www.sogou.com"); t ...
- 5.6 WebDriver API实例讲解(41-50)
41.操作Web页面的滚动条 (1)滑动页面的滚动条到页面的最下面. (2)滑动页面的滚动条到页面的某个元素. (3)滑动页面的滚动条向下移动某个数量的像素. package apiSample; i ...
- 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 实例详解(二)
十一.双击某个元素 被测试网页的html源码: <html> <head> <meta charset="UTF-8"> </head&g ...
- WebDriver API 实例详解(一)
一.访问某网页地址 被测试网页的网址: http://www.baidu.com Java语言版本的API实例代码: 方法1: package test; import org.testng.anno ...
- python+selenium自动化软件测试(第2章):WebDriver API
2.1 操作元素基本方法 前言前面已经把环境搭建好了,从这篇开始,正式学习selenium的webdriver框架.我们平常说的 selenium自动化,其实它并不是类似于QTP之类的有GUI界面的可 ...
随机推荐
- c语言中函数调用的本质从汇编角度分析
今天下午写篇博客吧,分析分析c语言中函数调用的本质,首先我们知道c语言中函数的本质就是一段代码,但是给这段代码起了一个名字,这个名字就是他的的这段代码的开始地址 这也是函数名的本质,其实也就是汇编中的 ...
- 3.linux man手册
(12) man作用:查询man手册,获得帮助信息man 1 ls 1表示查询的是linux命令man 2 xxx 2表示查询的是linux apiman 3 xxx 3表示查询的是C库函数注意:在m ...
- Java 常用排序算法/程序员必须掌握的 8大排序算法
Java 常用排序算法/程序员必须掌握的 8大排序算法 分类: 1)插入排序(直接插入排序.希尔排序) 2)交换排序(冒泡排序.快速排序) 3)选择排序(直接选择排序.堆排序) 4)归并排序 5)分配 ...
- 传递给系统调用的数据区域太小。 (异常来自 HRESULT:0x8007007A)
在做结构体向字节数组转换的时候,常遇到"传递给系统调用的数据区域太小"的错误,究其原因是因为英文与汉字的编码方式不同,一个汉字等于两个字节,而一个英文字母等于1个字节.所以,对于如 ...
- MSM8974 fastboot烧写软件
fastboot烧写是在aboot阶段做的,所以空板没有完整烧写aboot及其boot sequence前的image是没法使用fastboot的.在手机开机状态下,执行: adb re ...
- HCE基础知识
HCE基础知识普及 http://www.cebnet.com.cn2014-09-18 10:32来源:中钞研究院 字号: NFC技术发展 NFC(Near Field Communicat ...
- Ajax发送POST请求的心路历程
好多年前就在项目中用ajax实现了页面部分数据的发送,当时用的是GET方法. 这次用POST方法实现同样的功能,竟然花费了不少的时间! ① 关于JQuery ajax的配置项说明 url : 请求的u ...
- Limiting To Select Only 5 Check Boxes Out Of Ten In Oracle Forms
Suppose you want to give an option to user to select only 5 check boxes from given any number of che ...
- HDU 5810 Balls and Boxes(盒子与球)
Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
- win8和ubuntu双系统安装
做了一个windows和Ubuntu双系统,参考了一些文章.网上资料不少,我就不重复了. 虽然没什么难度,但是有些细节在装的时候需要注意.不然造成资料丢失,系统崩溃,你就得不偿失,需要折腾花费较长的时 ...