Skip to main content

[WebUI] Execute JavaScript

Description

Execute JavaScript on the currently selected frame or window. The provided script fragment will be executed as the body of an anonymous function.

Keyword name: executeJavaScript

Parameters

ParameterParameter TypeRequiredDescription
scriptStringYesThe JavaScript to execute.
argumentListYesThe arguments to the script. Can be empty or null.
flowControlFailureHandlingOptionalSpecify failure handling schema to determine whether the execution should be allowed to continue or stop.

Returns

  • Boolean, Long, Double, String, List, Web Element, or Null.

Example

Make an alert on the web page:

'Use Javascript to make an alert'
WebUI.executeJavaScript("alert('This is an alert')", null)

Return WebElement based on its id:

WebElement element = WebUI.executeJavaScript("return document.getElementById('someId');", null)

Interact with returned WebElement:

WebElement element = WebUiCommonHelper.findWebElement(findTestObject('your/object'),30)
WebUI.executeJavaScript("arguments[0].style.border='3px solid blue'", Arrays.asList(element))

Click on returned WebElement:

WebElement element = WebUiCommonHelper.findWebElement(findTestObject('your/object'),30)
WebUI.executeJavaScript("arguments[0].click()", Arrays.asList(element))
Was this page helpful?