Independent Test Methods and Tests
Tests should have a specific object and should not depend on another test. This is a very important consideration, and every test developer needs to be educated about this.
Consider this example:
@Test
public void eriBankLogin(@Optional("company") String userName, @Optional("company") String password) {
LOGGER.info("Enter eriBankLogin - " + "userName = " + userName + " password = " + password);
// Find Element commands for Find Login elements.
driver.findElement(ELEMENTS.LOGIN_USER.getBy(TYPE.ANDROID)).sendKeys(userName);
driver.findElement(ELEMENTS.LOGIN_PASS.getBy(TYPE.ANDROID)).sendKeys(password);
driver.findElement(ELEMENTS.LOGIN_BUTTON.getBy(TYPE.ANDROID)).click();
LOGGER.info("Exit eriBankLogin");
}
@Test (dataProvider = "makePaymentsData")
public void makePaymentTest(String phone, String name, String amount, String country) {
LOGGER.info("Enter makePaymentTest - Phone = " + phone + " name = "
+ name + " amount = " + amount + " country = " + country);
driver.findElement(ELEMENTS.PAYMENT_BUTTON.getBy(TYPE.ANDROID)).click();
driver.findElement(ELEMENTS.PHONE.getBy(TYPE.ANDROID)).sendKeys(phone);
driver.findElement(ELEMENTS.NAME.getBy(TYPE.ANDROID)).sendKeys(name);
driver.findElement(ELEMENTS.AMOUNT.getBy(TYPE.ANDROID)).sendKeys(amount);
driver.findElement(ELEMENTS.COUNTRY.getBy(TYPE.ANDROID)).sendKeys(country);
driver.findElement(ELEMENTS.SEND_PAYMENT_BUTTON.getBy(TYPE.ANDROID)).click();
driver.findElement(ELEMENTS.YES_BUTTON.getBy(TYPE.ANDROID)).click();
LOGGER.info("Exit makePaymentTest");
}
The test methods makePaymentsData and eriBankLogin are dependent on each other.
The test makePaymentsData assumes that eriBankLogin will be called before and hence the state of the user will be logged in. However, when tests are run in parallel or the order of the execution changes then makePaymentTests will fail.
The sample code performs login operation @BeforeMethod, which ensures it will be always called before executing any test method.
To conclude, It is of utmost importance to develop a method which is independent of each other.