WebDriver高级应用实例(4)
4.1操作web页面的滚动条
被测网页的网址:
http://v.sogou.com
Java语言版本的API实例代码
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod; import javax.swing.event.TreeWillExpandListener; import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod; public class scrolling {
WebDriver driver;
String url ="http://v.sogou.com";
//priority = 1 表示测试用例的第一优先级
@Test(priority = 1)
public void scrollingToBottomofAPage() {
//将页面滚动至页面最下方
((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight)");
//设置3秒停顿验证滚动条是否移动至指定位置
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test(priority = 2)
public void scrollingToElementofAPage(){
//找到标签文字为电视剧的标签
WebElement element = driver.findElement(By.xpath("//*[@id='container']//a[text()='电视剧']"));
//使用scrollIntView()函数。将滚动条移至元素所在的位置
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",element);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test(priority = 3)
public void scrollingByCoordinatesofAPage(){
//将页面滚动条向下移动800个像素
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,800)");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
//最大化
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
} }
4.2Robot对象操作键盘
被测网页的网址:
http://www.sogou.com
Java语言版本的API实例代码
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod; import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
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 testRoboot {
WebDriver driver;
String url ="http://www.sogou.com";
@Test
public void testRobootOperateKeyBoard() throws InterruptedException {
//设置显示等待10秒
WebDriverWait wait = new WebDriverWait(driver, 10);
//使用显示等待判断页面是否存在搜索框
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("query")));
//使用ctrl+v将seleium复制到搜索框中
setAndctrlVClipboardData("seleium");
//使用封装的方法 按下tab键将焦点移至搜索按钮
pressTabKey();
//调用封装的方法按下enter搜索
pressEnterKey();
//等待3秒查看是否执行
Thread.sleep(3000);
}
public void pressEnterKey() {
//声明Robot对象
Robot robot = null;
try {
//生成Robot对象
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//调用KeyPress()方法实现按下Enter键
robot.keyPress(KeyEvent.VK_ENTER);
//调用keyRelease()方法实现释放Enter键
robot.keyRelease(KeyEvent.VK_ENTER);
}
public void pressTabKey() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
public void setAndctrlVClipboardData(String string) {
//声明StringSelection对象并使函数的string实例化
StringSelection stringSelection = new StringSelection(string);
//使用toolkit对象的setContens方法将字符串放入将字符串放入剪切板中
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
e1.printStackTrace();
}
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
} @AfterMethod
public void afterMethod() {
driver.quit();
}
}
WebDriver高级应用实例(4)的更多相关文章
- WebDriver高级应用实例(10)
10.1控制HTML5语言实现的视频播放器 目的:能够获取html5语言实现的视频播放器视频文件的地址.时长.控制进行播放暂停 被测网页的网址: http://www.w3school.com.cn/ ...
- WebDriver高级应用实例(9)
9.1封装操作表格的公用类 目的:能够使自己编写操作表格的公用类,并基于公用类进行表格中的元素的各类操作 被测网页的网址的HTML代码: <html> <body> <t ...
- WebDriver高级应用实例(8)
8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...
- WebDriver高级应用实例(7)
7.1在测试中断言失败的步骤进行屏幕截图 目的:在测试过程中,在断言语句执行失败时,对当前的浏览器进行截屏,并在磁盘上新建一个yyyy-mm-dd格式的目录,并在断言失败时新建一个已hh-mm-ss格 ...
- WebDriver高级应用实例(6)
6.1精确比较网页截图图片 目的:对于核心界面进行截屏,并且使用测试过程中的截图和以前测试过程中的截图进行比较.确认页面是否发生了改变 被测网页的网址: http://www.baidu.com Ja ...
- WebDriver高级应用实例(5)
5.1对象库(UI Map) 目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离.方便不具备编码能力的测试人员进行修改和配置. 被测网页的网址: http:/ ...
- WebDriver高级应用实例(3)
3.1自动化下载某个文件 被测网页的网址: https://pypi.org/project/selenium/#files Java语言版本的API实例代码 import java.util.Has ...
- WebDriver高级应用实例(2)
2.1在日期选择器上进行日期选择 被测网页的网址: https://www.html5tricks.com/demo/Kalendae/index.html Java语言版本的API实例代码 impo ...
- WebDriver高级应用实例(1)
1.1使用JavaScriptExecutor单击元素 被测网页的网址: http://www.baidu.com Java语言版本的API实例代码 import org.testng.annotat ...
随机推荐
- flask 知识积累
PythonWEB框架之Flask Flask快速入门,知识整理 Flask 框架
- keepalive主从上同时出现VIP,且均无法消失
低版本bug 双主架构中,keepalived日志出现: more /var/log/messageOct 9 03:16:22 mysql-dzg-60-148 Keepalived_vrrp[85 ...
- AngularJs ng-repeat用法二$parent.$index
我们在开发时时常会出现repeat嵌套使用的情况,此时会想获取父级repeat数组的下标可使用$parent.$index
- Atcoder Grand-014 Writeup
A - Cookie Exchanges 题面 Takahashi, Aoki and Snuke love cookies. They have A, B and C cookies, respec ...
- canvas打字效果
运用fillText,写的打字效果. 唯一麻烦的地方是,换行问题, 我是把字符串转化为数组,数组一个单位完成,就换行,继续下一个单位. <!doctype html> <html&g ...
- 屏幕抓取程序 (位图DDB的例子)
屏幕抓取程序的意思是将整个屏幕图显示在应用程序的用户区中,等价于截图.对桌面窗口的操作:首先得知道桌面窗口的宽和高,获取宽和高需要利用窗口的设备句柄,而获取设备句柄需要知道窗口句柄,这一系列的连串关系 ...
- (转)Memcache内存分配策略
转自:http://hi.baidu.com/software_one/item/0a0a6712dc7a319899ce33e0 一.Memcache内存分配机制 关于这个机制网上有很多解释的,我个 ...
- spring之jdbcTemplate
spring的另一个功能模块data access对于数据库的支持 spring data access第一个helloword案例: 使用java程序实现访问配置 1.导包 2.测试案例 @Test ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
- CI
做项目时,经常会碰到需要使用php的情况,自己也下决心把php好好学一下. 先从CI开始,再看一下项目中的php代码是如何写的.