Question

I'm very new using TestNG and Java programming in general and I have a question in regards to using @Factory and @DataProvider together.

I want to test submitting a web form multiple times with different input data every time.

I have the following code:

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"} };

}

When I run my test cases, the data provider will always use the last values contained in the array. For example, if I run the test now it will input the values: "000008", "Stack", "Overflow" into the web form and completely ignore "000007", "Bill", "Gates".

Thanks!!

EDIT: I printed all three Strings in the function:

@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);
    }

Console:

000007 Bill Gates 000008 Stack Overflow

EDIT 2: I'll post the entire code with the test cases so you guys can take a look and see if you can spot the mistake:

 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);
}

 }

Also, the XML file for this test:

<suite name="Parameter test Suite" verbose="2"> <test name="Parameter Test one"> <classes> <class name="test.classes.SolicitudEmpleo"> </class> </classes> </test> </suite>

Was it helpful?

Solution

Running the following code:

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");
    }
}

Produces the following output:

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

Therefore, the problem is not with TestNG. It must be something with WebDriver, and probably the site you are using.

OTHER TIPS

Just to be sure, did you provide the testng.xml settings for your factory (http://beust.com/weblog/2004/09/27/testngs-factory/)?

Another way is this:

I am building my parametrized tests with the Annotation @RunWith(Parameterized.class) for my testclass and

@Parameterized.Parameters
public static Collection<Object[]> getTestData() {....}

...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top