How can I verify how many <title> tag in a xml page?

Hello All,

How can I verify how number of the opening and closing xml tag in a xml page? For example in the page xml tag has 41 times but how can I validate it using Katalon Studio? Thanks, below is the how xml page is look like. Thanks!!!

H.H. https://www. Hazard <p><span>The sole is not puncture-resistant, posing an injury hazard.</span></p> January 04, 2024 H.H. https://www. Homedics https://www. <p><span>The massagers can overheat while charging, posing fire and burn hazards.</span></p> January 04, 2024 Homedics Recalls Massagers Due to Fire and Burn Hazards https://www.
1 Like

The following might work:

import org.openqa.selenium.By as By
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement 
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
 
 
WebDriver driver = DriverFactory.getWebDriver()

List<WebElement> pieces = driver.findElements(By.tagName('your tag name here'))
WebUI.verifyEqual(pieces.size(), 41)

The above code was taken from the below Katalon Doc with a slight amendment.

Thank you but recived this error or I am doing something not right? (I am not an expert.Thanks)

You have to remove to beginning and ending “Greater Than” ( > ) and “Less Than” (< ) signs from the tag. You can review how Katalon did it in their document.


I tried still same error. Thanks

Here is another option for you to try. You will have to adjust the selected method to your tag.
Edit: the below method uses javascript, so you will have to wrap the script in an executeJavaScript method

Edit2: you might also try a change in the manner to find the tag in the first response, like:

List<WebElement> pieces = driver.findElements(By.xpath('//title'))
WebUI.verifyEqual(pieces.size(), 41)
1 Like

I find no <title> tag in the string you presented in the original post. Where is the <title> tag you mean?

Your question just puzzles me. Please restate your question so that others can understand it.


It is a xml page and I need to verify how many tags are there. Hope it make scene. Thanks

I believe that the code that @grylion54 proposed should work with the XML that your screenshot partly presents.

But you wrote that your code failed. I guess that your code has some mistake or your target XML document is something tricky.

You haven’t disclosed your code and the XML document enough. So, it is difficult for others in this forum to investigate your case any further. Please show us more information. You should disclose the full source code and the full XML document. Screenshots do not suffice. You should show source codes.

https://www.cpsc.gov/Newsroom/CPSC-RSS-Feed/Recalls-RSS

You could also try looking into the XmlSlurper to navigate/interact with the xml file…It’s been a while since i’ve used it so I’d have to do some refreshing, but that might be another option.

I created a Test Object for Web Service testing, named cpsc, which just contains the target URL and HTTP COMMAND to use (GET):

I wrote 2 Test Cases. The first one cpsc_save_rss downloads the RSS document and save it into a local file. The second one cpsc_verify_rss reads the RSS document from the file, parse it and verify if it contains 1 or more title elements.

cpsc_save_rss

// cpsc_save_rss
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kms.katalon.core.testobject.HttpBodyContent
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

ResponseObject response = WS.sendRequest(findTestObject('cpsc'))

HttpBodyContent content = response.getBodyContent()

println "content encoding: ${content.getContentEncoding()}"
println "content length: ${content.getContentLength()}"
println "content type: ${content.getContentType()}"

// save the responded XML into a file for further processings
InputStream is = content.getInputStream();
Path outFile = Paths.get("cpsc.xml")
OutputStream os = new FileOutputStream(outFile.toFile())
Files.copy(is, os)

cpsc_verify_rss

// cpsc_verify_rss
import java.nio.file.Path
import java.nio.file.Paths

import javax.xml.namespace.NamespaceContext
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.XPath
import javax.xml.xpath.XPathConstants
import javax.xml.xpath.XPathFactory

import org.w3c.dom.Document
import org.w3c.dom.NodeList


// The "cpsc.xml" file was created by the css_save_rss script.
// It downloaded a RSS document from https://www.cpsc.gov/Newsroom/CPSC-RSS-Feed/Recalls-RSS 
// and saved it into the file

Path rssFile = Paths.get("cpsc.xml")
InputStream is = new FileInputStream(rssFile.toFile())

DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance()
dbfactory.setNamespaceAware(true)
DocumentBuilder db = dbfactory.newDocumentBuilder()
Document xmlDocument = db.parse(is)

// XPath to select <title xmlns="https://www.cpsc.gov/"> elements
XPath xPath = createXPathForThis()
NodeList titles = (NodeList) xPath.compile("//title").evaluate(xmlDocument, XPathConstants.NODESET);

println titles.getLength()
assert titles.getLength() == 41
println "Success"

/*
 * 
 */
XPath createXPathForThis() {
	XPath xPath = XPathFactory.newInstance().newXPath()
	xPath.setNamespaceContext(new NamespaceContext() {
		@Override
		public Iterator getPrefixes(String namespaceURI) {
			return null;
		}
		@Override
		public String getPrefix(String namespaceURI) {
			return null;
		}
		@Override
		public String getNamespaceURI(String prefix) {
			if ("".equals(prefix)) {
				return "https://www.cpsc.gov/";
			}
			return null;
		}
	})
	return xPath
}

I ran the cpsc_save_rss first, and then cpsc_verify_rss. When I ran the cpsc_verify_rss, I saw the following output in the console:

2024-01-17 15:51:56.996 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2024-01-17 15:51:57.001 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/cpsc_verify_rss
41
Success
2024-01-17 15:51:58.458 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/cpsc_verify_rss

I could assert that the RSS document contained 41 title elements. I think that your problem has been resolved.


I wrote the Test Case cpsc_verify_rss using fully-fledged XML programming techniques in Java using the javax.xml.* packages and org.w3c.dom package. This is an orthodox style of XML programming in Java. I did so because the target RSS document deserves it; as the document employs XML Namespace fully. I am afraid that the cpsc_verify_rss is difficult to understand for those who are new to this problem domain.

I don’t know if Katalon Studio provides any other method that let you solve the problem more easily and magically.