TestNG - selenium 脚本中的测试执行顺序 [英] TestNG - Order of Tests execution in selenium script

查看:24
本文介绍了TestNG - selenium 脚本中的测试执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium 3.8.1 和 TestNG 6.9.2 版本,在完成 @Test 方法之前测试执行另一个 @Test 方法开始,因此我在完成测试用例后在 selenium 脚本中遇到错误执行.

一类

公共类 LoginPage{@测试(优先级=0)公共无效测试1(){System.out.println(第一次测试);}@测试(优先级=1)公共无效测试2(){System.out.println(第二次测试);}}

二级

公共类主页{@测试(优先级=0)公共无效测试3(){System.out.println(第一次测试);}@测试(优先级=1)公共无效测试4(){System.out.println(第二次测试);}}

testng.xml

在完成登录页面类的 test2 之前使用 testng.xml 文件执行上述后,test3 是 HomePage 的开始,因此我得到异常,无法找到元素.

解决方案

所以很明显,实际的顺序是:

test1() ->test2() ->test3() ->测试4()

<小时>

琐事

您可以使用 testng-results.xml 进行更细致的观察,如下所示:

<?xml version="1.0" encoding="UTF-8"?><testng-results skipped="0" failed="0" ignore="0" total="4" passed="4"><报告者输出></reporter-output><suite name="Suite" duration-ms="61" started-at="2017-12-25T12:57:12Z" finished-at="2017-12-25T12:57:12Z"><组></组><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" 完成-at="2017-12-25T18:27:12Z"><报告者输出></reporter-output></测试方法><!-- 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" 完成-at="2017-12-25T18:27:12Z"><报告者输出></reporter-output></测试方法><!-- 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" 完成-at="2017-12-25T18:27:12Z"><报告输出></reporter-output></测试方法><!-- 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" 完成-at="2017-12-25T18:27:12Z"><报告者输出></reporter-output></测试方法><!-- test2 --></class><!-- testng_order_of_tests_execution.LoginPage --></测试><!-- 测试--></套房><!-- 套房 --></testng-results>

testng-results.xml 中,您会看到所有测试从 2017-12-25T12:57:12Z 开始,到 2017-12 结束-25T12:57:12Z.尽管 Test Execution 所用的时间甚至不到 1 秒,您仍然可以观察到实例名称的差异,例如 instance:testng_order_of_tests_execution.HomePage@5419f379instance:testng_order_of_tests_execution.LoginPage@735b5592.由于我们的测试是一个单线程测试,因此我们可以得出结论,执行顺序是正确的,符合预期.但是控制台输出搞混了.

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.

解决方案

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.

这篇关于TestNG - selenium 脚本中的测试执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆