Home > Back-end >  How to organise and parameterise tests for various scenarios in the same form using testng and java
How to organise and parameterise tests for various scenarios in the same form using testng and java

Time:10-05

This might be a silly question but I am looking for best practices to handle that situation, hence easy to maintenance in the future. Imagine you got few steps (pages) on your web app with a lot of fields that you need to fill in to complete. You also got few scenarios with different values to use.

What is the best way to do that?

Thanks

CodePudding user response:

Usualy people start writting test like this:

WebElement firstName = driver.findElement(By.Id("firstName"));
firstName.sendKeys("John");
WebElement lastName = driver.findElement(By.Id("lastName"));
lastName.sendKeys("Doe")
WebElement btnSubmit = driver.findElement(By.Id("btnSubmit"));
btnSubmit.click();

My (and ofc not only) practice is to create a pojo, in this supereasy example would be:

package selenium;

public class Person {

String firstName;
String lastName;

public String getFirstName() {
    return firstName;
}
public String getLastName() {
    return lastName;
}

public Person(String firstName, String lastName) {
    super();
    this.firstName = firstName;
    this.lastName = lastName;
}

}

Now you can wrap it into this:

Person person = new Person("John", "Doe");

public void register(Person person) {
    WebElement firstName = driver.findElement(By.Id("firstName"));
    firstName.sendKeys(person.getFirstName());
    WebElement lastName = driver.findElement(By.Id("lastName "));
    lastName.sendKeys(person.getLastName())
    WebElement btnSubmit = driver.findElement(By.Id("btnSubmit "));
    btnSubmit.click();
}

Using Apache POI you can prepare your test data in some XLSX file, load it as pojo and use those for your tests.

CodePudding user response:

Try to create a set of data you need your tests to enter and verify.

And use @DataProvider mechanism for that - https://www.toolsqa.com/testng/testng-dataproviders/

  • Related