If i need to run same one method with two different browser at the same time then how will i implement it? For example:
public class AppTest2{
@parameters(\"br
You can consider something like the below as a possible solution
package com.rationaleemotions.stackoverflow;
import org.testng.IAlterSuiteListener;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.collections.Maps;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MultiBrowserSample {
@Test
@Parameters("browser")
public void testMethod(String browser) {
System.err.println("Browser : " + browser + " on Thread [" + Thread.currentThread().getId() + "]");
}
public static class MySuiteAlterer implements IAlterSuiteListener {
@Override
public void alter(List suites) {
XmlSuite suite = suites.get(0);
//Check if there was a parameter named "browserFlavors" defined at the suite
String browserFlavors = suite.getParameter("browserFlavors");
if (browserFlavors == null || browserFlavors.trim().isEmpty()) {
//If no such parameter was found, then Try querying the JVM arguments to see if it contains
//value for it. Just to ensure we don't end up in a situation wherein there's no JVM also provided
//Lets add a default value for the JVM argument which in our case is "firefox"
browserFlavors = System.getProperty("browserFlavors", "firefox");
}
String[] browsers = browserFlavors.split(",");
List xmlTests = new ArrayList<>();
for (String browser : browsers) {
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName(browser + "_test");
Map parameters = Maps.newHashMap();
parameters.put("browser", browser);
xmlTest.setParameters(parameters);
XmlClass xmlClass = new XmlClass();
xmlClass.setName(MultiBrowserSample.class.getCanonicalName());
xmlTest.getClasses().add(xmlClass);
xmlTests.add(xmlTest);
}
suite.setTests(xmlTests);
}
}
}
The suite xml file can look like below
So as you can see, here we are resorting to using a TestNG listener called IAlterSuiteListener
implementation which is going to help us construct the
tags in the suite xml file dynamically and the number of
tags in the suite xml file will be directly equal to the number of browsers specified either via the suite level parameter browserFlavors
(or) via the JVM argument -DbrowserFlavors
The output would be as below
[TestNG] Running:
/Users/krmahadevan/githome/PlayGround/testbed/src/test/resources/multi-browsers.xml
[ThreadUtil] Starting executor timeOut:2147483647ms workers:3 threadPoolSize:5
Browser : ie on Thread [13]
Browser : chrome on Thread [12]
Browser : firefox on Thread [11]
PASSED: testMethod("firefox")
PASSED: testMethod("ie")
PASSED: testMethod("chrome")
===============================================
ie_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
firefox_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
chrome_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
1265_Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================