WebDriverAPI(5)
将当前浏览器截屏
测试网址
http://www.baidu.com
Java语言版本实例
@Test
public void captureScreenInCurrentWindows() {
driver.manage().window().maximize();
driver.get(url);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile,new File("D:\\a.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
杀掉Windows的浏览器进程
测试前打开3个浏览器
Java语言实例代码:
@Test
public void operateWindowsProcess(){
WindowsUtils.tryToKillByName("firefox.exe");
WindowsUtils.tryToKillByName("iexplore.exe");
WindowsUtils.tryToKillByName("chrome.exe");
}
将当前浏览器截屏
测试网址
http://www.baidu.com
Java语言版本实例
@Test
public void isElementTextPresent() {
driver.manage().window().maximize();
driver.get(url);
WebElement text = driver.findElement(By.xpath("//*[@id='u1']/a[1]"));
String contentText = text.getText();
//判断页面文字是否匹配
Assert.assertEquals("新闻", contentText);
//判断页面文字是否包含
Assert.assertTrue(contentText.contains("新"));
//判断页面文字开头是否匹配
Assert.assertTrue(contentText.startsWith("新"));
//判断页面文字结尾文字是否匹配
Assert.assertTrue(contentText.endsWith("闻"));
}
执行avaScript脚本
测试网址
http://www.baidu.com
Java语言版本实例
@Test
public void executeJavaScript() {
driver.manage().window().maximize();
driver.get(url);
//声明一个JavaScript对象
JavascriptExecutor js = (JavascriptExecutor) driver;
//调用executeScript方法执行return document.title
String title = (String) js.executeScript("return document.title");
Assert.assertEquals("百度一下,你就知道", title);
//获取按钮对象返回按钮文字
String serachButtonText = (String) js.executeScript("var button = document.getElementById('su');return button.value");
System.out.println(serachButtonText);
}
拖拽页面元素
测试网址
https://jqueryui.com/draggable/
Java语言版本实例
@Test
public void dragePageElement() throws InterruptedException {
driver.manage().window().maximize();
driver.navigate().to(url);
//找到需配置的对象
Thread.sleep(6000);
//切换至第一个iframe
driver.switchTo().frame(0);
WebElement draggable = driver.findElement(By.xpath("//*[@id='draggable']"));
//向下拖动10个像素,共5次
for(int i=0;i<5;i++){
//0表示坐标不变
new Actions(driver).dragAndDropBy(draggable, 0, 10).build().perform();
}
Thread.sleep(2000);
//向右拖动10个像素,共5次
for(int i=0;i<5;i++){
new Actions(driver).dragAndDropBy(draggable, 10, 0).build().perform();
}
Thread.sleep(2000);
}
模拟键盘操作
测试网址
http://www.baidu.com
Java语言版本实例
@Test
public void clickKeys() {
driver.manage().window().maximize();
driver.navigate().to(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
//模拟输入大写字符
action.keyDown(Keys.SHIFT).sendKeys("abcdefg").perform();
}
模拟鼠标右键操作
测试网址
http://www.baidu.com
Java语言版本实例
@Test
public void rightClickMouse() {
driver.manage().window().maximize();
driver.get(url);
Actions action = new Actions(driver);
//模拟鼠标右键事件
action.contextClick(driver.findElement(By.id("kw"))).perform();
}
WebDriverAPI(5)的更多相关文章
- WebDriverAPI(7)
查看页面元素的属性 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void getWebElementAttribute() { dri ...
- WebDriverAPI(10)
操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...
- WebDriverAPI(9)
操作JavaScript的Alert窗口 测试网址代码 <html> <head> <title>你喜欢的水果</title> </head> ...
- WebDriverAPI(4)
单击某个元素 采用元素id.click()方法即可 双击某个元素id.doubleClick 操作单选下拉列表 测试网页HTML代码 <html> <body> <sel ...
- WebDriverAPI(2)
操作浏览器窗口 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com" ...
- WebDriverAPI(8)
判断页面元素是否存在 测试网址 http://www.baidu.com Java语言版本API实例 @Test public void testIsElementPresent(){ driver. ...
- WebDriverAPI(6)
在指定元素上方进行鼠标悬浮 测试网址 http://www.baidu.com Java语言版本实例 @Test public void roverOnElement() { driver.manag ...
- WebDriverAPI(3)
获取页面的Title属性 被测网址http:http://www.baidu.com Java语言版本的API实例代码 String url = "http://www.baidu.com& ...
- WebDriverAPI(1)
访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...
随机推荐
- 《Just for Fun》---读后感
<Just for Fun>本书是Linux之父林纳斯自传,书名的意思是:只是为了好玩.主要是讲了林纳斯的人生经历,以及Linux的诞生过程.Linux从一个终端仿真器到一个世界瞩目的操作 ...
- 十年前,女:“对不起,我不会喜欢你的,你不要再坚持了,就好比让 Linux 和 Windows 同时运行在一台PC机上,可能吗?
1.十年前,女:“对不起,我不会喜欢你的,你不要再坚持了,就好比让 Linux 和 Windows 同时运行在一台PC机上,可能吗?”男生听后默默走开, 十年后,在一次虚拟技术大会上,我听到一名虚拟技 ...
- 字典树Java实现
Trie树的原理 Trie树也称字典树,因为其效率很高,所以在在字符串查找.前缀匹配等中应用很广泛,其高效率是以空间为代价的. 利用串构建一个字典树,这个字典树保存了串的公共前缀信息,因此可以降低查询 ...
- TabHost实现通话记录界面
//MainActivity.java public class MainActivity extends TabActivity { @Override public ...
- HDU 2393 Higher Math (判断直角三角形)
题意:给定三个边,判断是不是直角三角形. 析:水题,勾股定理... 代码如下: #include <iostream> #include <cstdio> #include & ...
- 分享一个以前写的基于C#语言操作数据库的小框架
一:前言 这个是以前写的操作MySQL数据库的小型框架,如果是中小型项目用起来也是很不错的,里面提供Filter.ModelPart.Relationship等机制实现操作数据库时的SQL语句的拼接和 ...
- SPI通信协议(SPI总线)学习
1.什么是SPI? SPI是串行外设接口(Serial Peripheral Interface)的缩写.是 Motorola 公司推出的一 种同步串行接口技术,是一种高速的,全双工,同步的通信总线. ...
- Tcl写法
#=============================================================== #Analysi&Synthesis execute_modu ...
- spring AbstractRoutingDataSource实现动态数据源切换
使用Spring 提供的 AbstractRoutingDataSource 实现 创建 AbstractRoutingDataSource 实现类,负责保存所有数据源与切换数据源策略:public ...
- 让tableView的某行移动到tableView的某位置
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:lineNumber inSection:0]; [lrcTableView selectR ...