WebDriver高级应用

public class Demo4 {

    WebDriver driver;

    // @BeforeMethod:在每个测试方法开始运行前执行
@BeforeMethod
public void beforeMethod(){
System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
driver = new ChromeDriver();
} // @AfterMethod:在每个测试方法开始运行结束后执行
@AfterMethod
public void afterMethod(){
driver.close();
} /**
* 使用JavaScriptExecutoru单击元素
*/
@Test
public void testDame(){
driver.get("http://www.baidu.com");
//找到百度首页的搜索输入框
WebElement searchInbox = driver.findElement(By.id("kw"));
//找到搜索按
WebElement searchButton = driver.findElement(By.id("su"));
//在输入框中输入字符
searchInbox.sendKeys("selenium");
//调用javaScriptClick方法单击按钮
javaScriptClick(searchButton); } public void javaScriptClick(WebElement element){
try {
//判断传入的element元素是否为可单击状态,是否可见
if(element.isEnabled() && element.isDisplayed()){
System.out.println("使用javascript进行页面元素的单击");
((JavascriptExecutor) driver).executeScript("arguments[0].click();",element);
}else {
System.out.println("页面上的元素无法进行单击操作");
}
} catch (NoSuchElementException e1) {
System.out.println("页面元素没有附加在网页中");
e1.printStackTrace();
} catch (Exception e2) {
System.out.println("无法完成单击操作");
e2.printStackTrace();
}
} /**
* 在Ajax方式产生的浮动框中,选择单击包含某些关键字的选项
* @throws InterruptedException
*/
@Test
public void testAjaxDemo() throws InterruptedException{
driver.get("https://www.sogou.com");
//找到搜索框
WebElement searthInbox = driver.findElement(By.id("query"));
searthInbox.click();
Thread.sleep(2000); //将浮动的列表选项存list集合中
List<WebElement> webElements = driver.findElements(By.xpath("//*[@id='vl']/div[1]/ul/li"));
for(WebElement webElement : webElements){
//判断是否包含关键字
if(webElement.getText().contains("5月新规")){
System.out.println("............. "+ webElement.getText());
webElement.click();
Thread.sleep(3000);
}
} } //设置一个页面对象的属性值
@Test
public void testDemo() throws InterruptedException{
driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/text.html");
//获取到id为text的元素对象
WebElement textElement = driver.findElement(By.id("text_test"));
//调用setAttribute方法修改文本框的value属性值,改变文本框中的文字
setAttribute(driver,textElement,"value","selenium");
//修改文本框的size属性值
setAttribute(driver, textElement, "size", "5");
Thread.sleep(2000);
//删除文本框的size属性值
removeAttribute(driver, textElement, "size");
Thread.sleep(3000);
} //修改页面元素属性的方法
private void setAttribute(WebDriver driver, WebElement textElement, String attributeName, String value) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute(arguments[1],arguments[2])", textElement,attributeName,value);
} //删除页面元素属性的方法
private void removeAttribute(WebDriver driver, WebElement textElement, String attributeName) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute(arguments[1])",textElement,attributeName);
} /**
* 在日期选择器上进行日期选择
*/
@Test
public void datepickerTest(){
driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/text.html");
//获取到日期控制元素对象
WebElement datepicker = driver.findElement(By.xpath("//*[@id='d12']"));
//datepicker.click();
datepicker.sendKeys("2019-04-29");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 高亮显示正在被操作的页面元素
*/
@Test
public void highLightTest(){
driver.get("http://www.baidu.com");
WebElement searchInbox = driver.findElement(By.id("kw"));
WebElement submitButton = driver.findElement(By.id("su"));
//调用高亮处理方法
highLightElenment(searchInbox);
searchInbox.sendKeys("selenium");
highLightElenment(submitButton);
submitButton.click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //高亮处理元素对象
private void highLightElenment(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style',arguments[1])", element,"background:yellow;solid:red;"); } /**
* 用例执行失败对屏幕进行截屏
*/
public void screenTest(){ }
}
/**
* 自动下载文件
* @author Administrator
*
*/
public class DownloadFiles { //指定文件下载地址
static String downloadFilePath = "D:/download";
WebDriver driver;
JavascriptExecutor js;
String url; @BeforeMethod
public void beforeMethod(){
url = "http://ftp.mozilla.org/pub/firefox/releases/35.0b8/win32/zh-CN/";
} /* @AfterMethod
public void afterMethod(){
driver.quit();
}*/ @Test
public void download(){
ChromeOptions options = downloadChromeFile();//更改默认下载路径
driver = new ChromeDriver(options);
driver.get(url);
driver.findElement(By.partialLinkText("Stub")).click();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} public static ChromeOptions downloadChromeFile(){
ChromeOptions options = new ChromeOptions();
//指定下载路径
Map<String, Object> map = new HashMap<String, Object>();
map.put("profile.default_content_settings.popups", 0);
map.put("download.default_directory", downloadFilePath);
options.setExperimentalOption("prefs", map);
return options;
}
}
public class Upload {

    @Test
public void test(){
System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("file:///F:/workspace/selenium_demo/src/main/webapp/upload.html");
driver.findElement(By.name("uploadFile")).sendKeys("C:\\Users\\Administrator\\Desktop\\IDEA快捷键.txt");
}
}
/**
* 滑动滚动条到屏幕中间
* 滑动滚动条到指定元素位置
* 按横纵坐票滑动屏幕
* @author Administrator
*
*/
public class ScrollBar { WebDriver driver;
String url; @BeforeMethod
public void beforeMethod(){
url="http://v.sogou.com";
System.setProperty("webdriver.chrome.driver", "e:\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(url);
} @AfterMethod
public void afterMethod(){
driver.quit();
} //第一优先级执行测试用例
@Test(priority=1)
public void scrollBarToPage(){
//将页面的滚动条滑动到屏幕的最下方
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //第二优先级执行测试用例
@Test(priority=2)
public void scrollBarToElemneTOfoPage(){
//将页面的滚动条滑动到页面指定的元素位置 ... 电视剧栏位置
WebElement element = driver.findElement(By.xpath("//*[@id='container']/div[2]/div[2]/div[2]/div[1]/h3/a"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //第三优先级执行测试用例
@Test(priority=3)
public void position(){
//按模纵坐标滑动屏幕
((JavascriptExecutor) driver).executeScript("window.scrollBy (0,800)");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Selenium WebDriver高级应用的更多相关文章

  1. Selenium WebDriver高级用法

    Selenium GitHub地址 选择合适的WebDrvier WebDriver是一个接口,它有几种实现,分别是HtmlUnitDrvier.FirefoxDriver.InternetExplo ...

  2. Selenium webdriver 高级应用

    对于这一段还蛮有感慨的,只想说,代码还是需要自己去敲的. 1. 改变用户代理 import org.junit.AfterClass; import org.junit.BeforeClass; im ...

  3. Selenium webdriver Java 高级应用

    对于这一段还蛮有感慨的,只想说,代码还是需要自己去敲的. 1. 改变用户代理 import org.junit.AfterClass; import org.junit.BeforeClass; im ...

  4. Python3 Selenium自动化web测试 ==> 第八节 WebDriver高级应用 -- 结束Windows中浏览器的进程

    学习目的: 掌握WebDriver的高级应用 正式步骤: # -*- coding:utf-8 -*- from selenium import webdriver from selenium.web ...

  5. Python3 Selenium自动化web测试 ==> 第七节 WebDriver高级应用 -- 浮动框中,单击选择某个关键字选项

    学习目的: 了解WebDriver的高级应用 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  6. Python3 Selenium自动化web测试 ==> 第十一节 WebDriver高级应用 -- 显示等待 + 二次封装

    学习目的: 掌握显示等待 掌握二次封装 正式步骤: step1:显示等待的代码示例 # -*- coding:utf-8 -*- from selenium import webdriver from ...

  7. Python3 Selenium自动化web测试 ==> 第九节 WebDriver高级应用 -- 操作select 和 alert

    学习目的: 掌握页面常规元素的定位方法 场景: 网页正常的select元素下拉框常规方法和select专属方法 正式步骤: step1:常规思路select页面元素定位 处理HTML代码截图 # -* ...

  8. Python3 Selenium自动化web测试 ==> 第六节 WebDriver高级应用 -- 操作web页面的滚动条

    学习目的: 掌握页面元素定位以外的其他重要知识点. 正式步骤: 测试Python3代码 # -*- coding:utf-8 -*- from selenium import webdriver fr ...

  9. Python3 Selenium自动化web测试 ==> 第五节 WebDriver高级应用 -- 使用JavaScript操作页面元素

    学习目的: 中级水平技术提升 在WebDriver脚本代码中执行JS代码,可以解决某些 .click()方法无法生效等问题 正式步骤: Python3代码如下 # -*- coding:utf-8 - ...

随机推荐

  1. MYSQL实战-1.mysql基本架构

    1.mysql可分为server层和存储引擎 1.1 server层: 连接器.查询缓存.分析器.优化器 .执行器.包含所有内置函数(日期,时间,数学.加密函数),所有跨存储引擎的功能都在此层,比如存 ...

  2. linux安装mysql(tar.gz)

    1. 查看卸载自带的mysql # rpm -qa|grep MySQLMySQL-X.X.X#rpm -e MySQL-X.X.X # rpm -qa|grep mariadb #有些版本还得查看卸 ...

  3. [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...

  4. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  5. 洛谷 P1462 通往奥格瑞玛的道路(二分答案,堆优化dijkstra)

    传送门 解题思路 首先看题目问题,求经过的所有城市中最多的一次收取的费用的最小值是多少.一看“最大值最小”就想到了二分答案. 在读一遍题目,就是二分收取的费用,然后对于每一个二分的费用,跑一边最短路, ...

  6. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  7. SDK manager打不开解决办法

    在下载管理android SDK过程中,有时会出现SDK manager.exe打不开的情况,网上也罗列了各种解决办法,其中地址为http://blog.csdn.net/pipisorry/arti ...

  8. 使用Nginx代理和转发Websocket连接

    1.Websocket 简介 WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端. 2.Nginx 简介 ...

  9. 未能加载文件或程序集“WebApi”或它的某一个依赖项。试图加载格式不正确的程序。

    将项目的平台目标改为“Any CPU” 在项目上右击选择属性——>生成——>平台目标  选择Any CPU

  10. Python2视频教程

    目录 1. 说明 1.1. 马哥视频_修复v1 1.2. 马哥视频_修复v2 2. 目录 3. 下载链接 1. 说明 Python从入门到精通视频(全60集)马哥教育视频(已修复部分视频无声音的问题+ ...