Home » Java » java – What is the correct way to writing e2e tests?-Exceptionshub

java – What is the correct way to writing e2e tests?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have a task do create e2e for a web app, but I don’t know what is the correct way to do it.
For example:
In app there is a possibility to add new employee, so the scenario is:

  1. Login to application(it can be implemented in TestNG @BeforeClass annotation)
  2. Switch to correct role that have posibility to add employee, forward to form
  3. Filling form and submit
  4. Switch to employee tab, assert that employee exists
  5. @AfterClass teardown

In testing project I must use Selenium with TestNG. My question is what is the correct way to do this?

Option 1:


public class CreateEmployeeTest {


    @BeforeClass
    public void loginToApplication() {

        driver.get("url");
        new LoginPage(driver).login("username", "password");
    }

    @Test
    public void addNewEmployee() {
        new DashboardPage(driver)
                .clickAddEmployeeButton();

        new AddEmployeeForm(driver)
                .fillFirstName("Name")
                .fillLastName("LastName")
                .fillEmail("email")
                .submitForm();

        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.urlContains("/employees"));
        assertThat(new EmployeeDetails(driver).getEmployeeName(), equalTo(String.format("%s %s", "Name", "LastName")));
    }

    @Test(dependsOnMethods = "addNewEmployee")
    public void verifyThatEmployeeIsOnList() throws InterruptedException {
         new DashboardPage
                .switchToEmployesTab()
                .search(String.format("%s %s", "Name", "LastName"));

        Assert.assertTrue(DashboardPage.checkThatUserIsOnList(String.format("%s %s", "Name", "LastName")));
    }
}

Is it correct to create tests like this? I mean to create few @Test in one scenario with ‘dependsOnMethod’ option.

Option 2:

public class CreateEmployeeTest {


    @BeforeClass
    public void loginToApplication() {

        driver.get("url");
        new LoginPage(driver).login("username", "password");
    }

    @Test
    public void addNewEmployee() {
        new DashboardPage(driver)
                .clickAddEmployeeButton();

        new AddEmployeeForm(driver)
                .fillFirstName("Name")
                .fillLastName("LastName")
                .fillEmail("email")
                .submitForm();

        new WebDriverWait(driver, 10)
                .until(ExpectedConditions.urlContains("/employees"));
        assertThat(new EmployeeDetails(driver).getEmployeeName(), equalTo(String.format("%s %s", "Name", "LastName")));

        new DashboardPage
               .switchToEmployesTab()
               .search(String.format("%s %s", "Name", "LastName"));

        Assert.assertTrue(DashboardPage.checkThatUserIsOnList(String.format("%s %s", "Name", "LastName")));

   }
}

Option 2 is to create one @Test and write a full scenario in it. If option two is correct – what is the best way with dealing with very long tests?

Option 3:
Maybe two of examples are incorrect – your idea?

How to&Answers: