@DataProvider(name = "userinfo")
public Object[][] createData1() {
return new Object[][] {
{ "test@126.com", "123444" },
{ "Anne", "test" },
};
}
如上,简历了一个 userinfo的dataprovider,更多信息可以从官网获取: https://testng-docs.readthedocs.io/fulldoc/#parameters-with-dataproviders
testdata建好了,那么就开始使用了(在方法前面写上 @Test 并且写上dataprovider的名字),具体使用方法可以参考以上链接官方文档给出的讲解
@Test(dataProvider = "userinfo")
public void loginTest(String username, String password) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, timeout);
driver.get(prop.getProperty("testurl"));
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("load-bg")));
driver.findElement(By.xpath(prop.getProperty("email_field"))).sendKeys(username);
// 等待遮挡元素消失
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("load-bg")));
// 等待并点击下一步按钮
WebElement nextBtn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("next_btn"))));
nextBtn.click();
// 等待密码字段可见
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(prop.getProperty("pwd_field"))));
driver.findElement(By.xpath(prop.getProperty("pwd_field"))).sendKeys(password);
// 再次等待遮挡元素消失
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("load-bg")));
// 再次等待并点击下一步按钮
WebElement nextBtn2 = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(prop.getProperty("next_btn"))));
nextBtn2.click();
}
考虑到如果数据更多,那应该怎么办? 所以我们把这些测试数据写在测试文件里。比如Excel文件。
如果使用文件数据驱动的话,那么我们还需要添加依赖,我列出了如下依赖列表:
如上, 我们需要获取这些依赖,怎么获取? 上几个笔记里也也写到了可以到Maven的repository里获取 直接搜索 Maven repository 到官方文档: (https://mvnrepository.com/)
如第一个poi,可以直接在官网上搜poi 并且获取对应的dependency代码
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.5</version>
</dependency>
同理 获取其他几个dependency的代码 并添加到pom.xml里