遇到的问题:
项目中出现了输入框中输入2.3,取值为2.29的问题,造成录入数据准确,最后发现是JS精度丢失的问题,手工输入小数位耗时又耗力,决定使用Selenium+Maven+chromedriver搭建自动化测试(ps:最开始打算使用Firefox浏览器进行测试,但是倒腾了很久,只能启动浏览器,不能输入网址,最后以超时结束,网上进行了查询,发现是Selenium、Firefox、geckodriver三者存在版本兼容问题,找到三者适配的版本耗时又耗力,后选择了Chrome浏览器,果断成功~)。
搭建步骤:
一、使用“Katalon Recorder”插件录制页面操作,并将操作命令导出为java文件,也可以在选择导出的时候直接复制导出的代码到编辑器中
二、创建Maven项目,在pom.xml中引入相关依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.5.1</version>
</dependency>
三、下载chromedriver.exe,chromedriver.exe需要与Chrome浏览器版本对应(可参考:https://www.cnblogs.com/it-tsz/p/11753800.html)
四、构建代码
package com.cmcc.ipb;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class App {
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
// 第三步下载下来的chromedriver.exe的安装地址
System.setProperty("webdriver.chrome.driver", "D:\\soft\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testIPB() throws Exception {
driver.get("http://10.8.8.50/#/login");
driver.findElement(By.xpath("//input[@type='text']")).click();
driver.findElement(By.xpath("//input[@type='text']")).clear();
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("admin");
driver.findElement(By.xpath("//div[@id='app']/div/div/div/div[2]/div/div[2]/div[2]/form/div[2]/div/div")).click();
driver.findElement(By.xpath("//input[@type='password']")).click();
driver.findElement(By.xpath("//input[@type='password']")).clear();
driver.findElement(By.xpath("//input[@type='password']")).sendKeys("!QAZ2wsx");
driver.findElement(By.xpath("(//input[@type='text'])[2]")).click();
driver.findElement(By.xpath("(//input[@type='text'])[2]")).clear();
driver.findElement(By.xpath("(//input[@type='text'])[2]")).sendKeys("1234");
driver.findElement(By.xpath("//button[@type='submit']")).click();
driver.findElement(By.xpath("//div[@id='app']/div/div[2]/div[2]/div/div/ul/li/div/span")).click();
driver.findElement(By.xpath("//div[4]/ul/li[3]/div")).click();
driver.findElement(By.xpath("//div[4]/ul/li[3]/div[2]/ul/li/span")).click();
driver.findElement(By.xpath("//div[@id='app']/div/div[2]/div[2]/div[2]/div/div[2]/div/div/div/div[2]/div[2]/button/span")).click();
List<String> numlist= getNum();
validate(numlist);
}
/**
* @param numlist
* 验证输入数据和取值是否一致
*/
private void validate(List<String> numlist){
String num=null;
//第一条0.00不需要,从0.01开始验证
for (int i=1;i<=numlist.size();i++) {
num=numlist.get(i);
driver.findElement(By.xpath("(//input[@type='text'])[19]")).click();
driver.findElement(By.xpath("(//input[@type='text'])[19]")).clear();
driver.findElement(By.xpath("(//input[@type='text'])[19]")).sendKeys(num);
driver.findElement(By.xpath("(//input[@type='text'])[9]")).click();
driver.findElement(By.xpath("(//input[@type='text'])[9]")).clear();
driver.findElement(By.xpath("(//input[@type='text'])[9]")).sendKeys("wo");
String value= driver.findElement(By.xpath("(//input[@type='text'])[19]")).getAttribute("value");
if(!value.equals(num)){
System.out.println("value:"+num+"======text:"+value);
}
}
}
/**
* @return
* 构造0.00-9.99之间的所有两位小数
*/
private List<String> getNum(){
List<String> numlist=new ArrayList<String>();
String num=null;
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
for (int q = 0; q <= 9; q++) {
num=i+"."+j+q;
numlist.add(num);
num=null;
}
}
}
return numlist;
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
}
五、运行代码,自动启动Chrome浏览器,控制台处输出录入值与取值不一致的数据,再根据输出结果做验证。