0

I am working on an Automation Framework and I am looking for an alternative for excel to store test data, element locators and page objects.

So one my friend working on Automation is using json file to store all data as its easy and faster in reading and writing data, Also it can be easy in maintaining. They are using ruby as the language.

So I wanted to know if we can do the same using java & selenium to achieve this?

I have searched google for this and it looks like there is a library called "gson" from google, but none that shows how to use it using selenium.

please share your thoughts on this.

Thank you!!

2
  • Of course it is possible to use JSON in ANY java project, as you already mentioned gson is a good java library for dealing with json. Please provide examples of the data you wish to use as json and how it will be used in your java selenium tests. Otherwise what other help can we offer? Commented Jun 1, 2016 at 20:46
  • Thanks user2272115 for your comment - when I am adding a question without an example means that its a general question and any help will be good for me to start with. Thanks! I will share the data soon! Commented Jun 1, 2016 at 22:54

2 Answers 2

2

I can't speak to including element locators in a JSON file, as I follow the page object model and include all those in the java classes. However, reading test data from a JSON file is very easy. It's been a while since I've messed around with this, but I used JSON Simple (which I still use to generate JSON objects/files) and did something like this to read in the file:

protected JSONObject getDataFile(String dataFileName) {
    String dataFilePath = "src/test/resources/";
    JSONObject testObject = null; 

    try {
        FileReader reader = new FileReader(dataFilePath + dataFileName);                        
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
        testObject = (JSONObject) jsonObject;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return testObject;
}

Once you have the JSON Object, JSON simple provides a few different ways to interact with it and get the values. I've played around with Gson a while back and I think that was fairly similar.

I don't know how your data file is/will be structured, but I had a key string that represented a test case name, and the value was a json object that contained other key-value pairs with the actual data and I fed that data to a TestNG data provider. If that is similar to your setup, I can share that code.

EDIT: Here is the method used by the @DataProvider

public Object[][] getTestScenarios(String dataFileName, String testCaseName) {
    JSONArray testCase = (JSONArray) getDataFile(dataFileName).get(testCaseName);
    List<JSONObject> testScenarioArray = new ArrayList<JSONObject>();

    for (int i = 0; i < testCase.size(); i++) {
         testScenarioArray.add((JSONObject) testCase.get(i));
    }

    Object[][] dataProviderArray = new Object[testScenarioArray.size()][];
    for (int scenario = 0; scenario < testScenarioArray.size(); scenario++) {
        String scenarioName = null;

        if ((String) testScenarioArray.get(scenario).get("scenario") != null) {
            scenarioName = (String) testScenarioArray.get(scenario).get("scenario");
        } else {
            scenarioName = "No scenario name specified";
        };
        dataProviderArray[scenario] = new Object[] { scenarioName, (JSONObject) testScenarioArray.get(scenario) };
    }
    return dataProviderArray;
}

The scenario name stuff could be removed, as I believe I only used that for logging or reporting, if I recall correctly. The reason I had it as a JSONArray and coded in this fashion is so a single test case could have an array with multiple scenarios with differing data. Didn't want the tests to have to care how many scenarios there were.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @pyde for your answer - I will implemet the parser and will share the data on how i am trying to read it soon.
Please Share the code Pyde. I am also using Testng.
Sorry for the delay in my response. Bare with me, I'll have it up in a moment.
0

I have read a JSON Array from DB and made a list of a JSON object from JSON Array.

The array looks as follows:

[{
    "index": "data",
    "type": "message",
    "sum": 
    {
        "message": "HELLO",
    },
}, {
    "index": "data",
    "type": "message",
    "sum":
    {
        "message": "HELLO123",
    }
}]

It is collected from DB in STRING form but is an array as it has SQUARE BRACKETS: [{Json1}, {Json2}].

String data = "ArrayFromDB";
JSONArray jsonArr = new JSONArray(data);
List<String> listJSON = new ArrayList<String>();

for (int i = 0; i < jsonArr.length(); i++)
{
    listSMSJSON.add(jsonArr.getJSONObject(i).getJSONObject("sum").getString("message"));
}

System.out.println(listJSON);

listJSON is printed as [HELLO, HELLO123]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.