1

I have a testng XML file which runs Selenium tests in groups like below. I want to pass through all the browser info set in the parameter names and values from a JSON or another sort of file so i don't have to keep copy and pasting them if i add config for another test - so there is less code. is this possible with a testng XML file?

Thanks for any help.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="test.java" annotations="JDK" parallel="tests">
    <test name="Test - Chrome Vienna">
        <parameter name="browser" value="chrome"/>
        <parameter name="browserVersion" value="78.0"/>
        <parameter name="os" value="OS X"/>
        <parameter name="osVersion" value="Mojave"/>
        <parameter name="resolution" value="1024x768"/>
        <groups>
            <run>
                <include name="vienna" />
            </run>
        </groups>
        <classes>
            <class name="com.bookinggo.ticketed.uiendtoend.TicketedSingleJourneyTest"/>
        </classes>
    </test>
Fenomenoq
  • 51
  • 4

1 Answers1

0

You can use data provider. In your test class annotate the test:

@Test(dataProvider="SearchProvider")

And this is how your data provider should look like:

@DataProvider(name="SearchProvider")
    public Object[][] getDataFromDataprovider(){
    return new Object[][] 
        {
            { "browser", "chrome" },
            { "browserVersion", "78.0" },
            { "os", "OS X" }
        };

    }

Alternatively , you can create a separate class for dataprovider and read the json contents using following:

@DataProvider
public Object[][] getSearchProvider() throws FileNotFoundException {
    JsonElement jsonData = new JsonParser().parse(new FileReader("<json path>"));
    JsonElement dataSet = jsonData.getAsJsonObject().get("dataSet");
    List<TestData> testData = new Gson().fromJson(dataSet, new TypeToken<List<TestData>>() {
    }.getType());
    Object[][] returnVal = new Object[testData.size()][1];
    int i = 0;
    for (Object[] each : returnVal) {
        each[0] = testData.get(i++);
    }
    return returnVal;
}
salsinga
  • 1,957
  • 1
  • 14
  • 26