How to check the existence of text on the entire web page?

I need to check if there is a word or phrase on the page and get:

1.true or false

2.the name or xpath of the object containing

For #1 you can do the following in JavaScript:

document.body.innerText.includes("your text here")

Or, if the DOM API police are watching


document.querySelector("body").innerText.includes("your text here")

But for #2 you’ll need a dedicated dom-walker, I believe.

You want to make a Test Object with selector by XPath:

//body

Once this XPath expression is applied to a web page http://demoaut.katalon.com , it returns such a String value:

CURA Healthcare
Home
Login
CURA Healthcare Service
We Care About Your Health
Make Appointment
CURA Healthcare Service
Atlanta 550 Pharr Road NE Suite 525
Atlanta, GA 30305
(678) 813-1KMS
info@katalon.com
Copyright © CURA Healthcare Service 2018

On the other hand, you want to make a Test Case with following script codes:

WebUI.openBrowser('')
WebUI.navigateToUrl('http://demoaut.katalon.com/')
def content = WebUI.getText(findTestObject('Page_Cura Healthcare Service/body'))
WebUI.verifyMatch(content, '.*Make Appointment.*', true)
WebUI.closeBrowser()

The Regular Expression here

.*Make Appointment.*

matches with ‘Make Appointment’ preceded with zero-or-more any characters and appended with zero-or-more any characters


By this, you can determine “1. true or false” if a string ‘Make Appointment’ appears in the page.

----

You raised another problem:

2.the name or xpath of the object containing

I think that you can not solve this problem using the Katalon Studio’s standard keywords. All of the Katalon keywords assumes that you are aware which HTML node is of your interest in a page.
If you want to do HTML node-tree traversal, I think you need to develop a custom keyword which performs serious DOM traversal and transformation.

1 Like

thx a lot)))

Try using WebUI.verifyTextPresent()
make sure to set the FailureHandling.OPTIONAL tag if you want to use this in an if statement.

Below example of capture returning message when the login button was clicked.
This helps in capturing text on the page which can be include in any point within the test cases.

import com.kms.katalon.core.webui.driver.DriverFactory
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.WebDriver

WebUI.openBrowser(‘’)
WebUI.navigateToUrl(‘Login Example - Semantic’)
WebDriver wd =DriverFactory.getWebDriver()
WebUI.click(findTestObject(‘Object Repository/Page_Login Example - Semantic/div_Login’))
String pageSource=wd.getPageSource()
Document doc = Jsoup.parse(pageSource);
String allText = “”;
elements = doc.body().select(“*”);
for (Element element : elements) {
allText+=element.ownText()+“\n”;
}

Hello to everyone,
For the first step, what about using the function WebUI.verifyTextPresent()? Yesterday, I use the solution give into the third message into a custom keyword but at the end, to use this function you don’t need to know where is the element right? :thinking:

1 Like