The JSONObject class

Once the data is extracted from the JSON file, it is available for use in the test methods. Users can cast the extracted data to a JSONObject of any type they desire to create. This allows them to access each field using a key/value pairing, and that data can be passed into test case methods that perform the actions on the screen.

Remember, when using the Selenium Page Object Model, each page object class contains all the methods that pertain to using the features on a specific screen, and those methods are called from within the test methods to vary data passed to them. This allows the test methods to be reused for multiple test scenarios, and keeps an abstract layer between the page object and the test classes.

The JSONObject is an interface that extends the JSONStructure class, inherits common methods from its superclass, and provides users with a simple data structure to store the test data. It can be used in conjunction with JSONReader, JSONWriter, JSONArray, and JSONObjectBuilder.

Now, let's explore a few examples of how to use it:

The JavaDoc for the JSONObject class is located at http://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html.
// using the rock bands JSON data we introduced earlier,
// create a JSONObject with the required field types

import org.json.simple.JSONObject;

/**
* Sample JSONObject Class
*
* @author name
*
*/
public class RockBands {
private String name, year, song;
private JSONObject members;

// the constructor requires the JSONObject when instantiated
public RockBands(JSONObject object) {
setName(object.get("name").toString());
setYear(object.get("year").toString());
setSong(object.get("song").toString());
setMembers((JSONObject) object.get("members"));
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public void setYear(String year) {
this.year = year;
}

public String getYear() {
return this.year;
}

public void setSong(String song) {
this.song = song;
}

public String getSong() {
return this.song;
}

public void setMembers(JSONObject members) {
this.members = members;
}

public JSONObject getMembers() {
return this.members;
}

@Override
public String toString() {
return "RockBands {" +
"name = '" + name + ''' +
", year = '" + year + ''' +
", song = '" + song + ''' +
", members = " + members +
'}';
}
}

First, the constructor in the class requires JSONObject to be passed into it when instantiated. Since we are using the JSON DataProvider to extract the data, we can cast it to a JSONObject on the fly as follows:

@Test(dataProvider="myData_JSON", dataProviderClass=JSONDataProvider.class)
public void tc001_getBandInfo(JSONObject testData) throws Exception {
// fetch object data and pass into Java object...
RockBands rockBands = new RockBands(testData);

// print out the JSONObject data extracted from file
System.out.println(rockBands.toString());
}

Second, notice that one of the members in the constructor takes another JSONObject parameter, that is because the band members key is a nested object in itself:

public RockBands(JSONObject object) {
setName(object.get("name").toString());
setYear(object.get("year").toString());
setSong(object.get("song").toString());
setMembers((JSONObject) object.get("members"));
}
// again, the data format looks like this in the JSON file:

"tc001_getBandInfo"
:[
{
"rowID":"tc001_getBandInfo.01",
"description":"Kiss Data",
"name":"Kiss",
"year":"1973",
"song":"Rock and Roll All Nite",
"members":{
"Vocals":"Paul Stanley",
"Bass":"Gene Simmons",
"Guitar":"Ace Frehley",
"Drums":"Peter Criss"
}
}

Finally, the data can be retrieved from the rockBands object using the key/value pairings:

System.out.println("
Name = " + rockBands.getName() +
" Year = " + rockBands.getYear() +
" Song = " + rockBands.getSong() +
" Vocals = " + rockBands.getMembers().get("Vocals") +
" Bass = " + rockBands.getMembers().get("Bass") +
" Guitar = " + rockBands.getMembers().get("Guitar") +
" Drums = " + rockBands.getMembers().get("Drums"));

Alternatively, the following can be used:

System.out.println(rockBands.toString());

The output of the first method noted above looks like this (although the intention is to pass it into a page object class method for processing):

Name = Kiss
Year = 1973
Song = Rock and Roll All Nite
Vocals = Paul Stanley
Bass = Gene Simmons
Guitar = Ace Frehley
Drums = Peter Criss

The alternative method will produce the following output:

RockBands {name = 'Kiss', year = '1973', song = 'Rock and Roll All Nite', members = {"Bass":"Gene Simmons","Guitar":"Ace Frehley","Vocals":"Paul Stanley","Drums":"Peter Criss"}}

Some users prefer to build the Java objects using the builder class interface, which has some of the same design pattern but allows users to set only the fields they want to change. Here's an example using the same data structure:

/**
* Sample JSON Object Class
*
* @author Name
*
*/
public class RockBandsBuilder {
public String name, year, song;
public JSONObject members;

/**
* Builder interface
*
*/
public static class Builder {
private String name, year, song;
private JSONObject members;

public Builder() {
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder year(String year) {
this.year = year;
return this;
}

public Builder song(String song) {
this.song = song;
return this;
}

public Builder members(JSONObject members) {
this.members = members;
return this;
}

public RockBandsBuilder build() {
RockBandsBuilder rockBands = new RockBandsBuilder(this);
return rockBands;
}
}

public RockBandsBuilder(Builder builder) {
this.name = builder.name;
this.year = builder.year;
this.song = builder.song;
this.members = builder.members;
}

public RockBandsBuilder(RockBandsBuilder rockBands) {
this.name = rockBands.name;
this.year = rockBands.year;
this.song = rockBands.song;
this.members = rockBands.members;
}

@Override
public String toString() {
return "RockBandsBuilder {" +
"name = '" + name + ''' +
", year = '" + year + ''' +
", song = '" + song + ''' +
", members = " + members +
'}';
}
}

The test method use of this class would look like this:

@Test(dataProvider="myData_JSON", dataProviderClass=JSONDataProvider.class)
public void tc002_getBandInfo(JSONObject testData) throws Exception {
// fetch object data and pass into Java object...
RockBandsBuilder rockBands = new RockBandsBuilder.Builder()
.name(testData.get("name").toString())
.year(testData.get("year").toString())
.song(testData.get("song").toString())
.members((JSONObject) testData.get("members"))
.build();


// print out the JSONObject data extracted from file
System.out.println(rockBands.toString());
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.120.133