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
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")
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.