How can I verify whether a text is present using Javascript in Katalon

Hi,
I have a webpage where Katalon Spyweb does not recognize any object. So I need to use javascript in Katalon to verify whether a text is present on the webpage. Could you please help?

Regards,
David

Assuming you had a Test Case that navigated to this forum page, this code will display a JavaScript alert when it finds your name:

String js = 'document.querySelector("body").innerText.indexOf("DavidJohn") !== -1 && alert("found it");'
WebUI.executeJavaScript(js, null)

You can test the JavaScript independently in the browser console:

document.querySelector("body").innerText.indexOf("DavidJohn") !== -1 && alert("found it")

@DavidJohn

Could you show me the HTML of the page, and the console view of the browser when you use Spy Tool on it ? I am curious as to why Spy Tool is not recognizing any object.

@ThanhTo Actually for my test,I have to download an Html attachment from webmail.I need to check whether my expected text is present in that html file.Attaching the screenshot of the html file.

I assume as this is not a webpage,object spy cannot recognize the object. ![html|690x259]. So I wanted to know using JS can we check whether we can verify the text is present

@Russ_Thomas
I tried that in my script but did not work am I missing something?

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘file:///Users/david/Downloads/securedoc_20200329T212340.html’)

WebUI.delay(2)

WebUI.setEncryptedText(findTestObject(‘Object Repository/Page_Secure Registered Envelope PROD_HIGH T_69456f/input_Password_key1’),

‘7hui7XImQDASDASDASA==’)

WebUI.click(findTestObject(‘Object Repository/Page_Secure Registered Envelope PROD_HIGH T_69456f/input_Forgot password_openButton’))

String js = ‘document.querySelector(“body”).innerText.indexOf(“PROD_TEST”) !== -1 && alert(“found it”);’

WebUI.executeJavaScript(js, null)

If I am reading your screenshot correctly, you tried to search for “PROD_TEST” which is indeed not present. In which case, the code is working.

@Russ_Thomas. I get a popup when the match is found but the test passes, irrespective of whatever I search using javaScript.

Execute JavaScript ‘document.querySelector(“body”).innerText.indexOf(“sdasdasd”) !== -1 && alert(“found it”);’ successfully

Execute JavaScript ‘document.querySelector(“body”).innerText.indexOf(“PROD_HIGH”) !== -1 && alert(“found it”);’ successfully

Got it working using the keyword verify alert present.

I gave you an expression statement. To use essentially the same logic in an if() statement…

String js = 'document.querySelector("body").innerText.indexOf("DavidJohn") !== -1;'
boolean result = (boolean) WebUI.executeJavaScript(js, null)

if(result) {
 println("Text was found")
} else {
 println("Text was NOT found")
}

Note that I’m being paranoid about type/casting. Feel free to trim if you want.

1 Like