问题
I am a newbie with Rest Assured and seek help. I have written code to read values from an excel sheet and pass them as a path params in my Rest Assured tests. And this works as expected. However after I get the json response from the post request I make, I want to update the same excel with values from the response.
The excel has columns called Train number, date of journey and various class codes as other columns like first class code, business class code, standard class code that should show the availabality in them and so on something like this:
Initially:
ServiceName | Date | A0 | A1 | A2 | ...... 45 columns
9008 |2019-07-28| 0 | 0 | 0 |....... 45 columns
After test:
ServiceName | Date | A0 | A1 | A2 | ...... 45 columns
9008 |2019-07-28| 45 | 23 | 64 |....... 45 columns
I have followed apache-poi documentation to read values from excel with supporting methods to read rows, columns and getting cell value. However could not find anything that suggests how to update the same excel with the values from Json response.
These are the test and data provider methods I have come up for reading values from excel
@DataProvider(name="logicalAvProvider")
String [][] getLogicalAv() throws IOException
{
String path = "C:\\Misc\\LogAv.xlsx";
int rownum = XLUtils.getRowCount(path, "LogAv");
int colcount=XLUtils.getCellCount(path, "LogAv",rownum );
String LogAvData[][] = new String[rownum-1][colcount];
for (int i=1; i <rownum; i++ ) {
for (int j=0; j<colcount; j++) {
LogAvData[i-1][j] = XLUtils.getCellData(path, "LogAv", i, j);
//System.out.println("Data is " +LogAvData[i-1][j]);
}
}
return(LogAvData);
}
@Test(dataProvider="logicalAvProvider")
public void LogicalAvailablity(String ServiceName, String Date) throws IOException {
Response res=
given()
//.log().all()
.spec(reqSpec)
.pathParams("service_name", ServiceName, "travel_date", Date)
.when()
.get(EndPoints.LOGICAL_AVAILABILTY)
.then()
.spec(resSpec)
.extract().response();
//.log().body();
}
This is the sort of response we see after doing the post request. I need the number available to be updated under the respective columns in the excel.
[
{
"od_pair": "7015400:8727100",
"buckets": [
{
"bucket": "C00",
"original": 2,
"available": 2
},
{
"bucket": "A01",
"original": 76,
"available": 0
},
{
"bucket": "B01",
"original": 672,
"available": 477
},
{
"bucket": "B03",
"original": 578,
"available": 383
}
]
}
]
An advice on the approach to update the excel sheet with the values. I am not expecting anyone to give me the exact solution of my problem but any advice on the approach or any reference that I can refer to achieve this would be highly appreciated.
回答1:
Extract the required data from response and then write it to excel sheet. Using restAssured/JsonPath you can extract data from API response. Using apachePOI you should be able to write into Excel, refer below link for more details: https://www.codejava.net/coding/how-to-write-excel-files-in-java-using-apache-poi
Let us know if you run into any issues
来源:https://stackoverflow.com/questions/57243315/updating-excel-with-values-from-rest-assured-json-response-using-apache-poi