Quality Testing

Quality is delighting customers

How to read data from JSON file using Selenium WebDriver?

Views: 150

Reply to This

Replies to This Discussion

To read json file you need only simple java code and JSON (maven repositories) and you can use it in where ever you required(Selenium webdriver). Reading test data from a JSON file is easy. In quality assurance services they did something like this to read json file:

Sample.jason:
{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}



Code:
package com.readjson;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.json.JSONObject;

public class ReadJsonFile {
    public static void main( String[] args ) throws IOException
    {
        String data = new String(Files.readAllBytes(Paths.get("src/resources/sample.json")));
        System.out.println(data);
        JSONObject jsonObject = new JSONObject(data);
        System.out.println(jsonObject.get("fruit"));
        System.out.println(jsonObject.get("size"));
        System.out.println(jsonObject.get("color"));
    }
}

Output:
{
    "fruit": "Apple",
    "size": "Large",
    "color": "Red"
}
Apple
Large
Red

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2019   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service