需要在运行时在Testng中跳过测试用例

时间:2019-03-29 09:15:06

标签: java selenium-webdriver testng testrail

我必须设置我的Selenium框架才能从中读取测试用例  要运行的testrail并在运行时获取其ID,然后仅运行  这些测试用例。

但是问题是:

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

因此,我可以从中读取需要使用硒运行的测试用例  测试导轨,但是如何在运行时将其传递给try  通过批处理文件启动吗?

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

这是我的示例 testng.xml文件

catch

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

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 -->

我可以存储从上述代码中获取的ID,但如何传递  在运行时进行测试,然后跳过测试中的测试用例  不在我的数组中吗?

1 个答案:

答案 0 :(得分:0)

您可以为此目的使用TestNG侦听器。在这种情况下,方法选择器或method interceptor都是最合适的。您可以通过测试栏上的自定义注解或方法名和测试用例ID来检查值。

为简单起见,我们假设您使用的方法名称为test_<testrailid>。现在,在侦听器中,仅当方法名称以从api调用获取的ID结尾时,才可以包含方法。以下是拦截器的示例。

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,同样可以使用方法选择器。在实现方法选择器时,需要使用方法选择器而不是侦听器进行注册。

相关问题