简体   繁体   中英

Need to skip test cases at run time in Testng

I have to set up my selenium framework to read test cases from testrail to be run and get their id at run time and then run only those test cases.

But the problem is :

The Business Analyst team is only going to Select the test cases to be run and drag them in test run section of test rail and then wants a batch file which they can double click and selenium should start running the selected test cases.

So I can read the test cases that needs to be run using selenium from test rail, but how do I pass it to testng.xml at run time which I initiate through a batch file ?

I have multiple testng files for different applications but the selenium script is in 1 single project folder .

This is my sample testng.xml file

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

and below is my code for batch file set

 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"));

I can store the id that I am getting from above code but how do I pass it to testing at run time and then skip test cases in testing which are not present in my array ?

You can utilize TestNG listener for that purpose. Either method selector or method interceptor will best fit in this case. You can check value from custom annotation or method name with testcase id from the test rail.

For simplicity, let's assume that you have method names given as test_<testrailid> . Now in listener you can include method only if method name ends with id(s) obtain from api call. Below is example of interceptor.

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

Same way you can have method selector as well by implementing IMethodSelector . When you are implementing method selector you need to register it using method selector instead of listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM