我一般都非常习惯使用TestNG和Java编程,我对使用@Factory和@DataProvider有疑问。
我想测试每次使用不同的输入数据多次提交网络表单。
我有以下代码:
public class SolicitudEmpleo extends LaunchBrowser {
private String campoDni;
private String firstName;
private String lastName;
@Factory (dataProvider="dataProviderMethod")
public SolicitudEmpleo(String campoDni, String firstName, String lastName){
this.campoDni=campoDni;
this.firstName = firstName;
this.lastName = lastName;
}
@DataProvider
public static Object[][] dataProviderMethod() {
return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };
}
当我运行测试用例时,数据提供程序将始终使用数组中包含的最后一个值。例如,如果我现在运行测试,它将在Web表单中输入值:“000008”,“Stack”,“Overflow”并完全忽略“000007”,“Bill”,“Gates”。
谢谢!
编辑:我在函数中打印了所有三个字符串:@Factory (dataProvider="dataProviderMethod")
public SolicitudEmpleo(String campoDni, String firstName, String lastName){
this.campoDni=campoDni;
this.firstName = firstName;
this.lastName = lastName;
System.out.println(campoDni);
System.out.println(firstName);
System.out.println(lastName);
}
控制台:
000007 法案 盖茨 000008 堆 溢出
编辑2:我将用测试用例发布整个代码,这样你们就可以看看你是否能发现错误:
public class SolicitudEmpleo extends LaunchBrowser {
private String campoDni;
private String firstName;
private String lastName;
@Factory (dataProvider="dataProviderMethod")
public SolicitudEmpleo(String campoDni, String firstName, String lastName){
this.campoDni=campoDni;
this.firstName = firstName;
this.lastName = lastName;
System.out.println(campoDni);
System.out.println(firstName);
System.out.println(lastName);
}
@DataProvider
public static Object[][] dataProviderMethod() {
return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };
}
@Test (priority = 1)
public void testCase1() throws InterruptedException {
Thread.sleep(3000);
WebElement addButton = driver.findElement(By.xpath("//*[@id='pt1:r1:0:b3_2']/a/span"));
addButton.click();
}
@Test (priority = 2)
public void testCase2() throws InterruptedException {
Thread.sleep(5000);
Select dropdown = new Select (driver.findElement(By.id("pt1:r1:1:s2:soc1A::content")));
dropdown.selectByVisibleText("Dropdown");
Thread.sleep(5000);
WebElement campoDNI = driver.findElement(By.xpath("//*[@id='pt1:r1:1:s2:it1::content']"));
campoDNI.sendKeys(campoDni);
WebElement verifyButton = driver.findElement(By.xpath("//*[@id='pt1:r1:1:s2:b1']/span"));
verifyButton.click();
Thread.sleep(5000);
}
@Test (priority = 3)
public void testCase3() throws InterruptedException {
WebElement firstNameElement = driver.findElement(By.xpath("//*[@id='pt1:r1:2:it2::content']"));
firstNameElement.sendKeys(firstName);
WebElement lastNameElement = driver.findElement(By.xpath("//*[@id='pt1:r1:2:it3::content']"));
lastNameElement.sendKeys(lastName);
}
@Test (priority = 4)
public void testCase4() throws InterruptedException {
Thread.sleep(2000);
WebElement saveFormButton = driver.findElement(By.xpath("//*[@id='pt1:r1:2:b12']/a/span"));
saveFormButton.click();
Thread.sleep(6000);
}
}
public class LaunchBrowser {
WebDriver driver;
@BeforeSuite()
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Users/user/Documents/selenium-2.40.0/chromedriver");
driver = new ChromeDriver();
driver.get("http://webform.jsf");
driver.findElement(By.id("pt1:pt_s1:itUsuario::content")).sendKeys("username");
driver.findElement(By.id("pt1:pt_s1:itClave::content")).sendKeys("password");
driver.findElement(By.id("pt1:pt_s1:itClave::content")).sendKeys(Keys.ENTER);
Thread.sleep(5000);
}
}
此外,此测试的XML文件:
<suite name="Parameter test Suite" verbose="2">
<test name="Parameter Test one">
<classes>
<class name="test.classes.SolicitudEmpleo">
</class>
</classes>
</test>
</suite>
答案 0 :(得分:3)
运行以下代码:
public class SolicitudEmpleo extends LaunchBrowser{
private String campoDni;
private String firstName;
private String lastName;
@Factory (dataProvider="dataProviderMethod")
public SolicitudEmpleo(String campoDni, String firstName, String lastName){
this.campoDni=campoDni;
this.firstName = firstName;
this.lastName = lastName;
System.out.println("data number"+campoDni);
}
@DataProvider
public static Object[][] dataProviderMethod() {
return new Object[][] { {"000007", "Bill", "Gates"}, {"000008", "Stack", "Overflow"} };
}
@Test (priority = 1)
public void testCase1(){
System.out.println("Test 1");
}
@Test (priority = 2)
public void testCase2() throws InterruptedException {
System.out.println("Test 2 "+campoDni);
}
@Test (priority = 3)
public void testCase3() throws InterruptedException {
System.out.println("Test 3 "+firstName+","+lastName);
}
@Test (priority = 4)
public void testCase4() throws InterruptedException {
System.out.println("Test 4");
}
}
public class LaunchBrowser {
@BeforeSuite()
public void launchBrowser() throws InterruptedException {
System.out.println("Launching");
}
}
产生以下输出:
data number000007
data number000008
Launching
Test 1
Test 1
Test 2 000007
Test 2 000008
Test 3 Bill,Gates
Test 3 Stack,Overflow
Test 4
Test 4
因此,问题不在于TestNG。它必须是WebDriver,可能是你正在使用的网站。
答案 1 :(得分:0)
可以肯定的是,您是否为工厂提供了testng.xml设置(http://beust.com/weblog/2004/09/27/testngs-factory/)?
另一种方式是:
我正在使用注释@RunWith(Parameterized.class)
为我的测试类和
@Parameterized.Parameters
public static Collection<Object[]> getTestData() {....}
...