Earlier in previous post, We have learnt how to set test execution priority of WebDriver test cases If you have multiple tests In same class. TestNG has one more very useful feature of setting test execution dependency. Depends on method in testng means If one @Test method fails or skipped from execution then It's dependent @Test method must not be executed. So you can set test execution dependency using dependsonmethods in selenium.
Using depends on method in testng Selenium
Supposing you are testing online email application using selenium WebDriver and you have two @Test methods In your class : 1) For LogIn and 2) For Check Mail. Now due to the some reasons (Invalid credentials), LogIn @Test method fails. In this case you not need to execute Check Mail @Test method because how can you check emails without LogIn In email application? In this case, You can use testng dependsOnMethods with @Test annotation of Check Mail method to skip Its execution.
I have created simple example as bellow to demonstrate how It dependsonmethods testng works.
Example of testng dependsonmethods:
package TestNG_Advanced;
import org.testng.Assert;
import org.testng.annotations.Test;
public class setPriority {
//This Is Independent method so It will be executed.
@Test(priority=1)
public void Login() {
System.out.println("LogIn Test code.");
//Bellow give assertion will fail to fail Login() method Intentionally.
Assert.assertTrue(5>6, "Condition Is False.");
}
//This method Is depends on Login method.
//This method's execution will be skipped from execution because Login() method will fail.
@Test(priority=2, dependsOnMethods={"Login"})
public void checkMail() {
System.out.println("checkMail Test code.");
}
//This method Is depends on Login and checkMail methods.
//This method's execution will be skipped from execution because Login() method will fail.
@Test(priority=3,dependsOnMethods={"Login","checkMail"})
public void LogOut() {
System.out.println("LogOut Test code.");
}
//This Is Independent method so It will be executed.
@Test(priority=4)
public void checkLogInValidations() {
System.out.println("checkLogInValidations Test code.");
}
}
TestNg result will looks like bellow for dependsonmethods testng test.
- LogIn() method Is failed because we have written wrong condition In assertion.
- checkMail() and LogOut() methods are skipped from execution because they are depends on LogIn() method which Is failed.
- checkLogInValidations() method will be executed because It Is Independent.
So this way, We can set dependency on @Test methods to skip Its execution If It's dependsonmethods in selenium fails.
No comments:
Post a Comment