GET some REST with API Test using Data Driven:
In the last post we have
seen how to work on GET method using Rest Assured. But it was tested for the using
single value and its hard coded. What if the requirement is like to test
different set of test data. In this case we need to go to DataDriven testing
reading data from external sources like XML, Excel, CSV or data files.
So here in this post we
will see how to feed data to the GET REST request using data from external source. Here
I will be using Excel file for the test data. I have prepared some sample data
for the testing purpose. Below is the test data, this is how my test data in excel file looks
like.
SI.No |
InputData |
Expected_FieldName |
Expected_Value |
1 |
INR |
name |
India |
2 |
INR |
name;capital;languages.name;region |
India;New Delhi;Hindi;Asia |
3 |
NOK |
name;capital;languages.name |
Norway;Oslo;Norwegian |
1st Column is for Serial number column
2nd Column is
for Input data, that we are passing in GET method to retrieve the details of
the entered value
3rd Column contains
the expected field names from the response received. Here we are concatenated
the expected field with comma separated. Consider if you want to verify more
than one field in response so we can pass like this.
4th Column contains
the expected field value from the response received. Example from 3rd
column for first record I am expecting only name field and value as India. Again,
in second records I am expecting name, capital, language name and region so
same way I am expecting value from that field. Make sure expected field name and
expected value column should same number of arguments. If we are expecting blank
value in field just blank value.
String strTestDataPath = "C:/Testdata/TestData_Currency.xls";
String strTestDataSheet = "Currency";
Workbook wrkBookOBj = Workbook.getWorkbook(new File(strTestDataPath));
Sheet sheet = wrkBookOBj.getSheet(strTestDataSheet);
int rRows = sheet.getRows();
strCurrencyName = sheet.getCell(1,Row).getContents();
strExpFieldNames = sheet.getCell(2,Row).getContents();
strExpFieldValues = sheet.getCell(3,Row).getContents();
strExpFieldNames: Contains the data from 2nd column i.e. Expected field name.
Here, Column names are hard coded as 1,2,3 as we know these columns are fix and not dynamic so.
To iterate the value till last row of the test data table we need to loop
it using 'for.. loop' and also pass the test data to the GET method. The combined
code looks like below.
RestAssured.baseURI = "https://restcountries.eu/rest/v2/currency/";
RequestSpecification reqObj = RestAssured.given();
Response resObj = null;
try {
Workbook wrkBookOBj = Workbook.getWorkbook(new File(strTestDataFilePath);
Sheet sheet = wrkBookOBj.getSheet(strTestDataSheetName);
int rRows = sheet.getRows();
int i = 0;
for(i=1;i<=rRows-1;i++) { //Rows loop
strCurrencyName = sheet.getCell(1,i).getContents();
strExpFieldNames = sheet.getCell(2,i).getContents();
strExpFieldValues = sheet.getCell(3,i).getContents();
resObj = reqObj.request(Method.GET,strCurrencyName);
} //Rows loop Ends
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
Till now we all set to run the test with multiple set of test data. Now
It’s time to read the response and we need to validate our expected field names
and expected values. To do that we need to split “strExpFieldNames”
and “strExpFieldValues” with the comma “;” separated,
here we will get expected field name expected values and compare with the
actual values and field name read from response. Below is the code for the splitting
the values and assigning it to an array and iterate using for loop for all the
expected field name and values we are expecting.
Once we got the “extFieldName” expected
field name, we need to pass that variable to fetch it’s value from the response.
After successfully reading the actual value from the response compare with the
expected values, if both are matches then print as “PASS” or else print as “FAIL”.
String strExpFieldName[] = strExpFieldNames.split(";");String strExpFieldValue[] = strExpFieldValues.split(";");for(int x = 0;x<=strExpFieldName.length-1;x++) {String extFieldName = strExpFieldName[x];String extFieldValue = strExpFieldValue[x];String strActFieldValues = resObj.jsonPath().get(extFieldName).toString().replace("[","").replace("]","");String strActFieldValue[] = strActFieldValues.split(",");boolean temp = false;for(int z = 0;z<=strActFieldValue.length-1;z++) {
if(strActFieldValue[z].trim().equalsIgnoreCase(extFieldValue.trim())) {System.out.println("PASS");temp = true;break;}if((!temp) && z == strActFieldValue.length-1) {System.out.println("FAIL");}}if(temp && x==strExpFieldName.length-1) {break;}}
Now I will put above code into one method with that our code
also looks neat and clean and this method can be reused for another class another
test.
Here I am passing expected field, expected values and Response object. By using Response object we can read the response content and retrieve the expected field values.
public static void fnCheckResult(String ExpFields, String ExpValues, Response resObj){
String strExpFieldName[] = ExpField.split(";");
String strExpFieldValue[] = ExpValues.split(";");
for(int x = 0;x<=strExpFieldName.length-1;x++) {
String extFieldName = strExpFieldName[x];
String extFieldValue = strExpFieldValue[x];
String strActFieldValues = resObj.jsonPath().get(extFieldName)
String strActFieldValue[] = strActFieldValues.split(",");
boolean temp = false;
for(int z = 0;z<=strActFieldValue.length-1;z++){ if(strActFieldValue[z].trim().equalsIgnoreCase(extFieldValue.trim())) {
System.out.println("PASS");}
temp = true;
break;
}
if((!temp) && z == strActFieldValue.length-1) {
System.out.println("FAIL");
}
}
if(temp && x==strExpFieldName.length-1) {
break;
package test.rest.practice;
import java.io.File;
import java.io.IOException;
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class HTTPGETTestREST {
static Workbook wrkBookOBj = null;
static Sheet sheet = null;
public static void main(String arg[]) {
String strCurrencyName = null; String strExpFieldNames = null;
String strExpFieldValues = null;
String strTestDataPath = "C:/TestRESTPractice/Testdata/TestData_Currency.xls";
String strTestDataSheet = "Currency";
RestAssured.baseURI = "https://restcountries.eu/rest/v2/currency/";
RequestSpecification reqObj = RestAssured.given();
Response resObj = null;
try {
wrkBookOBj = Workbook.getWorkbook(new File(strTestDataPath));
sheet = wrkBookOBj.getSheet(strTestDataSheet);
int rRows = sheet.getRows();
int i = 0;
for(i=1;i<=rRows-1;i++) { //Rows loop
strCurrencyName = sheet.getCell(1,i).getContents();
strExpFieldNames = sheet.getCell(2,i).getContents();
strExpFieldValues = sheet.getCell(3,i).getContents();
resObj = reqObj.request(Method.GET,strCurrencyName);
if(resObj.getStatusCode()==200){
**************************************************************************
} //Rows loop Ends
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
wrkBookOBj.Close();}}
String strExpFieldName[] = ExpField.split(";");
String strExpFieldValue[] = ExpValues.split(";");
for(int x = 0;x<=strExpFieldName.length-1;x++) {
String extFieldName = strExpFieldName[x];
String extFieldValue = strExpFieldValue[x];
String strActFieldValues = resObj.jsonPath() .get(extFieldName)
String strActFieldValue[] = strActFieldValues.split(",");
boolean temp = false;
for(int z = 0;z<=strActFieldValue.length-1;z++) {
if(strActFieldValue[z].trim().equalsIgnoreCase(extFieldValue.trim())) { System.out.println("PASS");**************************************************************************
temp = true;
break;
}
if((!temp) && z == strActFieldValue.length-1) { System.out.println("FAIL");
}
}
if(temp && x==strExpFieldName.length-1) {
break;
}
}
}
}