Hi Community members,
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.
- Create a new API project with Katalon Studio
- Import WSDL with this link: http://www.dneonline.com/calculator.asmx?WSDL
- 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)
- 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());
}
- 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 or a 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!