How to Parse SOAP response which contains a JSON with in it as a variable contains authtoken to further use in next request

Hello Everyone,

Someone can help me out here as i am totally new with Katalon Studio things with Groovy.

My Question is as below:
A Login request which is in SOAP

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <LoginUser xmlns="http://tempuri.org/">
      <parameters>{ "LoginId": "${GlobalVariable.baseloginid}", "Password": "${GlobalVariable.baseloginidpassword}", "DeviceId": "Deviceid1","LogoutFromWebsite": "true","DeviceName":"${GlobalVariable.DeviceName}","IpAddress":"${GlobalVariable.IpAddress}","StartMode":"1"}</parameters>
    </LoginUser>
  </s:Body>
</s:Envelope>

Its response is below:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <LoginUserResponse xmlns="http://tempuri.org/">
      <LoginUserResult>{"AuthenticationToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJWYWxpZGF0aW9uIFRva2VuIiwiaXNzIjoiRGV2aWNlaWQxIiwiZXhwIjoiOS8yOC8yMDIyIDY6MDQ6MTAgUE0iLCJhdWQiOiJhMzIwYzRhYzIwZDIwMWY5MGViYWNlZTU4MzdjOTExZSJ9.WBejY4jbPoMW3J7RTwqlh-vIdRzSuKq98GjYk9IImHw","LoginId":"User 3","DeviceName":"Device 1","Status":"200","Code":0}
      </LoginUserResult>
    </LoginUserResponse>
  </s:Body>
</s:Envelope>

Now i want to make AuthenticationToken as a Global variable to use in some other request which is required.

As i am using Test Listener before the Test Suite execution

class Authentication {
	/**
	 * Executes before every test suite starts.
	 * @param testSuiteContext: related information of the executed test suite.
	 */
	private void setToken()
		{
	//set token to global variable 'Token'
	def authentication_response = 	WS.sendRequest(findTestObject('Object Repository/Login'))
	
	WS.verifyResponseStatusCode(authentication_response, 200, FailureHandling.STOP_ON_FAILURE)
	
	def response = new XmlSlurper().parseText(authentication_response.getResponseBodyContent())
	
	print response.LoginUserResponse.LoginUserResult
	
	println (response.LoginUserResponse.LoginUserResult)
	
	JsonSlurper slurper = new JsonSlurper()
	Map parsedJson_Authentication2 = slurper.parseText(response.LoginUserResponse.LoginUserResult)

	KeywordUtil.logInfo("TOKEN received:    " + parsedJson_Authentication2.AuthenticationToken)

	GlobalVariable.Token = parsedJson_Authentication2.AuthenticationToken
	
	KeywordUtil.logInfo("TOKEN set to Global variable :    " + GlobalVariable.Token)
}

	@BeforeTestSuite
		def beforeTestSuite()
		{
			//set global variables
			setToken()	
		}
}

Console Log:

2022-08-29 18:13:00.672 INFO c.k.katalon.core.main.TestSuiteExecutor - START Test Suites/Login
2022-08-29 18:13:00.707 INFO c.k.katalon.core.main.TestSuiteExecutor - hostName = harshvardhan - EPL128.scceo.local
2022-08-29 18:13:00.708 INFO c.k.katalon.core.main.TestSuiteExecutor - os = Windows 10 64bit
2022-08-29 18:13:00.708 INFO c.k.katalon.core.main.TestSuiteExecutor - hostAddress = 192.168.1.100
2022-08-29 18:13:00.709 INFO c.k.katalon.core.main.TestSuiteExecutor - katalonVersion = 8.4.1.208
2022-08-29 18:13:04.786 INFO c.k.k.core.webservice.common.HarLogger - HAR: C:\Users\HARSHVARDHAN\Katalon Studio\MobileAPI_V2\Reports\20220829_181249\Login\20220829_181250\requests\main\0.har

2022-08-29 18:13:05.193 ERROR c.k.k.core.context.internal.TestHooker - :x: org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (groovy.util.slurpersupport.NodeChildren) values:
Possible solutions: parseText(java.lang.String), parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader) at Authentication.invokeMethod(Authentication.groovy) at com.kms.katalon.core.context.internal.TestHooker.invokeMethod(TestHooker.java:111) at com.kms.katalon.core.context.internal.TestHooker.lambda$3(TestHooker.java:95) at com.kms.katalon.core.context.internal.TestHooker.invokeContextMethods(TestHooker.java:94) at com.kms.katalon.core.context.internal.TestListenerCollector.lambda$3(TestListenerCollector.java:88) at com.kms.katalon.core.context.internal.TestListenerCollector.handleListenerEvent(TestListenerCollector.java:85) at com.kms.katalon.core.context.internal.ExecutionEventManager.publicEvent(ExecutionEventManager.java:36) at com.kms.katalon.core.main.TestSuiteExecutor.execute(TestSuiteExecutor.java:104) at com.kms.katalon.core.main.TestCaseMain.startTestSuite(TestCaseMain.java:185) at com.kms.katalon.core.main.TestCaseMain$startTestSuite$0.call(Unknown Source) at TempTestSuite1661776971000.run(TempTestSuite1661776971000.groovy:36) Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (groovy.util.slurpersupport.NodeChildren) values:
Possible solutions: parseText(java.lang.String), parse([B), parse([C), parse(java.io.File), parse(java.io.InputStream), parse(java.io.Reader)
at Authentication.setToken(Authentication.groovy:46)
at Authentication.beforeTestSuite(Authentication.groovy:59)
… 11 more

2022-08-29 18:13:05.407 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2022-08-29 18:13:05.407 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/Logout
2022-08-29 18:13:05.694 DEBUG testcase.Logout - 1: sendRequestAndVerify(findTestObject(“Logout”, [“baseurl”:baseurl, “MobileAuthToken”:MobileAuthToken]))
2022-08-29 18:13:05.852 INFO c.k.k.core.webservice.common.HarLogger - HAR: C:\Users\HARSHVARDHAN\Katalon Studio\MobileAPI_V2\Reports\20220829_181249\Login\20220829_181250\requests\main\1.har
2022-08-29 18:13:05.945 INFO c.k.k.core.main.WSVerificationExecutor - --------------------
2022-08-29 18:13:05.945 INFO c.k.k.core.main.WSVerificationExecutor - START Verification
2022-08-29 18:13:06.175 DEBUG testcase. - 1: request = getInstance().getCurrentRequest()
2022-08-29 18:13:06.181 DEBUG testcase. - 2: response = getInstance().getCurrentResponse()
2022-08-29 18:13:06.183 INFO c.k.k.core.main.WSVerificationExecutor - END Verification
2022-08-29 18:13:06.257 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/Logout
2022-08-29 18:13:06.615 INFO com.kms.katalon.core.util.KeywordUtil - Start generating HTML report folder at: C:\Users\HARSHVARDHAN\Katalon Studio\MobileAPI_V2\Reports\20220829_181249\Login\20220829_181250…
2022-08-29 18:13:06.704 INFO com.kms.katalon.core.util.KeywordUtil - HTML report generated
2022-08-29 18:13:06.707 INFO com.kms.katalon.core.util.KeywordUtil - Start generating CSV report folder at: C:\Users\HARSHVARDHAN\Katalon Studio\MobileAPI_V2\Reports\20220829_181249\Login\20220829_181250…
2022-08-29 18:13:06.756 INFO com.kms.katalon.core.util.KeywordUtil - CSV report generated
2022-08-29 18:13:07.108 INFO c.k.katalon.core.main.TestSuiteExecutor - --------------------
2022-08-29 18:13:07.108 INFO c.k.katalon.core.main.TestSuiteExecutor - END Test Suites/Login
2022-08-29 18:13:07.108 INFO c.k.katalon.core.main.TestSuiteExecutor - ====================

But after that its end up here.

In your code you have some statements that call “println” “WebUI.comment”. What did you see?

Please tell us the messages you saw in the LogViewer and the Console log.

What is LoginUserResponse? What is LoginUserResult? I do not find these 2 names in the XML document presented above, so I havd no idea why you use these 2 names in the print xxxx statement.

Did you edit the XML document above to shorten the previous post? Did you remove a <LoginUserRepose> element and a <LoginUserResult> element? If so, don’t do that. That makes it impossible for others to understand you. Please share the whole XML document.

Hi @kazurayam , due to copy paste actual response not visible, now please check.

And also Console Log has been updated in original post.

This error was caused by the following statement in your test case script:

def response = new XmlSlurper().parseText(authentication_response.getResponseBodyContent())

The method call getResponseBodyContent() returned an instance of groovy.util.slurpersupport.NodeChildren, not a String. So the error occured.

You need the text value of the NodeChildren object.

According to the GroovyDoc at NodeChildren (Groovy 2.4.6)

you could try

def response = new XmlSlurper().parseText(authentication_response.getResponseBodyContent().text())

Hi @kazurayam i tired but still giving error in Console.

please report with detail information. Withoug information disclosed others can not understand you.