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.
Difficulty:
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 |
---|---|
|
|
XMLPath Match assertions example:
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:
https://support.smartbear.com/readyapi/docs/soapui/steps/groovy.html#working-with
https://support.smartbear.com/readyapi/apidocs/soapui/DefaultPackage/XPathContainsAssertion.html
https://support.smartbear.com/readyapi/apidocs/pro/DefaultPackage/JsonPathContentAssertion.html
Good luck and have fun😊
Solved! Go to Solution.
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.
This is a solution created for [TechCorner Challenge #7]
Thanks, @sonya_m! When I started as a tester, I realized the power groovy gave for automating even a lot of the manual processes of building tests within ReadyAPI. Having a degree on software engineering, I leaned hard on that and groovy and the api's of ReadyAPI itself to try to remove a lot of the menial parts of building a suite of tests. I love groovy and how much it helps!
That being said, I may have went down the more complicated path of looking at the xpath statement and I may have tried to solve a problem that wasn't there. The issue I ran into was the <e> element under tags, as that made me think that may be a listable element. While the example provided only one <e>, I went down the path of how I would write this if there were MORE than that. That's when things (to me) got really complex and hard to follow. I did come up with something, but I am not 100% comfortable with it. I either made it way too complex or made too many (potentially bad) assumptions.
Here's what I came up with:
def project = context.testCase.testSuite.project;
def URL = "https://example.com/test";
for (ts in project.getTestSuiteList())
{
for (tc in ts.getTestCaseList())
{
for (step in tc.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep))
{
// Was also stuck here. Is there an easier way to get the rest URL/URI of a REST test step? This method requires the REST step has been ran.
// Seems like room for improvement.
if (step.getHttpRequest().getResponse().getURL().toString().equals(URL))
{
for (assertion in step.getAssertionList())
{
if (assertion.getName().equals("XPath Match"))
{
def xpaths = assertion.getPath().toString().tokenize("\n");
def json = xpaths[1]
// Strip the xpath response statement down to the necessary parts. This makes assumptions on the xpath string always matching the example other than indexed location of E.
json = json.replace("//ns1:Response[1]/", "").replaceAll("ns1:", "").replace("tags[1]", "tags").replace("id[1]", "id");
// Split the xpath statement into an array for ease of modifying, using the / character as a delimiter
json = json.tokenize("/");
// Assuming the xpath statement is always tags/e/id, the second spot in the array is the position of the <e> element we're concerned with.
json[1] = (json[1].replaceAll("[A-z\\[\\]]","").toInteger() - 1).toString()
// Rebuild the json path.
json = json[0] + "[" + json[1] + "]." + json[2];
log.info(json);
// def json = "tags[0].id";
// Get the expected content as a string
def result = assertion.getExpectedContent().toString();
// Remove the offending assertion
// step.removeAssertion(assertion);
// Add new JSONPath assertion
step.addAssertion("JsonPath Match");
def jsonAssertion = step.getAssertionByName("JsonPath Match");
// Set the path and the expected content
jsonAssertion.setPath(json);
jsonAssertion.setExpectedContent(result);
}
}
}
}
}
}
It's not perfect, because I am not sure if I was supposed to actually figure out a way to convert the Xpath Statement from the XPath Match assertion to something the JsonPath could accept and translate. I was thrown off a bit, so I hard coded that part. Eager to see what others came up with!
def project = context.testCase.testSuite.project;
def URL = "https://example.com/test";
for (ts in project.getTestSuiteList())
{
for (tc in ts.getTestCaseList())
{
for (step in tc.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep))
{
// Was also stuck here. Is there an easier way to get the rest URL/URI of a REST test step? This method requires the REST step has been ran.
// Seems like room for improvement.
if (step.getHttpRequest().getResponse().getURL().toString().equals(URL))
{
for (assertion in step.getAssertionList())
{
if (assertion.getName().equals("XPath Match"))
{
def xpaths = assertion.getPath().toString().tokenize("\n");
log.info(xpaths[1].toString());
// def xpaths = xpath.;
// I could not figure out the sorcery to convert the xpath statement to the json statement. Would be interested to see what others come up with.
// def json = xpaths[1].replace("//ns1:Response[1]", "").replace("[1]","[0]").replace('''/ns1:''', ".");
def json = "tags[0].id";
// Get the expected content as a string
def result = assertion.getExpectedContent().toString();
// Remove the offending assertion
step.removeAssertion(assertion);
// Add new JSONPath assertion
step.addAssertion("JsonPath Match");
def jsonAssertion = step.getAssertionByName("JsonPath Match");
// Set the path and the expected content
jsonAssertion.setPath(json);
jsonAssertion.setExpectedContent(result);
}
}
}
}
}
}
That's a start! Thank you @msiadak , you are really good at scripting!
Who will try to develop this idea? Richie, will you try? msiadak provided us with a great basis, but, yes, he is right - the main task is to find a way of converting XPath to JSONPath!
@avidCoder @HimanshuTayal @Radford @nmrao ?🙂
Or, maybe, msiadak will finish what he started himself!
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.
This is a solution created for [TechCorner Challenge #7]
Thanks, @sonya_m! When I started as a tester, I realized the power groovy gave for automating even a lot of the manual processes of building tests within ReadyAPI. Having a degree on software engineering, I leaned hard on that and groovy and the api's of ReadyAPI itself to try to remove a lot of the menial parts of building a suite of tests. I love groovy and how much it helps!
That being said, I may have went down the more complicated path of looking at the xpath statement and I may have tried to solve a problem that wasn't there. The issue I ran into was the <e> element under tags, as that made me think that may be a listable element. While the example provided only one <e>, I went down the path of how I would write this if there were MORE than that. That's when things (to me) got really complex and hard to follow. I did come up with something, but I am not 100% comfortable with it. I either made it way too complex or made too many (potentially bad) assumptions.
Here's what I came up with:
def project = context.testCase.testSuite.project;
def URL = "https://example.com/test";
for (ts in project.getTestSuiteList())
{
for (tc in ts.getTestCaseList())
{
for (step in tc.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep))
{
// Was also stuck here. Is there an easier way to get the rest URL/URI of a REST test step? This method requires the REST step has been ran.
// Seems like room for improvement.
if (step.getHttpRequest().getResponse().getURL().toString().equals(URL))
{
for (assertion in step.getAssertionList())
{
if (assertion.getName().equals("XPath Match"))
{
def xpaths = assertion.getPath().toString().tokenize("\n");
def json = xpaths[1]
// Strip the xpath response statement down to the necessary parts. This makes assumptions on the xpath string always matching the example other than indexed location of E.
json = json.replace("//ns1:Response[1]/", "").replaceAll("ns1:", "").replace("tags[1]", "tags").replace("id[1]", "id");
// Split the xpath statement into an array for ease of modifying, using the / character as a delimiter
json = json.tokenize("/");
// Assuming the xpath statement is always tags/e/id, the second spot in the array is the position of the <e> element we're concerned with.
json[1] = (json[1].replaceAll("[A-z\\[\\]]","").toInteger() - 1).toString()
// Rebuild the json path.
json = json[0] + "[" + json[1] + "]." + json[2];
log.info(json);
// def json = "tags[0].id";
// Get the expected content as a string
def result = assertion.getExpectedContent().toString();
// Remove the offending assertion
// step.removeAssertion(assertion);
// Add new JSONPath assertion
step.addAssertion("JsonPath Match");
def jsonAssertion = step.getAssertionByName("JsonPath Match");
// Set the path and the expected content
jsonAssertion.setPath(json);
jsonAssertion.setExpectedContent(result);
}
}
}
}
}
}
It works great for the given XPath, @msiadak ! Solved!
I am wondering if anybody will dare to write code for a more general case, so that it could work for other Xpath structures, as well.
I won't lie, @sonya_m, I'm still trying to figure this out. So if anyone has a solution and can let me stop thinking about this, that'd be much appreciated! 🤣
@msiadak You can try shifting your thoughts to the next one, mayhaps, this helps😁
@richie this one has max difficulty, a tough one indeed!
@sonya_m, I have to ask. Is there a known solution for converting the xpath statement to json??? I still haven't figured it out. 😑
User | Count |
---|---|
6 | |
6 | |
4 | |
2 | |
1 |
Subject | Author | Latest Post |
---|---|---|