How to run jquery or javascript with WebUI.executeJavaScript

Hi there,
I’m starting with katalon, and I can not get these script to run, or it shows an error or returns null, even though executing the script from the browser and executes correctly, I want to know how many “tr” there are in the Table

System.out.println(WebUI.executeJavaScript(“\$(‘#gridEmpresas tbody tr’).length > 0”))

or

//System.out.println(WebUI.executeJavaScript(“document.getElementById(‘gridEmpresas’).getElementsByTagName(‘tr’)”, null))

I accept suggestions,

regards

<table id="gridEmpresas"> <tbody>
   <tr>
     <td>1st text</td>
   </tr>
   <tr>
     <td>2nd text</td>
   </tr>
   <tr>
     <td>N text</td>
   </tr> <tbody></table>

This should bring up an alert dialog with 3 in it.

WebUI.executeJavaScript("alert(document.getElementById('gridEmpresas').getElementsByTagName('tr').length)", null)

This should place 3 in the variable len:

def len = WebUI.executeJavaScript("document.getElementById('gridEmpresas').getElementsByTagName('tr').length", null)

Hope that helps

1 Like

present: FAILED because (of) Unable to execute JavaScript. (Root cause: org.openqa.selenium.JavascriptException: TypeError: document.getElementById(…) is null
don´t works but solve the problem with this way

WebDriver driver = DriverFactory.getWebDriver()

WebElement Table = driver.findElement(By.id(‘gridServicios’))

List rows_table = Table.findElements(By.tagName(‘tr’))

int rows_count = rows_table.size()

I have problem when I set value in a field text by using this command
String js = “”"
var clientTextField = document.querySelector("#PolarisTextField2");
clientTextField.value = ‘1000’;
clientTextField.onchange();
“”"
WebUI.executeJavaScript(js, null)
when I run this code , Katalon show the error like this
=============== ROOT CAUSE =====================

Caused by: org.openqa.selenium.JavascriptException: javascript error: clientTextField.onchange is not a function

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html

================================================

04-19-2021 09:20:28 AM Shopify_Merchant already set the invalid clientId MCH-0197-1687176144098 and invalid sharedKey SK-gTcg6LWFLD8SnO0BNW9p Mandiiri

Elapsed time: 1m - 15.095s

shopify.S002_FailedGeneratePaycode.Shopify_Merchant_already_set_the_invalid_clientId:110

Shopify_Merchant already set the invalid clientId MCH-0197-1687176144098 and invalid sharedKey SK-gTcg6LWFLD8SnO0BNW9p Mandiiri FAILED.

Reason:

com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:64)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:26)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.executeJavascript(ExecuteJavascriptKeyword.groovy:42)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.execute(ExecuteJavascriptKeyword.groovy:37)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:73)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4814)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$7.call(Unknown Source)

at shopify.S002_FailedGeneratePaycode.Shopify_Merchant_already_set_the_invalid_clientId(S002_FailedGeneratePaycode.groovy:110)

at ✽.Shopify_Merchant already set the invalid clientId MCH-0197-1687176144098 and invalid sharedKey SK-gTcg6LWFLD8SnO0BNW9p Mandiiri(/Users/lily/Documents/DokuProject/DokuTest/KatalonProject/devex-cms-automated-katalon/devex-cms-katalon/Include/features/Shopify/F002JokulShopifyInvalidClientIdSharedKey.feature:25)

Caused by: org.openqa.selenium.JavascriptException: javascript error: clientTextField.onchange is not a function

(Session info: chrome=90.0.4430.72)

Build info: version: ‘3.141.59’, revision: ‘e82be7d358’, time: ‘2018-11-14T08:25:53’

System info: host: ‘yhose.local’, ip: ‘127.0.0.1’, os.name: ‘Mac OS X’, os.arch: ‘x86_64’, os.version: ‘10.15.7’, java.version: ‘1.8.0_275’

Driver info: com.kms.katalon.selenium.driver.CChromeDriver

Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 90.0.4430.72, chrome: {chromedriverVersion: 90.0.4430.24 (4c6d850f087da…, userDataDir: /var/folders/2q/xsstzq9x2kv…}, goog:chromeOptions: {debuggerAddress: localhost:50493}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}

Session ID: 0e13dc49c6fd19f0be5d3cc454c05cce

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)

at com.kms.katalon.selenium.driver.CChromeDriver.execute(CChromeDriver.java:19)

at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)

at org.openqa.selenium.support.events.EventFiringWebDriver.lambda$new$1(EventFiringWebDriver.java:105)

at com.sun.proxy.$Proxy17.executeScript(Unknown Source)

at org.openqa.selenium.support.events.EventFiringWebDriver.executeScript(EventFiringWebDriver.java:229)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.doCall(ExecuteJavascriptKeyword.groovy:48)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword$_executeJavascript_closure1.call(ExecuteJavascriptKeyword.groovy)

at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:20)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.executeJavascript(ExecuteJavascriptKeyword.groovy:42)

at com.kms.katalon.core.webui.keyword.builtin.ExecuteJavaScriptKeyword.execute(ExecuteJavascriptKeyword.groovy:37)

at com.kms.katalon.core.keyword.internal.KeywordExecutor.executeKeywordForPlatform(KeywordExecutor.groovy:73)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4814)

at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$7.call(Unknown Source)

at shopify.S002_FailedGeneratePaycode.Shopify_Merchant_already_set_the_invalid_clientId(S002_FailedGeneratePaycode.groovy:110)

at cucumber.runtime.Utils$1.call(Utils.java:26)

at cucumber.runtime.Timeout.timeout(Timeout.java:16)

at cucumber.runtime.Utils.invoke(Utils.java:20)

at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:48)

at cucumber.runtime.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)

at cucumber.runner.TestStep.executeStep(TestStep.java:55)

at cucumber.runner.TestStep.run(TestStep.java:42)

at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:53)

at cucumber.runner.TestCase.run(TestCase.java:47)

at cucumber.runner.Runner.runPickle(Runner.java:44)

at cucumber.runtime.Runtime.runFeature(Runtime.java:120)

at cucumber.runtime.Runtime.run(Runtime.java:106)

at cucumber.api.cli.Main.run(Main.java:35)

at cucumber.api.cli.Main$run.call(Unknown Source)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$_runFeatureFile_closure1.doCall(CucumberBuiltinKeywords.groovy:106)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$_runFeatureFile_closure1.doCall(CucumberBuiltinKeywords.groovy)

at com.kms.katalon.core.keyword.internal.KeywordMain.runKeyword(KeywordMain.groovy:68)

at com.kms.katalon.core.keyword.internal.KeywordMain$runKeyword.call(Unknown Source)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:73)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile$0.callStatic(Unknown Source)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords.runFeatureFile(CucumberBuiltinKeywords.groovy:246)

at com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords$runFeatureFile.call(Unknown Source)

at WSVerification1618798826400.run(WSVerification1618798826400:2)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:178)

at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:172)

at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:155)

at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:147)

at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:129)

at com.kms.katalon.core.main.TestCaseMain.runFeatureFile(TestCaseMain.java:144)

at com.kms.katalon.core.main.TestCaseMain$runFeatureFile$0.call(Unknown Source)

at TempTempCase1618798824081.run(TempTempCase1618798824081.groovy:25)

The message tells you what you need to look at:

Caused by: org.openqa.selenium.JavascriptException: javascript error: clientTextField.onchange is not a function

So, you should remove the line that calls onchange():

String js = """
var clientTextField = document.querySelector("#PolarisTextField2");
clientTextField.value = ‘1000’;
// clientTextField.onchange();
"""
WebUI.executeJavaScript(js, null)

HTML Element does not have onchange function.

Check out onchange Event