Web UI自动化测试之Selenium3

258阅读模式

一、WebDriver(import org.openqa.selenium.WebDriver)操作浏览器

selenium通过WebDriver对象来定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html

1、打开浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html

打开火狐浏览器:文章源自懂站帝-http://www.sfdkj.com/13937.html

WebDriver driver = new FireFoxDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html

打开IE浏览器:文章源自懂站帝-http://www.sfdkj.com/13937.html

WebDriver driver = new InternetExplorerDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html

打开chrome浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html

WebDriver driver = new ChromeDriver();文章源自懂站帝-http://www.sfdkj.com/13937.html

2、最大化浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html

driver.manage().window().maximize();文章源自懂站帝-http://www.sfdkj.com/13937.html

3、关闭浏览器文章源自懂站帝-http://www.sfdkj.com/13937.html

driver.close();文章源自懂站帝-http://www.sfdkj.com/13937.html

driver.quit();文章源自懂站帝-http://www.sfdkj.com/13937.html

4、打开测试页面文章源自懂站帝-http://www.sfdkj.com/13937.html

driver.get("http:www.baidu.com");文章源自懂站帝-http://www.sfdkj.com/13937.html

二、By(org.openqa.selenium.By) 定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html

selenium通过By对象来定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html

1、By.id 通过ID定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html

By.id("UserCode")文章源自懂站帝-http://www.sfdkj.com/13937.html

2、By.name 通过name定位页面元素文章源自懂站帝-http://www.sfdkj.com/13937.html

By.id("UserCode")

3、By.className 通过className定位页面元素

By.className("input_class")

4、By.linkText 精确查找

drive.get("http://www.baidu.com");

By.linkText("百科");

5、By.partialLinkText 模糊查找

driver.get("http://www.baidu.com");

By.partialLinkText("百科");

6、By.tagName

dirver.get("http://www.baidu.com");

By.tagName("input");

三、WebElement( org.openqa.selenium.WebElement)操作页面元素

selenium通过WebElement对象来操作页面元素

1、操作输入框

WebElement inputElement = driver.findElement(By.id("UserCode"));//查找登录页面录入用户名元素

inputElement.setKeys("wyl");//输入框录入用户名wyl

inputElement.clear();//清空输入框

inputElement.getText();//获得输入框中的内容

2、操作单选框

WebElement radioElement = driver.findElement(By.id("Orders"));

radioElement.click();// 选择某个单选项

radioElement.clear();// 清空某个单选项

radioElement.isSelected();// 判断某个单选项是否已被选择

3、操作多选框

WebElement checkboxElement = driver.findElement(By.id("Orders"));

checkboxElement.click();// 选择某个多选项

checkboxElement.clear();// 清空某个多选项

checkboxElement.isSelected();//判断某个多选项是否已被选择

4、操作下拉框

Select select = new Select(diver.findElement(By.id("region")));

select.selectByVisibleText("北京市");

select.selectByText("10010");

select.deselectAll();

select.deselectByVisibleText("北京市");

select.deselectByText("10010");

select.getAllSelectedOptions();

select.getFirstSelectedOption();

5、操作上传文件

WebElement uploadElement = driver.findElement(By.id("file"));

String uploadFile = "D:\\1\\AgentCode.txt";

uploadElement.setKeys(uploadFile);

6、操作按钮

WebElement btnElement = driver.findElement(By.id("add"));

btnElement.click();

btnElement.isEnabled();

7、表单提交

WebElement formElement = driver.findElement(By.id("fm"));

fromElement.submit();

8、操作弹出对话框

Alert alert = driver.swichTo().alert();

alert.getText();//获得弹出框内容

alert.accept();//点击弹出框确认按钮

alert.dismiss();//点击弹出框取消按钮

9、Windows和Frames之间的切换

driver.switchTo().defaultContent();//返回到最顶层的frame

driver.switchTo().frame("leftMenuFrame");//切换到左侧菜单frame

driver.switchTo().window("windowName");//切换到某个windows

10、超时设置

driver.manage().timeouts().implicitly(10,timeUnit.Seconds);//识别元素时的超时时间

driver.manage().timeouts().pageLoadTimeout(10,timeUnit.Seconds);//页面加载时的超时时间

driver.manage().timeouts().setScriptTimeout(10,timeUnit.Seconds);//异步脚本的超时时间

四、调用js

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("JS脚本");

五、Selenium官方

1、官方download包下载地址

http://code.google.com/p/selenium/downloads/list

2、官方User Guide

http://seleniumhq.org/docs/

3、API

http://selenium.googlecode.com/git/docs/api/java/index.html

懂站帝
  • 本文由 发表于 2022年6月9日 22:32:38
  • 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至395045033@qq.com举报,一经查实,本站将立刻删除。
评论  0  访客  0