Set Priority For Selenium WebDriver TestNG @Test Method Execution

We can set test execution priority for selenium WebDriver @Test annotation methods. If there Is single @Test annotation method In your test class then you do not need to set test execution priority but supposing you have multiple @Test annotation methods In single test class then many of you may face Issue like @Test method's execution sequence Is different than the actual sequence.

You will find basic TestNG Framework Tutorials links on THIS PAGE.

Example Is as bellow.

package TestNG_Advanced;

import org.testng.annotations.Test;

public class setPriority { 
    @Test
    public void Login() {
  System.out.println("LogIn Test code.");   
    } 
    @Test
    public void checkMail() {
  System.out.println("checkMail Test code.");   
    } 
    @Test
    public void LogOut() {
  System.out.println("LogOut Test code."); 
    }
}

If you will run above test In eclipse then console output will looks like bellow.
LogOut Test code.
LogIn Test code.
checkMail Test code.

As per console output, LogOut() @Test method Is executed first, then Login() @Test method and at last checkMail() @Test method. So this Is strange and not as per my requirement. It should execute Login() @Test first, checkMail() @Test 2nd and LogOut() @Test 3rd.

TestNG has very good feature to resolve this Issue by setting priority of test. You can set priority with @Test annotation to correct test execution sequence as bellow.
package TestNG_Advanced;

import org.testng.annotations.Test;

public class setPriority {
 
    @Test(priority=1)
    public void Login() {
  System.out.println("LogIn Test code.");   
    } 
 
    @Test(priority=2)
    public void checkMail() {
  System.out.println("checkMail Test code.");   
    }
 
    @Test(priority=3)
    public void LogOut() {
  System.out.println("LogOut Test code."); 
    } 

}

If you will run above code snippet In eclipse then console output will looks like bellow.
LogIn Test code.
checkMail Test code.
LogOut Test code.

So now, Test execution sequence Is correct and as per my requirement. If you see In example, We set test execution priority for all three @Test methods.

This way you can set test execution priority for each and every @Test method of your class.

No comments:

Post a Comment