【问题标题】:How to execute multiple test cases using testNG如何使用 testNG 执行多个测试用例
【发布时间】:2019-10-23 21:09:33
【问题描述】:

我想了解我们如何使用 testNg 执行多个测试用例。假设我的 Web 应用由 10 个页面组成。那么我们如何执行以下测试用例。

1) 1st TC- 遍历或导航到第 1、2、3、4、5 和 6 页。

2) 2nd TC- 遍历或导航到第 1,2,3,8,9&10 页。

3) 3rd TC- 遍历或导航到第 1、2、6、7、8 页。

所有页面都有相应的优先级。第 1 页的优先级为 1,第 2 页的优先级为 2,依此类推。

是不是我们需要在每个@Test注解中调用相关的方法(每个页面中定义的方法。)。

谢谢!

【问题讨论】:

    标签: selenium testng


    【解决方案1】:

    似乎每个页面都有一个 @Test 注释方法。如果它们可以按照您提到的逻辑正确遍历,那么要为您的测试用例运行它们,我将从 TC 中删除优先级并将此 xml 与 preserve-order="true" 一起使用,因此它们以相同的顺序运行。您可以以相同的方式将更多 TC 添加到下面。看看this

    下面的xml会按顺序调用方法,你需要确保它们可以正确的进入你的页面

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="yourSuiteName" >
    
        <test name="1stTC" preserve-order="true">
    
            <classes>  
             <class name="yourPackage.YourClass" >
                   <methods> 
                      <include name="method1" />
                      <include name="method2" /> 
                      <include name="method3" />
                      <include name="method4" />
                       <include name="method5" />
                      <include name="method6" />
                   </methods> 
              </class>
           <classes> 
          </test>
    
    <test name="2ndTC" preserve-order="true"> 
           <classes>         
               <class name="yourPackage.YourClass">
                   <methods> 
                      <include name="method1" />
                      <include name="method2" /> 
                      <include name="method3" />
                      <include name="method8" /> 
                      <include name="method9" />
                      <include name="method10" />
                   </methods>
              </class>
            </classes>
        </test>
    </suite>
    

    【讨论】:

      【解决方案2】:

      如果您有多个页面,那么您可以为每个网页设置 Page 对象类。该类可以有多个方法,这些方法实现为可以在该页面上执行的操作,并且可以返回它正在导航到的下一页的对象。使用 Page 对象类的此类对象及其方法,您可以设计将被视为测试用例的测试方法。

      例如 - 对于登录页面,使用页面上所有必需的元素定义 Login.java 并定义方法如下

      public Homepage loginAction(String Username, String Password){
            // write code to perform login opeartion
            // it returns Homepage object that you can store in Homepage type variable and you can call other operations of Homepage on that object.
      }
      

      一旦您准备好使用所有 Page 对象类,您就可以通过调用 tat 方法编写测试用例。

      例如:

      @Test
      public void TestCaseOne(){
            Login loginpage = new Login();
            Homepage homepage = loginpage.loginAction("ABC","XYZ");
            homepage.selectAcc(1);
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-17
        • 1970-01-01
        • 2022-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-16
        • 2016-03-04
        • 2016-10-24
        相关资源
        最近更新 更多