cancel
Showing results for 
Search instead for 
Did you mean: 

[TechCorner Challenge #7] Change an XPath Match assertion to a JSONPath Match assertion

SOLVED
Highlighted
Community Manager

[TechCorner Challenge #7] Change an XPath Match assertion to a JSONPath Match assertion

Imagine that the logic of the server has been changed and you started getting responses from the server in the JSON format instead of XML. And, it may appear that XMLPath assertions don’t suit you anymore. In this case, you need to convert all XPath Match assertions to JSONPath Match assertions in your project. There could be a lot of test steps to change, so it’s better to automate this process.

Task: Create a script to convert XPath Match assertion to JSONPath Match assertion for the REST test steps where the request URL is the following: https://example.com/test.

DifficultyStar_Gold.pngStar_Gold.pngStar_Gold.png

A script should do this:

1. Add the JSONPath Match assertion to REST test steps that have the https://example.com/test endpoint.
2. Convert the XPath expression and expected result from the XPath Match assertion to the JSONPath expression and expected result for the corresponding test steps.
3. Remove the XPath Match assertions for those test steps.

A sample response:

 

JSON

XML

{
	"id": 1111,
	"category": {
		"id": 0,
		"name": "string"
	},
	"name": "doggie",
	"photoUrls": [
		"string"
	],
	"tags": [
		{
			"id": 99,
			"name": "string"
		}
	],
	"status": "available"
}
<Response xmlns="https://petstore.swagger.io/v2/pet">
   <id>1111</id>
   <category>
      <id>0</id>
      <name>string</name>
   </category>
   <name>doggie</name>
   <photoUrls>
      <e>string</e>
   </photoUrls>
   <tags>
      <e>
         <id>99</id>
         <name>string</name>
      </e>
   </tags>
   <status>available</status>
</Response>

 

XMLPath Match assertions example:

 

59754507-858b-4665-b54f-caf98d2443c6.png

 

Condition:

declare namespace ns1='https://petstore.swagger.io/v2/pet';
//ns1:Response[1]/ns1:tags[1]/ns1:e[1]/ns1:id[1]

Expected result:

${#TestCase#ID}

 

Tips:

 

Good luck and have fun😊


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
10 REPLIES 10
Community Hero

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.

Because,

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.



Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors