0

I've been trying to retrieve some parameters from TestNG XML suite. A parameter with a testrail reference is attached to xml suites that are supposed to fail with a known bug. I want to include this parameter in a generated report (we use Extent Reporting for that). I've tried numerous ways of retrieving parameters from xml suites but the number of returned parameters is always zero.

I've tried to retrieve data from xml file by using ITestContext.

flush() method is called in one of the listeners upon completion of a test and accepts ITestContext parameter. I'm trying to retrieve parameters from provided ITestContext argument.

  public void flush( ITestContext testContext ) {

Map<String, String> parameters = ( ( (ITestContext) testContext ).getCurrentXmlTest() )
      .getAllParameters();

    for ( Map.Entry<String, String> entry : parameters.entrySet() ) {
      warn( "testrail_case_id: " + entry.getKey() + " (id): " + entry.getValue() );
    }
    extent.flush();
}

This is example of xml suite where we specify a parameter (sometimes a few) with a reference to a bug ticket. To be more specific - I'm trying to retrieve a variable with a parameter name testrail_case_id

<suite name="MySuite" parallel="false" thread-count="1"
       junit="false" verbose="1">

  <listeners>
    <listener class-name="MyListener"/>
  </listeners>

  <test name="MyTest">
    <parameter name="testrail_case_id" value="684342"/>
    <classes>
      <class name="MyTestClass"/>
    </classes>
  </test>
</suite>

The problem is that Map<String, String> parameters = ( ( testContext ).getCurrentXmlTest() ).getAllParameters(); always returns zero parameters (not null).

How can I retrieve a parameter with testrail_case_id from xml suite?

  • I don't know if there is any relation between your problem and what's discussed in the following question : https://stackoverflow.com/questions/37132322/testng-getcurrentxmltest-getallparameters-api-using-very-old-testng-5-4-j It seems like one of the answers suggests calling " (((TestRunner)context).getTest()).getParameters(); " to get the parameters. Depending on your version of TestNG, Could you try and see if it works at all. – AntiqTech Jun 03 '19 at 20:50
  • Thank you. Tried. Doesn't seem to work – Roman Bostonian Jun 03 '19 at 21:13
  • I see :/ I'm checking documentation and example projects. I'll comment here in case I find anything. – AntiqTech Jun 03 '19 at 21:46
  • 1
    Hi there again, I tried to recreate your problem. I created a new empty project with TestNG with a suit xml. Created two configuraitons, one running class directly and the other one running the test through the suite. Result is that getAllParameters() returns zero when you run the the class directly, return the parameters when run through the suit xml. You can see the results from the composite ss on the following link. My guess is that somehow your configuration either run class directly or if suit is selected, it bypasses or can't access the suit file (unlikely): https://ibb.co/8DL99nH – AntiqTech Jun 06 '19 at 13:02
  • 1
    Addendum: When I try to get the suit file name got these results : Directly running class: C:\Users\****\AppData\Local\Temp\testng-eclipse-300667166\testng-customsuite.xml Running through Suit: D:\eclipse-workspace2\TestNGExamples\src\tester So it seems like If configuration is not set to use a specific suit file, it will use a temporarily created suit xml. – AntiqTech Jun 06 '19 at 13:10
  • @AntiqTech You solved the problem! It does seem like tests should be run from xml suite, not class itself. A good example in the link as well! (If you'll post your solution as a separate answer, not a comment, I'll be able to select it as a right answer) – Roman Bostonian Jun 06 '19 at 18:10

2 Answers2

1

if you want to get only one parameter instead of Map try like

  String browser = context.getCurrentXmlTest().getParameter("Browser");

which works well for me

murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

I tried to recreate your problem. I created a new empty project with TestNG with a suit xml. I created two configurations, one running class directly and the other one running the test through the suite. The result is that getAllParameters() returns zero when you run the the class directly, return the parameters when it is run through the suit xml.

My guess is that somehow your configuration either run class directly or if suit is selected, it bypasses or can't access the suit file (unlikely).

When I try to get the suit file name got these results :

Directly running class: C:\Users****\AppData\Local\Temp\testng-eclipse-300667166\testng-customsuite.xml

Running through Suit: D:\eclipse-workspace2\TestNGExamples\src\tester So it seems like If configuration is not set to use a specific suit file, it will use a temporarily created suit xml.

Example code :

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;

import java.util.Map;

import org.testng.ITestContext;
import org.testng.annotations.AfterTest;

public class MyFirstTest {
  @Test
public void f(ITestContext testContext) {
  Map<String, String> parameters = ( ( (ITestContext) testContext     ).getCurrentXmlTest() )
          .getAllParameters();
  System.out.println("file name : " +( ( (ITestContext) testContext ).getCurrentXmlTest() ).getSuite().getFileName() );
  System.out.println("parameters size "+parameters.size() );

  for ( Map.Entry<String, String> entry : parameters.entrySet() ) {
      System.out.println( "testrail_case_id: " + entry.getKey() + " (id): " + entry.getValue() );
    }
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
}
}

Here is the composite screenshot showing code, xml file, the configurations and their corresponding outputs: composite screenshot

AntiqTech
  • 717
  • 1
  • 6
  • 10