Retrieve value from Javascript variable

Hi guys

Is it possible to find out the value of a Javascript variable in the scope of the AUT? I need to build a test case that will STOP_ON_FAILURE if the Javascript variable is true.

Thanks in advance.




If your Javascript function returns a variable, then you can assign it to your test variable as well:

Thanks for the link Vinh but that’s a minefield of errors mostly because I don’t understand the pure selenium environment or the Ruby answers presented.

If the answer you’re suggesting is present on that page, please point me to the actual answer AND please tell me the imports needed to use it (none of the examples explain the imports needed AFAICT). For example…

import whatever

def myvar = runJS(“jsvar”)


@Vinh Nguyen

Neither of these work:

def isDirty = WebUI.executeJavaScript(“isDirty”, null)

def isDirty = WebUI.executeJavaScript(“window.isDirty”, null)

isDirty, a boolean in JS, comes back null.

Wow. It certainly helps to RTFM :wink:

“The provided script fragment will be executed as the body of an anonymous function.”

def isDirty = WebUI.executeJavaScript(“return isDirty;”, null)

No I have my boolean!

To see it:

def isDirty = WebUI.executeJavaScript(“alert(isDirty);”, null)

Vinh, safe to close this.


1 Like

Here is the proper method:

// +++ Imports +++

import org.openqa.selenium.WebDriver as WebDriver
import org.openqa.selenium.JavascriptExecutor as JavascriptExecutor
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

// +++ Code +++

WebDriver driver = DriverFactory.getWebDriver()

JavascriptExecutor js = ((driver) as JavascriptExecutor)

String archiveID = js.executeScript(‘return window.archiveId;’)


1 Like

Old thread but YOU are a hero - this works fantastically for attaining javascript values. THANK YOU!