@DataProvider
注解对参数优化通过注解把业务参数从代码中分离出来,这样做有以下优点:
每一组测试数据可以针对同一个测试点进行不同场景测试;
public class SeleniumTest1 {
WebDriver webDriver;
@BeforeMethod
public void beforMethod() throws IOException {
// 业务无关,启动配置相关代码
String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
// 使用注解,参数化用例
@DataProvider(name = "selenium_data1")
public Iterator<Object[]> dataProvider() {
Set<Object[]> set = new HashSet<Object[]>();
// 通过添加不同数据可以对一个测试点进行一组测试用例的测试
set.add(new String[] { "zhangsan", "password", "true" });
set.add(new String[] { "lisi", "password", "false" });
return set.iterator();
}
@Test(dataProvider = "selenium_data1") //指定使用的数据驱动是selenium_data1;
public void test(String username, String password, String expected) // 参数是对应的set中每个单元的值
{
webDriver.get("https://www.baidu.com/");
webDriver.manage().window().maximize();
webDriver.findElement(By.xpath("//*[@id='u1']/a[7]")).click();
webDriver.findElement(By.id("TANGRAM__PSP_10__userName")).sendKeys(username);
webDriver.findElement(By.id("TANGRAM__PSP_10__password")).sendKeys(password);
webDriver.findElement(By.id("TANGRAM__PSP_10__verifyCode")).sendKeys("验证码");
webDriver.findElement(By.id("TANGRAM__PSP_10__submit")).click();
boolean result = webDriver.findElement(By.id("user-name")).getText().contains(username);
if (expected.equals("true")) {
assert result;
} else {
assert !result;
}
}
@AfterMethod
public void afterMethod() {
// 业务无关代码
webDriver.quit();
}
}
抽出 Elements 定位使用的变量:
单独的类存放定位参数:
public class HomePage {
public static final String byXpathButtonLogin = "//*[@id='u1']/a[7]";
public static final String byIdInputName = "TANGRAM__PSP_10__userName";
public static final String byIdInputPassword = "TANGRAM__PSP_10__password";
public static final String byIdInputVerifyCode = "TANGRAM__PSP_10__verifyCode";
public static final String byIdButtonSubmit = "TANGRAM__PSP_10__submit";
public static final String byIdTextName = "user-name";
}
public class SeleniumTest1 {
WebDriver webDriver;
@BeforeMethod
public void beforMethod() throws IOException {
// 业务无关,启动配置相关代码
String driverPath = new File("./").getCanonicalPath() + "/src/main/resources/driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", driverPath);
webDriver = new ChromeDriver();
webDriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test(dataProvider = "selenium_data1")
public void test(String username, String password, String expected) {
webDriver.get("https://www.baidu.com/");
webDriver.manage().window().maximize();
webDriver.findElement(By.xpath(HomePage.byXpathButtonLogin)).click();
webDriver.findElement(By.id(HomePage.byIdInputName)).sendKeys(username);
webDriver.findElement(By.id(HomePage.byIdInputPassword)).sendKeys(password);
webDriver.findElement(By.id(HomePage.byIdInputVerifyCode)).sendKeys("验证码");
webDriver.findElement(By.id(HomePage.byIdButtonSubmit)).click();
boolean result = webDriver.findElement(By.id(HomePage.byIdTextName)).getText().contains(username);
if (expected.equals("true")) {
assert result;
} else {
assert !result;
}
}
// set的每一个数据迭代就相当于一个用例
@DataProvider(name = "selenium_data1")
public Iterator<Object[]> dataProvider() {
Set<Object[]> set = new HashSet<Object[]>();
// 通过添加不同数据可以对一个测试点进行一组测试用例的测试
set.add(new String[] { "zhangsan", "password", "true" });
set.add(new String[] { "lisi", "password", "false" });
return set.iterator();
}
@AfterMethod
public void afterMethod() {
// 业务无关代码
webDriver.quit();
}
}
这样一个大致的 UI 自动化算是完成了,还存在一些问题