Home > Back-end >  How to use JSON/XML to pass data to a form using Selenium WebDriver
How to use JSON/XML to pass data to a form using Selenium WebDriver

Time:09-21

I am automating a registration form that has various fields such as Name, Email, Phone etc. and I wish to pass data to the form without using Excel or CSV files, and I want to use JSON/XML instead. Now I have no clue how to achieve that, would be very helpful if someone can tell me what dependencies I need to use and how do I go about it.

CodePudding user response:

As you want to read the JSON and pass the data to scripts but we don't know your JSON so I am assuming the JSON in below format.

JSON:

{
    "name":"Nandan",
    "email":"[email protected]",
    "phone":1234567890
 }

Maven dependency:

<dependency>
     <groupId>org.json</groupId>
     <artifactId>json</artifactId>
     <version>20160212</version>
 </dependency>

Imports:

import org.json.JSONObject;

Read and get data from JSON:

String jsonDataAsString = new String(Files.readAllBytes(Paths.get("C:\\Users\\Sample.json")));
         
JSONObject jsonData = new JSONObject(jsonDataAsString);
System.out.println(jsonData.get("name"));
System.out.println(jsonData.get("email"));
System.out.println(jsonData.get("phone"));

Output:

Nandan
nandan@gmail.com
1234567890
  • Related