I have DataProvider generating some data and feed it to my @Test
@DataProvider(name = "dp", parallel = true)
public Object[][] getBrowser(Method m) {
Iterator < String > iterator = Constants.browsers.iterator();
String browser = null;
Object[][] data = new Object[2][1];
for (int i = 0; i < 2; i++) {
if (iterator.hasNext()) {
browser = iterator.next();
data[i][0] = browser;
}
}
return data;
}
Constants.browsers would look like this
public static final List<String> browsers = new ArrayList<>(
Arrays.asList("Chrome","Edge"));
Basically just generating 2 data "Chrome" and "Edge" and then feed it to my @Test , which looks like this
@Test(dataProviderClass = TestUtilities.class, dataProvider = "dp")
public void testing(String browser, ITestContext context) throws InterruptedException {
ITestResult result = Reporter.getCurrentTestResult();
result.getMethod().setDescription(browser);
context.setAttribute("browser", browser);
initiate(browser, Constants.baseURL); //Instantiate Chrome or Edge WebDriver and go to the URL
// Do the testing
closeBrowser();
}
I have setup TestNG listeners
public class myListeners extends TestBase implements ITestListener, ISuiteListener {
@Override
public void onTestSuccess(ITestResult result) {
String methodName = result.getMethod().getMethodName();
String browser = result.getMethod().getDescription();
log.debug("ITestResult --> BROWSER is " + browser);
}
@Override
public void onFinish(ITestContext context) {
String usedBrowser = null;
usedBrowser = (String) context.getAttribute("browser");
log.debug("ITestContext --> Used Browser : " + usedBrowser);
}
}
Since @DataProvider(name = "dp", parallel = true)
, there will be 2 threads running at the same time. Chrome browser and Edge browser. I can see both of them starts at the same time and do some testing. Once the test finishes , both browsers quit().
I run it from my testng.xml which looks like this
?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="My Test Suite" parallel="tests">
<listeners>
<listener class-name="myListeners.myListeners"></listener>
</listeners>
<test name="myTest" preserve-order="true">
<classes>
<class name="testCases.testing" />
</classes>
</test>
</suite>
My issue is the listeners only print one of the thread. Sometimes it prints
18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Chrome
18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Chrome
Sometimes it prints
18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Edge
18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Edge
My expectation is that both parameters can be printed (logged) , like this (don't worry about the timestamp)
18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Chrome
18:38:02.110 DEBUG TestBase -ITestResult --> BROWSER is Edge
18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Chrome
18:38:04.190 DEBUG TestBase -ITestContext --> Used Browser : Edge
How can I do that ? Thanks.
Set the attribute as you are setting in ITestContext for ITestResult
@Test(dataProviderClass = TestUtilities.class, dataProvider = "dp")
public void testing(String browser, ITestContext context) throws InterruptedException {
ITestResult result = Reporter.getCurrentTestResult();
// Changes here
result.setAttribute("browser", browser);
context.setAttribute("browser", browser);
initiate(browser, Constants.baseURL); //Instantiate Chrome or Edge WebDriver and go to the URL
// Do the testing
closeBrowser();
}
@Override
public void onTestSuccess(ITestResult result) {
String methodName = result.getMethod().getMethodName();
//String browser = result.getMethod().getDescription();
// changes here
log.debug("ITestResult --> BROWSER is " + (String) result.getAttribute("browser"));
}
Below method will be executed only once
/**
* Invoked after all the test methods belonging to the classes inside the <test>
tag have run
* and all their Configuration methods have been called.
*
* @param context The test context
*/
@Override
public void onFinish(ITestContext context) {
String usedBrowser = null;
usedBrowser = (String) context.getAttribute("browser");
log.debug("ITestContext --> Used Browser : " + usedBrowser);
}
You will get output - something like this
DEBUG TestBase -ITestResult --> BROWSER is Chrome
DEBUG TestBase -ITestResult --> BROWSER is Edge
DEBUG TestBase -ITestContext --> Used Browser : Edge --- Note this--
On Finish will execute only once