Hi,
I have a WS object defined and I am trying to call it twice (each time using a different base URL - QA and PROD). This url is defined as a global variable and referenced in the variables tab of the ws object. It all works fine until I try to change the url from a function I have created like this:
GlobalVariable.env_url = “http://dev.mysite.com”
def response1 = WS.sendRequestAndVerify(requestObject)
GlobalVariable.env_url = “http://prod.mysite.com”
def response1 = WS.sendRequestAndVerify(requestObject)
this has no affect on the URL the object references when executing the below code…any ideas? Thanks
class Compare {
/**
* Send request and compare responses b/t two server instances
* @param request object - must be an instance of RequestObject
*/
@Keyword
def compareWsResponses(requestObject) {
//send requests
GlobalVariable.env_url = "http://apidev.mysite.com"
def response1 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("QA URL: " + requestObject.getRestUrl())
GlobalVariable.env_url = "http://api.mysite.com"
def response2 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("PROD URL: " + requestObject.getRestUrl())
//parse response
def parsedJson1 = new groovy.json.JsonSlurper().parseText(response1.getResponseBodyContent())
def parsedJson2 = new groovy.json.JsonSlurper().parseText(response2.getResponseBodyContent())
//JSONAssert.assertEquals(parsedJson1, parsedJson2, JSONCompareMode.STRICT)
//compare responses
if (parsedJson1 == parsedJson2) {
println('PASS')
KeywordUtil.markPassed('Responses Matched!')
} else {
println('FAIL')
KeywordUtil.markFailed('Responses did not match!')
}
KeywordUtil.logInfo("QA Response: " + parsedJson1.toString())
KeywordUtil.logInfo("PROD Response: " + parsedJson2.toString())
}
}
Hi Matt,
I think you must recreate your request object using the new URL. Once it is created, no references are updated automatically. Or update just URL in the original object.
//send original request
def response1 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("QA URL: " + requestObject.getRestUrl())
// update RequestObject instance
requestObject.setRestUrl("http://api.mysite.com")
// send updated request
def response2 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("PROD URL: " + requestObject.getRestUrl())
Edit: As I look on your code, I think even first change of URL does nothing. You pass the request object as a parameter to the method, thus any variable update has no effect.
See my code above.
Hi @Matt_Thurman,
I hope you can resolve the issue based on @Marek_Melocik’s advice.
I just want to inform that Katalon is going to create a dynamic web service object which allows users to send request by inputting a dynamic method, URL, header and content body. This means you don’t have to change the URL by using global variable any more as well as no need to create a fixed web service object, but pass directly in code. This is planned to be released in version 5.11. I’ll let you know when it’s available.
Thanks @Marek_Melocik and @Loan_Tran!
Here is what I am doing for now. Only the first part of the request URL is different, but I am having to re-build the entire URL using the same variables that are used in the WS object. Maybe I can execute the request once as it is defined in the object and then get the URL and replace the base URL before running the compare.
//vars
String wsPath = “/store/v1/topproducts/” + GlobalVariable.product_count + “?apikey=” + GlobalVariable.apikey + “&language=” + GlobalVariable.language + “&details=true”
String requestUrlQa = GlobalVariable.env_compare_qa + wsPath
String requestUrlProd = GlobalVariable.env_compare_prod + wsPath
//compare ws responses
CustomKeywords.‘com.ws.Compare.compareWsResponses’(findTestObject(‘Object Repository/API/storeAPI/General/top_products’), requestUrlQa, requestUrlProd)
I was able to get it working by grabbing the URL and then replacing the base URL with my QA and PROD versions.
Thanks!
class Compare {
/**
* Send request and compare responses b/t two server instances
* @param request object - must be an instance of RequestObject
*/
@Keyword
def compareWsResponses(requestObject) {
//get URL
def wsObjUrl = requestObject.getRestUrl().replace(GlobalVariable.env_url,"")
//set compare URLs
def requestUrlQa = GlobalVariable.env_compare_qa + wsObjUrl
def requestUrlProd = GlobalVariable.env_compare_prod + wsObjUrl
//send requests
// update RequestObject instance
requestObject.setRestUrl(requestUrlQa)
def response1 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("QA URL: " + requestObject.getRestUrl())
// update RequestObject instance
requestObject.setRestUrl(requestUrlProd)
def response2 = WS.sendRequestAndVerify(requestObject)
KeywordUtil.logInfo("PROD URL: " + requestObject.getRestUrl())
//parse response
def parsedJson1 = new groovy.json.JsonSlurper().parseText(response1.getResponseBodyContent())
def parsedJson2 = new groovy.json.JsonSlurper().parseText(response2.getResponseBodyContent())
//JSONAssert.assertEquals(parsedJson1, parsedJson2, JSONCompareMode.STRICT)
//compare responses
if (parsedJson1 == parsedJson2) {
println('PASS')
KeywordUtil.markPassed('Responses Matched!')
} else {
println('FAIL')
KeywordUtil.markFailed('Responses did not match!')
}
KeywordUtil.logInfo("QA Response: " + parsedJson1.toString())
KeywordUtil.logInfo("PROD Response: " + parsedJson2.toString())
}
}
1 Like