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<XmlSuite> 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<XmlTest> xmlTests = new ArrayList<>();
for (String browser : browsers) {
XmlTest xmlTest = new XmlTest(suite);
xmlTest.setName(browser + "_test");
Map<String, String> 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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="tests" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.stackoverflow.MultiBrowserSample$MySuiteAlterer"/>
</listeners>
<!--
If the below line gets uncommented, then 3 <test> tags will be formed one for each browser flavor.
Since its now commented, you will have to provide a value for it via the JVM argument
-DbrowserFlavors=firefox,chrome,ie (or) the system will default to just working with firefox
-->
<!--<parameter name="browserFlavors" value="firefox,chrome,ie"/>-->
</suite>
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 <test>
tags in the suite xml file dynamically and the number of <test>
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
===============================================