[WebUI] Execute JavaScript

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

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

1 Like

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.


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 !

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

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

1 Like

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?

1 Like

Thank you! It does!

1 Like

A post was split to a new topic: executeJavaScript/Click fails under Docker

A post was split to a new topic: How to use Test Case Variables in JavaScript

Please watch the below tutorial

I would propose to add one more sample code. for WebUI.executeJavaScript() keyword.

That is:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI


String js = """
let id = arguments[0];
let attributeName = arguments[1];
let e = document.getElementById(id);
return e.getAttribute(attributeName);

def placeholder = WebUI.executeJavaScript(js, ["txt-username", "placeholder"])

println "returned ${placeholder}"


This sample shows how to write a long JavaScript code which comprises with multi lines. This sample demonstrates how to use Groovy’s “triple double quoted string” syntax.

See also Execute JS always return null

1 Like