Reading data from a JSON payload with selenium web driver
Note: This post was updated on 4th April 2020
Data-driven testing is an important aspect of web driver automation. Selenium supports the popular options of reading data from excel, JSON and CSV file formats. In this blog post, we will look at how we can read data from a JSON file and perform a google search.
For our example, we are going to first create a simple json file as below
{
"url":"http://www.google.com",
"name": "Shavantha Weerasinghe"
}
For this test along with the Selenium web driver dependencies, we need to add the below dependency to help read data from a JSON file.
Note: This post was updated on 4th April 2020
Data-driven testing is an important aspect of web driver automation. Selenium supports the popular options of reading data from excel, JSON and CSV file formats. In this blog post, we will look at how we can read data from a JSON file and perform a google search.
For our example, we are going to first create a simple json file as below
{
"url":"http://www.google.com",
"name": "Shavantha Weerasinghe"
}
For this test along with the Selenium web driver dependencies, we need to add the below dependency to help read data from a JSON file.
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Next,we create a simple java class that will read the values URL and Name from the json file.
Once read the driver.get method will be passed with the URL value and as the search parameter
we will pass the name value we read from the json file.
As we can see from the below code, for reading values from the json file, we need to add the
json simple packages as shown below. Using the Object parser we read the values from the json
file and then assign them to String variables.
Once the values are read and assign to variables we pass the URL to the driver.get method so
that we can access the google search page. Once this is loaded we look for the text file and
pass the student name which we have read using the sendkeys option. This will ensure that the
search value is passed and the search is performd.
package readjson; import java.io.IOException;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class ReadJson {
WebDriver driver;
WebDriverWait wait;
WebElement searchButton;
String url,name,batch;
int rollNo;
JSONParser parser=new JSONParser();
@BeforeTest
public void setup() throws FileNotFoundException, IOException, ParseException
{
System.setProperty("webdriver.firefox.driver", "/training/training/geckodriver");
Object obj = parser.parse(new FileReader("/training/training/student.json"));
JSONObject jsonObject = (JSONObject) obj;
url = (String) jsonObject.get("url");
name = (String) jsonObject.get("name");
driver = new FirefoxDriver();
driver.get(url);
}
@Test
public void testSearch() throws java.lang.IllegalMonitorStateException,NullPointerException
{
driver.findElement(By.name("q")).sendKeys(name);
wait=new WebDriverWait(driver, 20);
searchButton= wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div[3]/form/div[2]/div[1]/div[3]/center/input[1]")));
searchButton.click();
}
@AfterTest
public void tearDown()
{
driver.quit();
}
}
Comments
Post a Comment