Doubt in Data driven with TestNG framework | Selenium Forum
rajan.kaushik1987 Posted on 01/04/2021

Hi Ashish,

I am following data driven framework with TestNG and I am creating Data driven framework with TestNG and I want to run test cases on different browsers . I am reading data from excel , should I add column of browsers in excel at every suite level i.e every sheet of excel sheet and read it and please explain the code how can I execute my test cases on different browsers.I am stuck badly.


Thanks and Regards,




Ashish Replied on 02/04/2021

In the data driven framework, I have told how you can do the browser configuration from the JSON file

Please us JSON

rajan.kaushik1987 Replied on 02/04/2021

Okay I will try.

rajan.kaushik1987 Replied on 06/04/2021

Hi Ashish,

I have tried to insert browser pararmeter. In console it displaying chrome and mozilla but my test cases are not executing on both browsers it's only executing on chrome.I have tried hard and done google but unable to do.Kindly provide the code I stuck badly since long time.Below is my code for JSONRunner :-


package runner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONRunner {
//can return string,jsonarray,jsonobject so cast to JSONArray
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
Map<String,String>classMethods=new DataUtil().loadClassMethods();
String path=System.getProperty("user.dir")+"//src//test//resources//jsons//testconfig.json";
JSONParser parser=new JSONParser();
JSONObject json=(JSONObject) parser.parse(new FileReader(new File(path)));
String parallelSuites=(String) json.get("parallelsuites");
TestNGRunner testNG=new TestNGRunner(Integer.parseInt(parallelSuites));

JSONArray browsers=(JSONArray) json.get("browsers");

for(int i=0;i<browsers.size();i++) {
String browser=(String) browsers.get(i);

JSONArray testSuites=(JSONArray) json.get("testsuites");
for(int sId=0;sId<testSuites.size();sId++) {
JSONObject testSuite=(JSONObject) testSuites.get(sId);
String runMode=(String) testSuite.get("runmode");
if(runMode.equals("Y")) {
String name=(String) testSuite.get("name");
String testdatajsonfile=System.getProperty("user.dir")+"//src//test//resources//jsons//"+(String) testSuite.get("testdatajsonfile");
String suitefilename=(String) testSuite.get("suitefilename");
String paralleltests=(String) testSuite.get("paralleltests");
boolean pTests=false;
testNG.createSuite(name, pTests);

String pathSuiteJSON=System.getProperty("user.dir")+"//src//test//resources//jsons//"+suitefilename;
JSONParser suiteParser=new JSONParser();
JSONObject suiteJSON=(JSONObject) suiteParser.parse(new FileReader(new File(pathSuiteJSON)));
JSONArray suiteTestCases=(JSONArray) suiteJSON.get("testcases");
for(int sTId=0;sTId<suiteTestCases.size();sTId++) {
JSONObject suiteTestCase=(JSONObject) suiteTestCases.get(sTId);

String tName=(String) suiteTestCase.get("name");
JSONArray parameternames=(JSONArray) suiteTestCase.get("parameternames");
JSONArray executions=(JSONArray) suiteTestCase.get("executions");
for(int eId=0;eId<executions.size();eId++) {
JSONObject testCase=(JSONObject) executions.get(eId);
String tRunMode=(String) testCase.get("runmode");
if(tRunMode !=null && tRunMode.equals("Y")) {
String executionname=(String) testCase.get("executionname");
String dataflag=(String) testCase.get("dataflag");
int dataSets=new DataUtil().getTestDataSets(testdatajsonfile, dataflag);
//how many sets of data are there
for(int dSId=0;dSId<dataSets;dSId++) {
JSONArray parametervalues=(JSONArray) testCase.get("parametervalues");
JSONArray methods=(JSONArray) testCase.get("methods");
//add to testng
//Add New Stock -New Stock -It.1
//Add New Stock -New Stock -It.2
for(int pId=0;pId<parameternames.size();pId++) {
testNG.addTestParameter("datafilepath", testdatajsonfile);
testNG.addTestParameter("dataflag", dataflag);
testNG.addTestParameter("iteration", String.valueOf(dSId));

List<String> includedMethods=new ArrayList<String>();

for(int mId=0;mId<methods.size();mId++) {
String method=(String) methods.get(mId);
String methodClass=classMethods.get(method);
if(mId==methods.size()-1 ||!((String)classMethods.get((String)methods.get(mId+1))).equals(methodClass)) {
//next method is from different class
testNG.addTestClass(methodClass, includedMethods);
includedMethods=new ArrayList<String>();
}else {
//next method is from same class





rajan.kaushik1987 Replied on 12/04/2021

Hi Ashish,

Please do needful.

rajan.kaushik1987 Replied on 13/04/2021

Please Ashish reply on this.I am waiting for your response since long time.

Ashish Replied on 13/04/2021

You have read the brwoser from JSON

Now you need to pass the browser to the test as well. You are not doing that

Pass it as a local parameter in the test and then according to that open the browser


testNG.addTestParameter("browser", ___);  You will have to put this line

Related Posts