Use Of preserve-order In TestNG With Selenium WebDriver

preserve-order Is very Important attribute In TestNG. In Selenium WebDriver test execution, Many times peoples are complaining like my test cases execution not runs In correct sequence as given In testng.xml file but they are being executed In random sequence(unpredictable order). Actually they have copy pasted testng.xml file from somewhere with attribute preserve-order="fasle" but they don't know meaning of this attribute. Let me try to explain you meaning of this attribute.

  1. In testng.xml file, If you have not set preserve-order attribute with <test> node, By default It will be true. So your test class execution order will remain same as you have given In testng.xml file.
  2. Sameway, If you set attribute preserve-order="true" with <test> node, Your test execution will be same as given In testng.xml file.
  3. But If you set preserve-order="false", Your test cases will be executed In unpredictable order.
Let me give you examples with preserve-order="true" and preserve-order="false" attribute.

You can find more tutorial links on testng with webdriver at PAGE 1 and PAGE 2

Create three test cases as bellow under package Testing_Pack.

1. Test_One.java
package Testing_Pack;

import org.testng.annotations.Test;

public class Test_One {
 @Test
 public void testCaseOne_ClassOne() {
  System.out.println("Executing testCaseOne_ClassOne Of ClassOne");
 }
}

2. Test_Two.java
package Testing_Pack;

import org.testng.annotations.Test;

public class Test_Two {
 @Test
 public void testCaseOne_ClassTwo() {
  System.out.println("Executing testCaseOne_ClassTwo Of ClassTwo");
 }
}

3. Test_Three.java
package Testing_Pack;

import org.testng.annotations.Test;

public class Test_Three {
 
 @Test
 public void testCaseOne_ClassThree() {
  System.out.println("Executing testCaseOne_ClassThree Of ClassThree");
 }
}

Create bellow given testng.xml file under your project to run all above test cases.

testng.xml (with preserve-order="false")
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="preserve-order Suite">
  <test name="preserve-order Test" preserve-order="false">
    <classes>
      <class name="Testing_Pack.Test_One"/>
      <class name="Testing_Pack.Test_Two"/>
      <class name="Testing_Pack.Test_Three"/>
    </classes>
  </test>
</suite>

If you will run above given testng.xml file, test classes execution order will looks like bellow. You can see that test execution sequence Is not same as given In testng.xml file.


Now let us use preserve-order="true" attribute In testng.xml file as bellow and observe execution result.

testng.xml (with preserve-order="true")
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="preserve-order Suite">
  <test name="preserve-order Test" preserve-order="true">
    <classes>
      <class name="Testing_Pack.Test_One"/>
      <class name="Testing_Pack.Test_Two"/>
      <class name="Testing_Pack.Test_Three"/>
    </classes>
  </test>
</suite>

Now run above testng.xml file and observe test classes execution order. It will be same as given In testng.xml file as shown In bellow given Image.


So this Is the example of using preserve-order attribute In testng.xml file. To execute test cases In correct order, you need to set preserve-order=true and to execute them In unpredictable order, set preserve-order=false.

2 comments:

  1. Thanks for the post, Interesting and very useful article to learn about Preserve in Testing.

    ReplyDelete
  2. even if using preserve order classes were not running in order is there any other method we can use

    ReplyDelete