Extend the width of an element

Hi All, I’m trying to extend the Bar of a chart by using the below code snippet.
First I extract the width from the style attribute then replace the new width and override the style attribute with the new width. But when I’m trying to execute the ‘WebUI.executeJavaScript’ to fetch them. I got the below error,

ERROR c.k.k.core.keyword.internal.KeywordMain - :x: Unable to execute JavaScript. (Root cause: com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript.

My code snippet,

1 Like

I find your code/screenshot hard to read, especially the almost muted quotes. As a result, I’m not going any further than pointing out where I think your problem lies.

  1. the style property on a DOM element is exactly that: a PROPERTY. In Groovy terms, that’s an object.

  2. the style attribute in HTML is used to build the style property.

As Mark Twain is quoted as saying, the problem aint what you don’t know, it’s what you think you know that aint so.

Lastly, why are you trying to modify the AUT?

1 Like

Don’t trim the StackTrace messages. Please show the full StackTrace messages after the line of “Unable to execute JavaScript”.


I guess, in the StackTrace message, you have a line

Caused by: java.lang.IllegalArgumentException: Argument is of an illegal type: com.kms.katalon.core.testobject.TestObject
     ...

This tells the reason of your failure.

1 Like

I made a Test Case script which mimics your code:

import org.openqa.selenium.WebElement

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

TestObject makeTestObject(String id, String selector) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("css", ConditionType.EQUALS, selector)
	return tObj	
}
String url = "https://katalon-demo-cura.herokuapp.com/"
TestObject tObj = makeTestObject("appointment", "#btn-make-appointment")
WebUI.openBrowser('')
WebUI.navigateToUrl(url)
WebElement element = WebUI.findWebElement(tObj, 30)
String js = """
//arguments[0].click();
return arguments[0]
"""

// (1)
String s = WebUI.executeJavaScript(js, Arrays.asList(element))
WebUI.comment("s:${s}")

// (2)
String t = WebUI.executeJavaScript(js, Arrays.asList(tObj))
WebUI.comment("t:${t}")

WebUI.closeBrowser()

When I ran this, I got the following output in the console. As found, the statement (1) worked, but the statement (2) raised an exception:

2023-08-27 15:58:47.065 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-08-27 15:58:47.069 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/TC2
Test Cases/TC2
[:]
2023-08-27 15:58:47.987 INFO  c.k.k.core.webui.driver.DriverFactory    - Starting 'Chrome' driver
8 27, 2023 3:58:47 午後 org.openqa.selenium.remote.DesiredCapabilities chrome
情報: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
2023-08-27 15:58:48.019 INFO  c.k.k.c.w.util.WebDriverPropertyUtil     - User set preference: ['property', '[--window-size=800,600]']
2023-08-27 15:58:48.066 INFO  c.k.k.core.webui.driver.DriverFactory    - Action delay is set to 0 milliseconds
Starting ChromeDriver 116.0.5845.96 (1a391816688002153ef791ffe60d9e899a71a037-refs/branch-heads/5845@{#1382}) on port 28858
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
8 27, 2023 3:58:51 午後 org.openqa.selenium.remote.ProtocolHandshake createSession
情報: Detected dialect: W3C
2023-08-27 15:58:51.410 INFO  c.k.k.core.webui.driver.DriverFactory    - sessionId = e09feea419066bff379f4b3dac9d5e07
2023-08-27 15:58:51.447 INFO  c.k.k.core.webui.driver.DriverFactory    - browser = Chrome 116.0.0.0
2023-08-27 15:58:51.449 INFO  c.k.k.core.webui.driver.DriverFactory    - platform = Mac OS X
2023-08-27 15:58:51.450 INFO  c.k.k.core.webui.driver.DriverFactory    - seleniumVersion = 3.141.59
2023-08-27 15:58:51.451 INFO  c.k.k.core.webui.driver.DriverFactory    - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2023-08-27 15:58:54.018 INFO  c.k.k.c.keyword.builtin.CommentKeyword   - s:[org.openqa.selenium.remote.RemoteWebElement@61998c91 -> unknown locator]
2023-08-27 15:58:54.086 ERROR c.k.k.core.keyword.internal.KeywordMain  - ❌ Unable to execute JavaScript. (Root cause: 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:74)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4873)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$2.call(Unknown Source)
	at TC2.run(TC2:27)
	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:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1693119522983.run(TempTestCase1693119522983.groovy:25)
Caused by: java.lang.IllegalArgumentException: Argument is of an illegal type: com.kms.katalon.core.testobject.TestObject
	at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:83)
	at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:484)
	at org.openqa.selenium.support.events.EventFiringWebDriver.lambda$new$1(EventFiringWebDriver.java:105)
	at com.sun.proxy.$Proxy13.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:74)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4873)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$2.call(Unknown Source)
	at Script1693117534056.run(Script1693117534056.groovy:27)
	... 11 more
)
2023-08-27 15:58:54.099 ERROR c.k.katalon.core.main.TestCaseExecutor   - ❌ Test Cases/TC2 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:74)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:4873)
	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$2.call(Unknown Source)
	at TC2.run(TC2:27)
	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:448)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
	at TempTestCase1693119522983.run(TempTestCase1693119522983.groovy:25)
Caused by: java.lang.IllegalArgumentException: Argument is of an illegal type: com.kms.katalon.core.testobject.TestObject
	at org.openqa.selenium.remote.internal.WebElementToJsonConverter.apply(WebElementToJsonConverter.java:83)
	at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:484)
	at org.openqa.selenium.support.events.EventFiringWebDriver.lambda$new$1(EventFiringWebDriver.java:105)
	at com.sun.proxy.$Proxy13.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)
	... 17 more

Test Cases/TC2
FAILED
2023-08-27 15:58:54.154 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC2

@Achini

Your code has the same mistake as my (2) statement.

2 Likes

Why the statement (1) works, but the statement (2) fails?

You can find the answer in the Javadoc of org.openqa.selenium.JavascriptExecutor#executeJavascript()

Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above. An exception will be thrown if the arguments do not meet these criteria. The arguments will be made available to the JavaScript via the “arguments” magic variable, as if the function were called via “Function.apply”

The statement (1) passes an instance of org.openqa.selenium.WebElement as an argument to WebUI.executeJavascript() call, which is accepted.

The statement (2) passes an instance of com.kms.katalon.core.testobject.TestObject as an argument, which is not accepted. Therefore your code fails.

2 Likes

The document of the WebUI.executeJavascript keyword is poorly written. It explains nothing about the rule of argument types. It explains nothing about the rule of types of returned values by the keyword.

Perhaps the document author thought that an ordinary Katalon user would never use this keyword. So they don’t have to write the keyword’s doc thoroughly. In fact, Katalon’s keyword WebUI.executeJavascript is merely a thin wrapper to the org.openqa.selenium.JavascriptExecutor. I think it is ok to assume that a user who demands the keyword would have been well experienced with org.openqa.selenium.JavascriptExecutor class already.

However, I think that the doc should show the link to the javadoc of Selenium project for reference. Otherwise users who don’t know the internal would loose their way.

@vu.tran

2 Likes

Yes, that is the issue. Now it’s working. Thanks for the solution.

1 Like