TestNG - Order of Tests execution in selenium script

亡梦爱人 提交于 2021-02-08 08:21:42

问题


I'm using selenium 3.8.1 and TestNG 6.9.2 version,while test execution before completing the @Test method another @Test method is starts,because of this i'm getting error in selenium script After completion of Test Cases execution.

One Class

public class LoginPage{


@Test(priority=0)
public void test1(){

System.out.println(first test);
}


@Test(priority=1)
public void test2(){

System.out.println(Second test);
}

}

Second Class

public class HomePage{


@Test(priority=0)
public void test3(){

System.out.println(first test);
}

@Test(priority=1)
public void test4(){

System.out.println(Second test);
}

}

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" preserve-order="true">
    <classes>
      <class name="com.tests.day.modules.LoginPage"/>
      <class name="com.tests.day.modules.HomePage"/>    
    </classes>
  </test>
</suite>

After Executing the above using testng.xml file before completing the test2 of login page class,test3 is starting of HomePage,because of this i'm getting exception,Unable to Find the Elements.


回答1:


The Annotations mentions about the preserve-order attribute of TestNG as follows:

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

I executed the same test similar to your code block and testng.xml as follows :

  • LoginPage

    package testng_order_of_tests_execution;
    
    import org.testng.annotations.Test;
    
    public class LoginPage 
    {
    
        @Test(priority=0)
        public void test1(){
    
        System.out.println("First Test");
        }
    
    
        @Test(priority=1)
        public void test2(){
    
        System.out.println("Second Test");
        }
    }
    
  • HomePage

    package testng_order_of_tests_execution;
    
    import org.testng.annotations.Test;
    
    public class HomePage 
    {
    
        @Test(priority=0)
        public void test3(){
    
        System.out.println("first test");
        }
    
        @Test(priority=1)
        public void test4(){
    
        System.out.println("second test");
        }
    }
    
  • testng.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite">
      <test name="Test" preserve-order="true">
        <classes>
          <class name="testng_order_of_tests_execution.LoginPage"/>
          <class name="testng_order_of_tests_execution.HomePage"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->
    

What I found as an output on my console was similar to yours as follows :

First Test
first test
Second Test
second test

This Console Output apparently gives us an impression that the sequence of execution was :

test1() -> test3() -> test2() -> test4()

But actually No

Looking at the Result of running suite you will get the actual sequence of execution as per the figure below :

So it's pretty clear that the actual sequence was :

test1() -> test2() -> test3() -> test4()

Trivia

You can be more granular in your observation with the testng-results.xml which is as follows :

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="0" ignored="0" total="4" passed="4">
  <reporter-output>
  </reporter-output>
  <suite name="Suite" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
    <groups>
    </groups>
    <test name="Test" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z">
      <class name="testng_order_of_tests_execution.HomePage">
        <test-method status="PASS" signature="test3()[pri:0, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test3" duration-ms="4" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test3 -->
        <test-method status="PASS" signature="test4()[pri:1, instance:testng_order_of_tests_execution.HomePage@5419f379]" name="test4" duration-ms="1" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test4 -->
      </class> <!-- testng_order_of_tests_execution.HomePage -->
      <class name="testng_order_of_tests_execution.LoginPage">
        <test-method status="PASS" signature="test1()[pri:0, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test1" duration-ms="14" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test1 -->
        <test-method status="PASS" signature="test2()[pri:1, instance:testng_order_of_tests_execution.LoginPage@735b5592]" name="test2" duration-ms="2" started-at="2017-12-25T18:27:12Z" finished-at="2017-12-25T18:27:12Z">
          <reporter-output>
          </reporter-output>
        </test-method> <!-- test2 -->
      </class> <!-- testng_order_of_tests_execution.LoginPage -->
    </test> <!-- Test -->
  </suite> <!-- Suite -->
</testng-results>

In testng-results.xml you will observe that all the tests starts at 2017-12-25T12:57:12Z and ends at 2017-12-25T12:57:12Z. Though the time taken for Test Execution is even less then 1 second still you may observe the difference in the instancename as instance:testng_order_of_tests_execution.HomePage@5419f379 and instance:testng_order_of_tests_execution.LoginPage@735b5592. As our test was a single threaded test, hence we can conclude that the sequence of execution was proper and as per expectation. But the Console Output got mixed up.




回答2:


Use group-by-instances="true" inside test tag of the testng.xml

Define your xml test tag like below:

<test name="Test" group-by-instances="true">

Or, you can also check below line of code:

<test name="Test" preserve-order="true" group-by-instances="true">


来源:https://stackoverflow.com/questions/47922782/testng-order-of-tests-execution-in-selenium-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!