0
votes

To perform cross browsing through TestNG @Parameter,

We have to by pass-through @Parameter annotation and define variable value in tag,

Case: When we have multiple .XML suites, And each .xml suite contains more than 5 or 7 or 10+ Test scripts.

On that scenario, we have to define parameter value for each browser with repetition of classes. Step forward if there are 50 Test class, So if will define total 150 times, Which is not meaningful at all.

<suite name="Suite">
    <test name="ChromeTest">
        <parameter name="browser" value="chrome" />
        <classes>
            <class name="Demos.Demo01" />
            <class name="Demos.Demo02" />
            <class name="Demos.Demo03" />
            <class name="Demos.Demo04" />
        </classes>
    </test>
    <!-- Test -->
    <test name="FirefoxTest">
        <parameter name="browser" value="Firefox" />
        <classes>
            <class name="Demos.Demo01" />
            <class name="Demos.Demo02" />
            <class name="Demos.Demo03" />
            <class name="Demos.Demo04" />
        </classes>
    </test>
    <test name="EdgeTest">
        <parameter name="browser" value="edge" />
        <classes>
            <class name="Demos.Demo01" />
            <class name="Demos.Demo02" />
            <class name="Demos.Demo03" />
            <class name="Demos.Demo04" />
        </classes>
    </test>
    <!-- Test -->
</suite>
<!-- Suite -->

Please share the effective way to manage,

Is there any other way in TestNG or Selenium to define the parallel browsing scope to have cross browsing on Local system? Except(Selenium Grid)

1

1 Answers

3
votes

You can use packages instead of class in order to execute all the tests instead of mentioning each and every class:

<test name="ChromeTest" parallel="none" preserve-order="true">
    <parameter name="browser" value="chrome" />
    <packages>
        <package name="Demos"  />
    </packages>
</test>
<test name="EdgeTest" parallel="none" preserve-order="true">
    <parameter name="browser" value="edge" />
    <packages>
        <package name="Demos"  />
    </packages>
</test>

Or to use your current method just with multiple parameters defined (you will need to use DataProvider too in order to parse the String array):

<suite name="Suite" verbose="3">
    <test name="SeleniumTest verbose="3"">
        <parameter name="browser" value="chrome, edge, firefox" />
        <classes>
            <class name="Demos.Demo01" />
            <class name="Demos.Demo02" />
            <class name="Demos.Demo03" />
            <class name="Demos.Demo04" />
        </classes>
       </test>
    <!-- Test -->
</suite>
<!-- Suite -->

And add the DataProvider to parse each browser value:

@DataProvider
public Object[][] getData(ITestContext context) {
    String parameter = context.getCurrentXmlTest().getLocalParameters().get("browser");
    String[] browsers = parameter.split(",");
    Object[][] returnValues = new Object[names.length][1];
    int index = 0;
    for (Object[] each : returnValues) {
        each[0] = browsers[index++].trim();
    }
    return returnValues;
}