[转]java selenium webdriver实战 应用小结
原文链接:http://www.cnblogs.com/itliucheng/p/5578788.html
部分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实战 应用小结
部分api 1.访问网站 driver.get("http://www.baidu.com"); 或者 driver.navigate().to("http://www. ...
- 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. ...
随机推荐
- express实现前后端通信上传图片,存储数据库(mysql)傻瓜教程(三)完结篇
终于完成了所有自己想要的功能(鼓励下自己),虽然还是很简陋,但是还是挺有满足感的,哈哈. 附上前两篇的链接: 第一篇 第二篇 进入正题,在第二篇里面已经完成了连接数据库,并且实现了对数据库的增删改查, ...
- 在VM虚拟机上安装Microsoft Dynamics CRM 2016 步骤图解及安装注意事项
安装Dynamics CRM 2016环境配置要求: 系统版本:Windows Server 2012 R2(必须) SQL 版本: SQLServer2014SP1-FullSlipstream-x ...
- MDM证书申请的流程
MDM证书申请的流程 整个流程分为两部分:vendor,customer. 一.Vendor 1.成为一个 MDM Vendor 1) 首先你需要拥有一个 Apple Enterprise accou ...
- GCD封装的个人理解和应用
GCD封装的个人理解和应用 特点 >>将GCD封装,使我们从繁琐的方法记忆中解脱出来,能够直接快速的应用. 使用方法 1.将工程中的GCD文件中的9个文件拖入自己的工程中(你自己最好建一个 ...
- CocoaPods安装及使用详情
CocoaPods是什么? 当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,SDWebImage等等.可能某个类库又用到其他类库,所以要使用它,必须得另外下载其他类库,而其他 ...
- MySQL的数据库与表格创建
打开MySQL: 1.进入终端输入:cd /usr/local/mysql/bin/ 2.回车后 登录管理员权限 sudo su 3.回车后输入以下命令来禁止mysql验证功能 ./mysqld_sa ...
- js中的运算总结(未完待续
[74由几个1几个5几个10几个20组成] var num = 74; for(one = 0; one <= num; one++){ for(five = 0; five <= num ...
- 测试几个xml的问题
使用sql server的时候,免不了与xml的参数打交道,xml大多数时候都给我们的程序带来方便,但是也有些时候会有变量赋值不通过的时候.(当然罗,如果你本身xml都通不过 xml spy 之类软件 ...
- ASP.NET MVC Razor
Razor是MVC3中才有的新的视图引擎.我们知道,在ASP.NET中,ASPX的视图引擎依靠<%和%>来调用C#指令.而MVC3以后有了一套新的使用@标记的Razor语法,使用起来更灵活 ...
- js字符串转为日期格式
1. <script type="text/javascript"> //字符串转日期格式,strDate要转为日期格式的字符串 function getDate(st ...