[WebUI] Execute JavaScript


#1

This is a companion discussion topic for the original entry at https://docs.katalon.com/katalon-studio/docs/webui-execute-javascript.html

#2

The examples may require two Import statements to be added at the top of the test script:

import com.kms.katalon.core.webui.common.WebUiCommonHelper as WebUiCommonHelper

import org.openqa.selenium.WebElement as WebElement


#3

Hi,
The execute Javascript function returns the data in a Json Array. But in my project, I need to get it in a Json Object.

Can someone help?

Please let me know if it is possible, and I will share more details.

Thanks,
Siddharth.


#4

Hi @siddharth14377

Hi, the doc says it returns Boolean, Long, Double, String, List, Web Element, or Null. Are you talking about this keyword or something else ?

Cheers !


#5

when i code “WebElement element = WebUI.executeJavaScript(“return document.getElementById(‘pane-car-info’);”, null)”,it runs error.
the error shows below:
2019-07-08 19:27:07.566 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/checkCar FAILED.
Reason:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
file:/Users/lilyyang/Katalon%20Studio/preTest/Scripts/checkCar/Script1562154035195.groovy: 122: unable to resolve class WebElement
@ line 122, column 13.
WebElement element = WebUI.executeJavaScript(“return document.getElementById(‘pane-car-info’);”, null)
^

1 error

at com.kms.katalon.core.main.ScriptEngine.getScript(ScriptEngine.java:199)
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.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1562585223860.run(TempTestCase1562585223860.groovy:21)

#6

Missing import - press Ctrl - Shift - O to organize imports.


#7

Thank you for reply.However,when I pressing ctrl-shift-o, nothing appended.Is there any other way to import?or by right-click something?


#8

Thank you! It does!