java selenium webdriver实战 应用小结
部分api
1.访问网站
driver.get("http://www.baidu.com");
或者
driver.navigate().to("http://www.baidu.com");
2.操作浏览器窗口
//声明一个point对象,两个150表示浏览器的位置相对于屏幕的左上角(0,0)的横坐标距离和纵坐标距离
Point point = new Point(150, 150);
//声明dimension对象,两个500表示浏览器窗口的长度和宽度
Dimension dimension = new Dimension(500, 500);
//设定浏览器窗口的大小为长500 宽500
driver.manage().window().setSize(dimension);
//最大化浏览器
driver.manage().window().maximize();
3.浏览器输入值 和点击
driver.navigate().to("http://www.baidu.com");
driver.findElement(By.id("kw")).clear();
driver.findElement(By.id("kw")).sendKeys("魔兽");
driver.findElement(By.id("su")).click();
4.操作多选的选择列表
Select dropSelect = new Select(driver.findElement(By.name("fruit")));
//判断页面是否进行多选
Assert.assertTrue(dropSelect.isMultiple());
//使用选择项索引选择第三个选项
dropSelect.selectByIndex(3);
//根据value值选择
dropSelect.selectByValue("value");
//根据选项文字选择
dropSelect.selectByVisibleText("苹果");
//取消所有选项的选中状态
dropSelect.deselectAll();
//取消第三个的选中状态
dropSelect.deselectByIndex(3);
//根据value值取消选择
dropSelect.deselectByValue("value");
//根据选项文字取消选择
dropSelect.deselectByVisibleText("苹果");
5.操作单选框
WebElement radioOption = driver.findElement(By.xpath("//input[@value='orange']"));
if(!radioOption.isSelected()){
radioOption.click();
}
List<WebElement> fruits = driver.findElements(By.name("fruit"));
for(WebElement fruit : fruits){
if(fruit.getAttribute("value").equals("watermelon")){
if(!fruit.isSelected()){
fruit.click();
Assert.assertTrue(fruit.isSelected());
break;
}
}
}
6.复选框同上
7.执行javascript脚本
JavascriptExecutor js = (JavascriptExecutor) driver;
//取消上传input隐藏
js.executeScript("document.getElementById(\"file\").style=\"display: block;\"");
JavascriptExecutor js = (JavascriptExecutor) driver;
//返回搜索按钮上的文字
String text = (String)js.executeScript("var button = document.getElementById(\"stb\");return button.value");
8.等待操作
设定查找页面元素的最大等待时间,调用findElement方法的时候没有能立即找到某个元素
,则程序会每隔一段时间后不断的尝试判断页面的DOM中是否出现被查找的元素,如果超过
设定的等待时长依旧没有找到,则抛出NoSuchElementException
隐形等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
@BeforeClass
public static void init() {
System.out.println("init...");
System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// 创建一个 ChromeDriver 的接口,用于连接 Chrome,
//必须要有chromedriver.exe文件,selenium默认不能启动chrome
// 创建一个 Chrome 的浏览器实例
driver = new ChromeDriver();
//最大化浏览器
driver.manage().window().maximize();
//设置全局的隐形等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
隐性等待的默认时长是0,一旦设置,这个隐试等待会在webdriver对象实例的整个生命周期起作用
显示等待
显示等待比隐式等待更节约测试脚本执行的时间,推荐使用显示等待判断页面元素是否存在
使用ExpectedConditions类中自带的方法,可以进行显示等待的判断
//页面元素是否在页面上可用和可被点击
ExpectedConditions.elementToBeClickable(By locator);
//页面元素是否处于被选中状态
ExpectedConditions.elementToBeSelected(By locator);
//页面元素在页面是否存在
ExpectedConditions.presenceOfElementLocated(By locator);
//是否包含特定的文本
ExpectedConditions.textToBePresentInElement(locator, text)
//页面元素值
ExpectedConditions.textToBePresentInElementValue(locator, text);
//标题
ExpectedConditions.titleContains(title);
// 等待元素可见且可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));
自定义的显示等待
public static void sendKeysByXPath(WebDriver driver, String path, String key) {
WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
WebElement element = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(path));
}
});
highLightElement(driver,element);
element.clear();
element.sendKeys(key);
}
注意一点:如果是该元素本来就在DOM中存在,但是此时是隐藏的,你是可以获得该元素的,但是缺无法操作
做法就是线程等待或者等待元素可用,推荐第二种,线程等待的时间不定,不稳定
public static void sendKeysByXPath(WebDriver driver, String path, String key) {
WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
// 等待元素可见且可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(path)));
WebElement element = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.xpath(path));
}
});
highLightElement(driver,element);
element.clear();
element.sendKeys(key);
}
9.操作javascript的Alert弹窗
driver.findElement(By.xpath(path)).click();
//切换到alert弹出框
Alert alert = driver.switchTo().alert();
AssertionUtil.assertEquals("这是个alert弹出框", alert.getText());
//使用alert的accept方法,单击alert的确定按钮,关闭alert
alert.accept();
//如果alert未弹出,会抛出NoAlertPresentException异常
//切换回去原来的窗体
driver.switchTo().defaultContent();
10.操作javascript的confirm弹窗
confirm与alert类似,不同的是
alert.accept();是确定
alert.dismiss();是取消
11.操作frame
有时候我们在定位一个页面元素的时候发现一直定位不了,反复检查自己写的定位器没有任何问题,
代码也没 有任何问题。这时你就要看一下这个页面元素是否在一个iframe中,这可能就是找不到的
原因之一。如果你在 一个default content中查找一个在iframe中的元素,那肯定是找不到的。反之
你在一个iframe中查找另一个 iframe元素或default content中的元素,那必然也定位不到
//进入id="frame"或者name="frame"的frame中,
driver.switchTo().frame("frame");
12.操作模态框
driver.switchTo().activeElement();
13.父子框
String newHandle = "";//获取当前窗口 父窗口
String currentHandle = driver.getWindowHandle();//点击页面元素打开新窗口
Set<String> handles = driver.getWindowHandles();//获取浏览器所有窗口
Iterator<String> itWin = handles.iterator();while(itWin.hasNext()){
String key = itWin.next();
if(currentHandle.equals(key)){
continue;
}
//得到新窗口
newHandle = key;
}//切换窗口
WebDriver newDriver = driver.switchTo().window(newHandle);
//业务操作
//set中移除新窗口
handles.remove(newHandle);
//切回主窗口
driver.switchTo().window(currentHandle);
14.js单击
有时候在点击页面的时候会存在该元素被其他元素覆盖的情况,比如,元素正被一个模态框挡住,此时模态框还没有消失,做法是等待
模态框消失,再点击一次.或者使用js来触发点击
/**
* js执行点击事件 for:Element is not clickable at point. Other element would
* receive the click
*
* @param driver
* @param selector
*/
public static void clickElementByJS(WebDriver driver, By selector) {
WebDriverWait wait = new WebDriverWait(driver, 10); // 最多等10秒
WebElement element = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
return d.findElement(selector);
}
});
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
}
java selenium webdriver实战 应用小结的更多相关文章
- [转]java selenium webdriver实战 应用小结
原文链接:http://www.cnblogs.com/itliucheng/p/5578788.html 部分api 1.访问网站 driver.get("http://www.baidu ...
- java selenium webdriver实战 seleniumIDE
Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...
- java selenium webdriver实战 helloWord
第一步:建立Maven项目 Selenium 支持 maven 工程,这会让你的工作更加简便. 用 Eclipse 建个 Maven 的工程,建成后,修改 pom.xml <dependenci ...
- java selenium webdriver实战 页面元素定位
自动化测试实施过程中,测试程序中常用的页面操作有三个步骤 1.定位网页上的页面元素,并存储到一个变量中 2.对变量中存储的页面元素进行操作,单击,下拉或者输入文字等 3.设定页面元素的操作值,比如,选 ...
- java selenium webdriver处理JS操作窗口滚动条
未经作者允许,禁止转载!!! java selenium webdriver处理JS操作窗口滚动条 java selenium webdriver处理JS操作窗口滚动条 import org.open ...
- python请求java Selenium Webdriver
下载jar包: selenium-server-standalone-2.44.0.jar 运行jar包: java -jar selenium-server-standalone-2.44.0.ja ...
- java selenium webdriver第一讲 seleniumIDE
Selenium是ThoughtWorks公司,一个名为Jason Huggins的测试为了减少手工测试的工作量,自己实现的一套基于Javascript语言的代码库 使用这套库可以进行页面的交互操作, ...
- Java + Selenium + WebDriver八大元素定位方式
UI自动化测试的第一步就是进行元素定位,下面给大家介绍一下Selenium + WebDriver的八大元素定位方式.现在我们就以百度搜索框为例进行元素定位,如下图: 一.By.name() Java ...
- java selenium webdriver第四讲 应用小结
部分api 1.访问网站 driver.get("http://www.baidu.com"); 或者 driver.navigate().to("http://www. ...
随机推荐
- 幻世(OurDream)2D图形引擎大更新——炫丽粒子特效强势回归!
本次更新终于让各位期待已久的绚丽粒子系统特效强势回归到幻世当中了.凭借新引擎强大而又高效的绘图,新的粒子系统将比旧有版本(原Ycnd 2D)在性能上有极大幅度的增强,增幅超过十倍! 更强的性能!更好的 ...
- shell基础认识
Shell 我们在终端下写命令Linux内核是看不懂的必须通过shell解释成内核可执行的代码 这就是shell(其实解释命令这只是它的一个功能模块,shell还可以用来进行程序设计) 有点类似win ...
- javascript外部ファイル
function myFunction() { document.getElementById("demo").innerHTML = "Paragraph cha ...
- 关于yii2的gridview关联搜索步骤
在使用yii2构建搜索视图,经常都会使用到gridview这个组件,这个组件十分强大,通过一定的配置就能进行关联搜索,下面就是简单的步骤 需求场景:一个车系表,里面存放在品牌表的id,现在要用品牌名字 ...
- Latex笔记-基本布局
转自https://linhan.blog.ustc.edu.cn/?p=135&cpage=1 目录改用中文标题并且居中 \renewcommand{\contentsname}{\cent ...
- 柯南君:看大数据时代下的IT架构(6)消息队列之RabbitMQ--案例(Publish/Subscribe起航)
二.Publish/Subscribe(发布/订阅)(using the Java Client) 为了说明这个模式,我们将构建一个简单的日志系统.它将包括两个项目: 第一个将发出日志消息 第二个将接 ...
- [LeetCode][Python]Reverse Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/reverse ...
- _extend用法总结
针对对象数组: 后面的属性会覆盖更新前面的属性 看代码: <!DOCTYPE html> <html> <head> <meta charset=" ...
- Linux下的经常使用性能查询命令top、vmstat、gprof、pidstat之对照
(1)查看各个CPU核的使用情况 sudo top -d 1 进入之后,按1,会出现以下的CPU使用情况,当中us列反映了各个CPU核的使用情况,百分比大说明该核在进行紧张的任务. (2)查看哪个进程 ...
- UVa 10491 Cows and Cars (概率&广义三门问题 )
10491 - Cows and Cars Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onli ...