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 ...
随机推荐
- python之数据类型1
什么是数据类型及数据类型分类 python中的数据类型 python使用对象模型来存储数据,每一个数据类型都有一个内置的类,每新建一个数据,实际就是在初始化生成一个对象,即所有数据都是对 ...
- 557. Reverse Words in a String III
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- hdu-1061(快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 思路:快速幂 #include<iostream> #include<cstd ...
- 关于FIR的modelsim
(1)FIR ip核仿真 (2)FIR 多通道应用 (3)多通道fir ip核需要注意的复位问题 =================================================== ...
- authentication 和 authorization
单词 词性 解释 authentication n. 认证 authentic adj. 真实的 authorization n. 授权 authorise vt. 授权 authentication ...
- 多线程中使用curl致coredump问题
coredump时的调用栈: #0 0x081eff2c in addbyter () #1 0x081f05b8 in dprintf_formatf () #2 0x081f15cf in ...
- (匹配 最小路径覆盖)Air Raid --hdu --1151
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1151 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- 类之间关系理解:组合>聚合>关联>依赖;实现,继承
类之间关系理解:组合>聚合>关联>依赖:实现,继承 1. 从类之间的关系来看,不外乎以下几种 组合>聚合>关联>依赖:实现,继承 且可以分为以下两类: (1)实现, ...
- Informatica bulk和normal模式
转载:http://bestxiaok.iteye.com/blog/1107612 Bulk 方式进行目标数据的Load,是Informatica提供的一种高性能的Load数据方式.它利用数据库底层 ...
- 通过FactoryBean方式来配置bean
1.实现FactoryBean接口的java类: 2.相应的.xml文件: