基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器
前边有详细介绍启动三种浏览器的方式(IE、Chrome、Firefox);
private WebDriver driver = null;
private String chromeDriverDir = "D:\\workspace\\A_Test\\resource\\chromedriver.exe";
/**
* 打开谷歌浏览器;
*/
public void openChromeBrowser(){
System.setProperty("webdriver.chrome.driver", chromeDriverDir);
driver = new ChromeDriver();
}
2.访问网页地址
方式一:
/**
* 访问网页地址方式一;
*/
public void visitURL1(){
String baseUrl = "http://www.baidu.com/";
driver.get(baseUrl);
}
方式二:
/**
* 访问网页地址方法二;
*/
public void visitURL2(){
String baseUrl = "http://www.sogou.com/";
driver.navigate().to(baseUrl);
}
3. 模拟后退功能
/**
* 模拟后退功能;
*/
public void visitRecentUrl(){
String url1 = "http://www.baidu.com/";
String url2 = "http://www.sogou.com/";
driver.navigate().to(url1); //先访问百度
driver.navigate().to(url2); //再访问搜狗
driver.navigate().back(); //返回上一次返回的百度页面;
}
4.模拟前进功能
/**
* 模拟前进功能;
*/
public void visitNextUrl(){
String url1 = "http://www.baidu.com/";
String url2 = "http://www.sogou.com/";
driver.navigate().to(url1); //先访问百度
driver.navigate().to(url2); //再访问搜狗
driver.navigate().back(); //返回上一次返回的百度页面;
driver.navigate().forward(); //从百度页面跳转的搜狗页面;
}
5. 刷新页面
/**
* 刷新当前页面;
*/
public void refreshCurrentPage(){
String baseUrl = "http://www.sogou.com/";
driver.navigate().to(baseUrl);
driver.navigate().refresh(); //刷新当前页面
}
6.窗口最大化
/**
* 窗口最大化
*/
public void maxWindows(){
String baseUrl = "http://www.sogou.com/";
driver.navigate().to(baseUrl);
driver.manage().window().maximize(); //窗口最大化;
}
7.获取当前页面的title属性值
/**
* 获取当前页面的title属性值;
*/
public void getTitle(){
String url = "http://www.baidu.com/";
driver.navigate().to(url);
String title = driver.getTitle(); //获取当前页面的title值;
System.out.println(title);
}
8.获取当亲页面的源代码
/**
* 获取当前页面的源代码;
*/
public void getPageSource(){
String url = "http://www.baidu.com/";
driver.navigate().to(url);
String source = driver.getPageSource(); //获取当前页面的源代码;
System.out.println(source);
}
9. 获取当前页面的网址
/**
* 获取当前页面的网址;
*/
public void getCurrentUrl(){
String url = "http://www.baidu.com/";
driver.navigate().to(url);
String currentUrl = driver.getCurrentUrl(); //获取当前页面的网址;
System.out.println(currentUrl);
}
10. 在输入框中清空原有的文字内容
/**
* 清空输入框原有的文字内容
*/
public void clearText(){
//获取输入框对象;
WebElement inputText = driver.findElement(By.id("kw"));
//清空输入框中的默认文字
inputText.clear();
}
11.在输入框中输入指定文本内容
/**
* 在输入框中输入指定文本
*/
public void inputText(){
//获取输入框对象;
WebElement inputText = driver.findElement(By.id("kw"));
//编辑需要输入的文本;
String text = "UI 自动化";
//在输入框中的输入文本
inputText.sendKeys(text); //sendKeys()方法,是用于输入;
}
12.单击按钮
/**
* 单击按钮
*/
public void clickButton(){
//获取按钮对象;
WebElement button = driver.findElement(By.id("su"));
//判断按钮是否可用
boolean isEnabled = button.isEnabled();
//如果按钮可以点击,就点击按钮;
if(isEnabled){
button.click();
}
}
13.双击元素
/**
* 双击某个元素
*/
public void doubleClick(){
//获取输入框对象;
WebElement inputText = driver.findElement(By.id("kw"));
//声明Action对象
Actions action = new Actions(driver);
//使用doubleClick方法,双击输入框;
action.doubleClick(inputText).build().perform();
}
14.右击元素
/**
* 右击某个元素
*/
public void rightClick(){
//获取输入框对象;
WebElement inputText = driver.findElement(By.id("kw"));
//声明Action对象
Actions action = new Actions(driver);
//使用contextClick方法,右击输入框;
action.contextClick(inputText).build().perform();
}
15.操作单选下拉列表
(1)使用下拉列表的下标选择子选项;
/**
* 通过下标选择下拉框的值;
*/
public void operateDropListByIndex(){
//获取下拉列表元素对象;
WebElement selectElement = driver.findElement(By.id("session_kept"));
//声明Select对象;
Select select = new Select(selectElement);
//通过子选项的下标来选择:下标从0开始
select.selectByIndex(0); //表示选择第一个子选项:不保存登录状态;
}
(2)使用value选择;
/**
* 通过子选项的value选择下拉框的值;
*/
public void operateDropListByValue(){
//获取下拉列表元素对象;
WebElement selectElement = driver.findElement(By.id("session_kept"));
//声明Select对象;
Select select = new Select(selectElement);
//通过子选项的选项的value值来选择:
String value = "60" ; //例如:“保存一小时 ” 这个选项的value值是:60
select.selectByValue(value);
}
(3)通过可见文本选择;
/**
* 通过子选项的可见文本选择下拉框的值;
*/
public void operateDropListByVisibleText(){
//获取下拉列表元素对象;
WebElement selectElement = driver.findElement(By.id("session_kept"));
//声明Select对象;
Select select = new Select(selectElement);
//通过子选项的选项的value值来选择:
String visibleText = "保存一天" ; //例如:第6个选项的 可见文本是:“保存1天 ” ;
select.selectByVisibleText(visibleText);
}
16.操作链接
/**
* 点击链接对象;
*/
public void clickLinkText(){
//获取链接元素对象;
WebElement link = driver.findElement(By.linkText("快速注册"));
//点击链接对象;
link.click();
}
17.操作单选按钮
/**
* 操作单选框RadioButton
*/
public void clickRadioButton(){
//获取单选按钮元素对象;
WebElement radio = driver.findElement(By.id("identity"));
//判断单选按钮是否已经被选中;
boolean isSelect = radio.isSelected();
//如果没有选中,就点击,如果已经选中,就不操作;
if(!isSelect){
radio.click();
}
}
18.操作复选框
/**
* 操作复选框CheckBox
*/
public void clickCheckBox(){
//获取复选框元素对象;
WebElement checkbox = driver.findElement(By.id("checked"));
//判断复选框是否已经被选中;
boolean isSelect = checkbox.isSelected();
//如果没有选中,就点击,如果已经选中,就不操作;
if(!isSelect){
checkbox.click();
}
}
我们专注于持续集成,更多原创请关注:www.hordehome.com
基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍的更多相关文章
- 基于Selenium2+Java的UI自动化(1) - 原理和环境搭建
一.Selenium2的原理 Selenium1是thoughtworks公司的一个产品经理,为了解决重复烦躁的验收工作,写的一个自动化测试工具,其原理是用JS注入的方 式来模拟人工的操作,但是由于J ...
- 基于Selenium2+Java的UI自动化(8)- 显式等待和隐式等待
一.隐式等待 package com.automation.waits; import java.util.concurrent.TimeUnit; import org.openqa.seleniu ...
- 基于Selenium2+Java的UI自动化(6)-操作Alert、confirm、prompt弹出框
alert.confirm.prompt这样的js对话框在selenium1 时代处理起来比价麻烦,常常要用autoit来帮助处理.而现在webdriver对这些弹出框做了专门的处理,使用seleni ...
- 基于Selenium2+Java的UI自动化(2) - 启动浏览器
一.准备工作 我们常用的浏览器主要有三个:chrome.Firefox.IE:其中chrome 和 IE 需要下载驱动程序,才能启动浏览器,注意驱动程序有32位和64位两种. 另外:如何查看本机的浏览 ...
- 基于Selenium2+Java的UI自动化(3) - 页面元素定位
一.几种网页定位方式 webdriver的页面定位很灵活,提供了8种定位方式: 其中,常见的有三种:id .cssSelector .xpath: 一个元素如果存在 id 属性,则这个 id 的值,在 ...
- 基于Selenium2+Java的UI自动化(5) - 执行JavaScript脚本
一.操作日期选择框 QQ图片20161118215530.png1336x545 22.6 KB 说明:日期选择框大部分是不支持前端输入的,因为这个对象是 readOnly,只读属性,selenium ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- 测试自动化:java+selenium3 UI自动化(2) - 启动Firefox
1. selenium和浏览器 基于selenium的这套自动化体系,其实现关键就在于对于各浏览器的顺畅操作. 事实上当selenium刚开始起家的时候,他使用的还是javascript注入的方式来驱 ...
- 测试自动化:java+selenium3 UI自动化(1) - 环境搭建
1.前言 我大概是在2012年第一次正式接触到自动化测试,那个时候跟随我的团队一起,就当时项目的UI自动化尝试做出了探索. 在我离开那家公司的时候,我们的自动化测试体系仍然难言完美,但是也已经达到了非 ...
随机推荐
- js基础第一天
js作用:网页特效(电梯导航).交互.表单特效.就是可以用来控制结构和样式. 常用的三个输出语句都属于js的内置对象,提供我们直接使用的功能就是内置对象功能. web三标准:结构.样式.行为.而js主 ...
- ruby 资料整理
http://blog.csdn.net/maingalaxy/article/details/46013393 http://blog.csdn.net/dzl84394/article/detai ...
- KNN算法java实现代码注释
K近邻算法思想非常简单,总结起来就是根据某种距离度量检测未知数据与已知数据的距离,统计其中距离最近的k个已知数据的类别,以多数投票的形式确定未知数据的类别. 一直想自己实现knn的java实现,但限于 ...
- HDU-4696 Answers 纯YY
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4696 题意:给一个图,每个点的出度为1,每个点的权值为1或者2.给n个询问,问是否能找到一条路径的权值 ...
- Uber将在泰国首推"优步摩托"服务
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 我用的Linux命令
从今天起,会在这里记一些我使用过的linux命令 =======I'm 分割线======= 11.查看某个端口号被哪个应用占用 netstat -apn|grep 端口号,lsof -i:端口号,n ...
- weblogic11g 安装——linux 无图形界面
weblogic11g 安装——linux下无weblogic安装图形界面 注意:此次安装,没做server.ip .系统规划 目的:学习weblogic11g 在linux下 无图形安装的过程 j ...
- [二]Json-lib的用法
1.Json字符串 PrintWriter out=response.getWriter(); // String resultJson="{\"name\":\&quo ...
- UINavgation日常小bug-有兴趣的朋友可以看看
UINavgation日常 UINavgation 今天在做一个小Demo,发现一个Bug,挺有意思的,就是在你不断调用Navigation- (void)pushViewController:(UI ...
- asp.net webservice 跨域解决方法
首先需要把[System.Web.Script.Services.ScriptService]前边的注释去掉,放开权限,其次需要在web.config 里<system.web节点下>添加 ...