一、打开chrome浏览器
1. 安装chrome浏览器
2. 下载控制chrome的驱动器
chrome的版本和chromedriver的版本对应关系和下载地址
https://blog.csdn.net/huilan_same/article/details/51896672
存放路径:
/工程名/src/main/resources/selenium/driver/chromedriver.exe
3. 下载selenium的jar包
pom.xml添加dependency
<project>
<dependencies>
<!--selenium框架 -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.50.0</version>
</dependency>
<!--testNG测试框架 -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
</project>
4. 新建java类,启动浏览器
自写的类——>selenium——>chromedriver.exe——>chrome浏览器
//此处src前面没有"/",说明是相对工程根目录的路径
System.setProperty("webdriver.chrome.driver",
"src/main/resources/selenium/driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();
如果要窗口最大化,先设置参数,启动的时候传入
//设置环境变量,指定chromedriver的路径
System.setProperty("webdriver.chrome.driver",
"src/main/resources/selenium/driver_v236_63_65/chromedriver.exe");
//设置浏览器的参数
ChromeOptions options = new ChromeOptions();
//最大化浏览器
options.addArguments("--test-type", "--start-maximized");
//指定浏览器位置
//options.setBinary("C:/XXXXXXX/chrome.exe");
//打开浏览器
WebDriver driver = new ChromeDriver(options);
常见报错原因:
- 浏览器和chromedriver版本不一致
- 防火墙导致无法访问chrome浏览器,关闭防火墙
5. 关闭浏览器
//先线程休眠3秒,便于观察,然后才关闭,不然启动就关闭像闪退
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//关闭浏览器,driver.close()是关闭当前窗口
driver.quit();
sleep()方法:
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
二、地址栏和导航(前进、后退、刷新)
1. get()方法打开
driver.get("http://www.baidu.com");
2. navigate().to()打开
driver.navigate().to("http://www.dangdang.com");
3. navigate导航
// 1\. 先打开一个界面
driver.navigate().to("http://www.baidu.com");
//2\. to()方法再打开另一个界面
driver.navigate().to("http://www.dangdang.com");
sleep(2000);
//3\. back()回退
driver.navigate().back();
sleep(2000);
//4\. forward()前进
driver.navigate().forward();
sleep(2000);
//5\. refresh()刷新
driver.navigate().refresh();
三、4种常见方式定位元素
元素包含信息:
- 标签
- 属性
- 内容
- 位置
1. 按id属性定位元素
WebElement alertButton = driver.findElement(By.id("alertButtonId"));
2. 按name属性定位元素
WebElement alertButton = driver.findElement(By.name("alertButtonName"));
3. 按class定位元素
WebElement buttons = driver.findElements(By.className("alertButtonClass"));
4. 使用xpath定位
1)自动化处理的对象:标签(也称为元素)
java | html |
---|---|
WebElement元素类 | 标签(如html、body、head、table、input、tr、alert弹出框等等) |
2) 标签
- 标签名
- 标签的属性
- 为了定位标签的属性:id、name、class、type
- 为了产生交互效果的属性:触发事件,可以指定触发后要执行的方法
- 要标识的数据:标签都是为了描述数据的
2)xpath:选择html标签
符号 | 用途 | 示例 |
---|---|---|
/ | 绝对路径 | /html/body/table/tbody/tr/td/input |
// | 相对路径 | //body/table//input |
标签名 | html的所有标签 | //input |
[] | 限定t条件 | //input[@id='xxxid' and @type='button'] |
数字 | 指定匹配到的第几个 | //input[3] |
@属性名=属性值 | 通过属性限定条件 | |
函数() | 通过函数限定条件 | |
and/or | 连接多个条件 |
WebElement alertButton = driver.findElement(
By.xpath("//input[@id='alertButtonId' and @type='button']"));
四、常见元素的基本操作
0. 界面模板
<html>
<head>
<title>导航栏</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
xxxxxx替换元素代码xxxxxxx
</body>
</html>
1. text文本框
界面:
<input type="text" name="edit" id="edit" value="" />
自动化代码
sendkeys()传要填的内容
WebElement text=
driver.findElement(By.xpath("//input[@type='text' and @id='edit']"));
text.clear();
text.sendKeys("傻不傻?傻!");
2. file文件上传
界面:
<input type="file" name="attach[]" />
自动化代码
找到元素,sendkeys()传文件路径
WebElement input=
driver.findElement(By.xpath("//input[@type='file' and @name='attach[]']"));
input.clear();
input.sendKeys("C:/HtmlWeb/selenium/html_00_summary.html");
3. radio单选框
界面:
<input type='radio' name="company" value='Baidu' /> <label>百度</label> <br/>
<input type='radio' name="company" value="AliBaBa"/> <label>阿里巴巴</label><br/>
<input type='radio' name="company" value='Tencent' checked /><label>腾讯</label><br/>
<input type='radio' name="company" value='Mi' /> <label>小米</label>
自动化代码
input元素,类型是raidio,name相同的多个radio类型的input组成选项,靠value进行区分
选择具体某个选项,并选择:
WebElement radio=
driver.findElement(
By.xpath("//input[@type='radio' and @name='company' and @value='Mi']"));
radio.click();
所有选项都点一遍:
List<WebElement> radios=
driver.findElements(By.xpath("//input[@type='radio' and @name='company']"));
for(int i=0;i<radios.size();i++){
WebElement item=radios.get(i);
sleep(1000);
}
4. checkbox多选框
界面:
<input type="checkbox" name="course" value="web" /><label>网络</label><br />
<input type="checkbox" name="course" value="training" /><label>培训</label><br />
<input type="checkbox" name="course" value="friend" /><label>朋友介绍</label><br />
<input type="checkbox" name="course" value="other" /><label>其他方式</label>
自动化代码
input元素,类型是checkbox,name相同的多个checkbox类型的input组成选项,靠value进行区分
选择具体某一个选项,并选择:
WebElement checkbox=
driver.findElement(
By.xpath("//input[@type='checkbox' and @name='course' and @value='web']"));
checkbox.click();
所有选项都勾选:
List<WebElement> checkboxs=
driver.findElements(
By.xpath("//input[@type='checkbox' and @name='course']"));
for(int i=0;i<checkboxs.size();i++){
WebElement item=checkboxs.get(i);
item.click();
sleep(1000);
}
5. 时间控件
界面:
<input type="date" name="startTime">
自动化代码
先写JavaScript代码,然后通过driver执行js
js第一句是将只读属性去掉(若时间没设只读,则不需要)
js第二句是给时间元素设置value属性,值为“2018-04-10”
String js="document.getElementsByName('startTime')[0].removeAttribute('readOnly');document.getElementsByName('startTime')[0].setAttribute('value','2018-04-10');";
JavascriptExecutor jsDriver = (JavascriptExecutor) driver;
jsDriver.executeScript(js);
6. button按钮
界面:
<input type="button" name="promptbutton"
value="测试prompt对话框" onclick="confirm('确定提交吗?');" />
自动化代码
找到元素,click()点击
WebElement button=
driver.findElement(By.xpath("//input[@type='button' and @id='alertButtonId']"));
button.click();
Alert alert=driver.switchTo().alert();
alert.accept();
7. 文本域
界面:
多行多列的输入框
<textarea rows="3" ></textarea>
自动化代码
WebElement textarea=driver.findElement(By.xpath("//textarea[@rows='3']"));
textarea.clear();
textarea.sendKeys(“内容”);
8. img图片
界面:
可点击的图片,都是外面有一层<a>超链接,只是用图片替代了文本
自动化测试的时候,要定位的是<a>超链接
<a id='imgA'>
<img src="xxxx">
</a>
自动化代码
- 定位<a>标签
- 点击
WebElement img=driver.findElement(By.xpath("//a[@id='imgA']"));
img.click();
7. select选择框
界面:
select标签:定义一个下拉框
option选项:定义一个选项,一个下拉框可以有很多个选项,即多个option
option的3个属性:index(选项序号,默认自动加上的)、value选项值、visibleText展现文字
<select id="Selector">
<option value="apple" >苹果</option>
<option value="peach" >桃子</option>
<option value="banana" >香蕉</option>
<option value="orange">桔子</option>
<option value="grape" >葡萄</option>
<option value="mango" >芒果</option>
</select>
网友评论