Wednesday, 6 February 2019

Run Test Sequentially in Selenium Testng

There are multiple ways to run test sequentially in Selenium using TestNG
  • Defining each test in order in XML
  • Using 'Priority' attribute 

Lets say we have 4 test in a class as shown below. By default test will run in an unpredictable order.
package com.example;

import org.testng.annotations.Test;

public class SequentialTest {

 @Test
 public void firstTest() {
  System.out.println("1");
 }

 @Test
 public void secondTest() {
  System.out.println("2");
 }

 @Test
 public void thirdTest() {
  System.out.println("3");
 }
 
 @Test
 public void fouthTest() {
  System.out.println("4");
 }
}

How can we run test sequentially using TestNG in Selenium


Testng.XML


Using Testng.xml we can run test sequentially. By default, TestNG will run your tests in the order they are found in the XML file. If you want the classes and methods listed in this file to be run in an unpredictable order, set the preserve-order attribute to false.

By default preserve-order is true.


@Test (priority=" ")


Using 'priority' attribute we can run test sequentially using TestNG in Selenium. We can define priority for each test methods. Lower priorities will be scheduled first.

No comments:

Post a Comment