2

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("browser")
@Test(dataProvider="loginData")
public void login(String userName , String password, String param){
  if(param.equals("firefox"){
         //do something
    }
  if(param.equals("chrome"){
        //do something else
    }
 }

}

in my testng.xml file contains:

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name ="My sutie" parallel = "methods",  thread-count="5">
   <parameter name="browser"  value="firefox"/> 
   <test name ="My Test1">
     <classes>
        <class name="mq.demo.selenium.AppTest2"/>
     </classes>
    </test>
    </suite>

So my target is to run the login method in two different browser at the same time using two different thread.

Can anyone Help?

Thanks

4
  • Does your loginData data provider have the value parallel = true? Commented Mar 11, 2017 at 16:06
  • No it does not have the value parallel=true. Commented Mar 11, 2017 at 21:12
  • Oh wait you are defining the browser name in the xml. Have a parallel data provider that returns the login data and the browser name if you want to pass in separate browser names the way you have it already set up Commented Mar 11, 2017 at 21:15
  • Why don't you try Selenium Grid? It does what you exactly require. Commented Mar 12, 2017 at 0:43

1 Answer 1

3

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
===============================================
Sign up to request clarification or add additional context in comments.

2 Comments

nice explanation!
@Krishnan Mahadevan how to add multiple params inside test tag ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.