How to verify occurence of a text in a webpage

Hi everyone,

Is it possible to verify that a text element is only present once in the webpage?

Many thanks

1 Like

Yes. You can get the text element via a statement and then verify that you only have one.

import org.openqa.selenium.By as By
import org.openqa.selenium.Keys as Keys
import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

WebDriver driver = DriverFactory.getWebDriver();

// get all the text elements that are present
List<WebElement> ourText = driver.findElements(By.xpath('//input'))
WebUI.verifyEqual(ourText.size(), 1, FailureHandling.STOP_ON_FAILURE)

Edit: you can also do it if you have a Test Object

import org.openqa.selenium.WebElement as WebElement
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebHelper

List<WebElement> ourText = WebHelper.findWebElements(findTestObject('...'), 10);
WebUI.verifyEqual(ourText.size(), 1, FailureHandling.STOP_ON_FAILURE)

Edit2: you will not be able to get the object by using the <id> attribute as that would be unique, so you won’t get any others. You likely will have to get the object by <class> or some other attribute that would be common to the other text elements.

Edit3: I added STOP_ON_FAILURE but if you don’t want to, then remove if from the parameter list.
WebUI.verifyEqual(ourText.size(), 1)