Re: [TechCorner Challenge #7] Change an XPath Match assertion to a JSONPath Match assertion
Wanted to add more practical use cases here.
1. There are many implementations provide dynamic response either XML or JSON based on the Accept header or query parameter for expected response format.
2. In existing version of the application, only SOAP services are there. However, new version of the application also provides respective REST services. This is even quite bigger challenge as all of the test cases have to have REST test step where SOAP step was there. Both SOAP and REST services have to be tested as well.
- In both of the above use cases, either XPath or jsonpath has to be validated but not both the same time. This is an unique challenge can't delete XPath assertion as well.
Going to provide how to deal the cases
The approach I suggest is to write an utility rather than a groovy script.
a. this has to deal not just a test step, rather entire project.
b. user should still be able to deal with both XML and JSON responses and their assertions during the testing.
What is function of the utility we are talking about?
- Input for the utility is existing project, say uses XML responses and XPath assertions.
- Output is to create new project with JSONPath assertions wherever tests have to deal with JSON responses
- Phase 1 - Part of this utility job is to first extract the existing xpath assertion details into a file of any preferred format say, XML or JSON or csv. This will make life little easier to translate each XPath to respective JSONPath(this will be manual, refer NOTE) and update this file with working JSONPath. Ensure the JSONPaths are tried on the respective JSON string.
- Phase 2- Use this updated file and the existing project to create the JSONPath assertions. Also disable existing XPaths or delete.
Once the above is done, user can now either use old project to deal with XML response and XPath assertions or use newly created project to deal with JSON response and JSONPath assertions.
This way, utility can help to transform the entire project rather just dealing a particular test step with additional groovy script step.
NOTE:Please keep the following in the mind
a. when XPath assertions are created initially (in the existing project), XPaths are created by hand.
b. even if a new JSONPath assertion is going to be created, paths will be created by hand based on the response structure
c. converting XPath to JSONPath seamlessly and automatically is not a simple thing, itself will be a separate develop project, and I don't see even any such library out there in the market.
d. there can be different XPaths to extract the same data, but not true for JSON as is.