[KShare] How to get the list of Nodes/Tag Names in the Response and Verify them?

Hi Community members, :wave:

How long have you been looking for a solution to get the list of nodes in the SOAP API response and verify them like in the screenshot below?

Today, our team will introduce you to an approach to help you achieve that.

You can follow the steps below to create a particular project for yourself.

  1. Create a new API project with Katalon Studio
  2. Import WSDL with this link: http://www.dneonline.com/calculator.asmx?WSDL

  1. Use the scripts below to send a request and get a response
import com.kms.katalon.core.testobject.RequestObject as RequestObject
import com.kms.katalon.core.testobject.ResponseObject as ResponseObject

RequestObject requestObject = findTestObject('SOAP Service/CalculatorSoap/Add')

ResponseObject response = WS.sendRequest(requestObject)

String responseTxt = response.getResponseText()

println(responseTxt)
  1. Use the scripts below to get a list of tag names in the response
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder
import org.xml.sax.InputSource
import org.w3c.dom.Document
import org.w3c.dom.NodeList
import org.w3c.dom.Element

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(response.getResponseText())));
NodeList nodeList=doc.getElementsByTagName("*");
for (int i=0; i<nodeList.getLength(); i++)
{
	// Get element
	Element element = (Element)nodeList.item(i);

	println(element.getNodeName());
}
  1. Also, we can add the Node Name above to a list and verify a specific node if it exists
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder
import org.xml.sax.InputSource
import org.w3c.dom.Document
import org.w3c.dom.NodeList
import org.w3c.dom.Element

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(response.getResponseText())));
NodeList nodeList=doc.getElementsByTagName("*");
List<Element> myList = new ArrayList<>(); <= DEFINE A LIST HERE
for (int i=0; i<nodeList.getLength(); i++)
{
	// Get element
	Element element = (Element)nodeList.item(i);

	myList.add(element.getNodeName()) // ADD NODE NAME TO THE LIST HERE
}
int count = myList.size()
for(int j = 0; j < count; j++) {
	if(myList.get(j) ==  "AddResult") { // VERIFY THE NODE NAME EXISTS HERE
		println 'the AddResult exists'
	}
}

Moreover, you can refer to this link for a sample project: https://dev.azure.com/linhnguyen0979/_git/KShare_APITesting_SOAPAPI_Nodes_Response


If you find this article helpful, then don’t forget to leave us a like :+1: or a heart :heart:, and share it with your colleagues or teammates.

To read other KShare articles, simply navigate to the support tag, go to the Product Insights category, or visit our Katalon Community Hub!

3 Likes

Thank you the Product Support team (@support.squad), and Linh Nguyen (@linh.nguyen) for yet another informative article on API Testing with Katalon Studio!

Linh Nguyen
Linh Nguyen (@linh.nguyen) - Product Support Manager at Katalon
Linh is the Product Support team Manager at Katalon. She spent many years working as an Automation Testing QA before joining Katalon Product Support as a technical support expert. Based on her experiences, she usually provides consumers with highly applicable solutions. She now manages her team with a user-centric approach to guarantee customers greater success with Katalon Products.
1 Like