Java自动化获取页面主题_基于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);

}

1f703f4550bd790bf3d7951fd833683cbfa92a4b_1_690x281.jpg

QQ图片20161117210058.jpg1117x456 68.6 KB

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.操作单选下拉列表

5ec6471997b056532fb2a60c685d09411c9ac3fc_1_690x422.png

QQ图片20161117213829.png1077x660 61.5 KB

(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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值