【问题标题】:Need to skip test cases at run time in Testng需要在 Testng 运行时跳过测试用例
【发布时间】:2019-03-29 09:15:06
【问题描述】:

我必须设置我的 selenium 框架来读取测试用例 要运行的 testrail 并在运行时获取它们的 id,然后只运行 那些测试用例。

但问题是:

业务分析师团队只会选择测试用例 运行并将它们拖到测试轨道的测试运行部分,然后想要一个 他们可以双击的批处理文件,硒应该启动 运行选定的测试用例。

所以我可以阅读需要使用 selenium 运行的测试用例 测试轨道,但我如何在运行时将它传递给testng.xml 通过批处理文件启动?

我有多个针对不同应用程序的 testng 文件,但是 selenium 脚本位于 1 个单个项目文件夹中。

这是我的示例 testng.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
  <test name="Test">
    <classes>
      <class name="com.SalesForce.Testone" />
      <class name="com.SalesForce.Testtwo" />
      <class name="com.SalesForce.Testthree" />
    </classes>
  </test>
  <!-- Test -->
</suite>
<!-- Suite -->

以下是我的批处理文件集代码

 projectLocation=H:\Automation\SF\AutomatedTestCases\usingSelnium\runFromTestRail\CAanzAutomation
 cd %projectLocation% set
 classpath=%projectLocation%\bin;%projectLocation%\resources\* java
 org.testng.TestNG %projectLocation%\testng.xml pause
 APIClient client = new APIClient("https://abc.testrail.io/");
 client.setUser("email id");
 client.setPassword("password");
 JSONObject c = (JSONObject) client.sendGet("get_case/4");
 System.out.println(c.get("id"));

我可以存储我从上面的代码中获得的 id,但是我该如何传递 它在运行时进行测试,然后在测试中跳过测试用例 不在我的数组中?

【问题讨论】:

    标签: java selenium-webdriver testng testrail


    【解决方案1】:

    您可以为此目的使用 TestNG 侦听器。方法选择器或method interceptor 最适合这种情况。您可以使用测试轨道中的测试用例 id 从自定义注释或方法名称中检查值。

    为简单起见,我们假设您的方法名称为test_&lt;testrailid&gt;。现在在侦听器中,只有当方法名称以从 api 调用获取的 id(s) 结尾时,您才能包含方法。下面是拦截器的示例。

    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
    
      APIClient client = new APIClient("https://abc.testrail.io/");
      client.setUser("email id");
      client.setPassword("password");
      JSONObject c = (JSONObject) client.sendGet("get_case/4");
      String id = "_"+c.get("id");
    
      List<IMethodInstance> result = new ArrayList<IMethodInstance>();
    
      for (IMethodInstance m : methods) {
        if (m.getMethod().getMethodName().endsWith(id)) {
          result.add(m);
        }
      }
      return result;
    }
    

    通过实现IMethodSelector,您也可以拥有方法选择器。当你实现方法选择器时,你需要使用方法选择器而不是监听器来注册它。

    【讨论】:

    • 感谢您的回复,我会尝试并通知您
    • 嗨@user861594,我已经编辑了你的方法,但知道如何调用它。我已经发布了我的答案
    猜你喜欢
    • 2015-03-21
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    相关资源
    最近更新 更多