How do I make this work as a keyword?

Hi There,
After many failed attempts at finding some way to change the display style for a web pages entire HTML (That contains Iframes) I have found this to work. However when I attempt to turn this into a keyword it throws errors upon saving the keyword. Any advice on how to get this to work in the keyword section?

document.querySelectorAll("select").forEach(a=>$(a).show());
});
document.querySelectorAll(".fastcombo").forEach(a=>$(a).hide());
});
document.querySelectorAll('iframe').forEach(function(iframe){
iframe.contentWindow.document.body.getElementsByClassName("fastcombo").forEach(function(fastcombo){
$(fastcombo).hide();
});
document.querySelectorAll('iframe').forEach(function(iframe){
iframe.contentWindow.document.body.getElementsByTagName("select").forEach(function(select){
$(select).show();
));
))

so the first half changes the Display style for the parent and the second half changes the Display style within the Iframes.

Hi Joseph

Firstly, that’s a whole bunch of JavaScript. I’ve been away from Katalon for short while but as far as I’m aware, you can’t (never could) just throw that JavaScript in a script and expect it to work.

Did you make a copy-paste error? Where is the groovy code to execute that JavaScript?

Read this and follow the applicable steps:

Russ Thomas said:

Hi Joseph

Firstly, that’s a whole bunch of JavaScript. I’ve been away from Katalon for short while but as far as I’m aware, you can’t (never could) just throw that JavaScript in a script and expect it to work.

Did you make a copy-paste error? Where is the groovy code to execute that JavaScript?

Read this and follow the applicable steps:

http://forum.katalon.com/discussion/9337/tip-my-test-is-broken-nothing-works-please-help-me

Sorry about my misunderstanding of Groovy. It is very new to me. Let me show you what I am attempting to do. This IS just the javascript part of the Keyword I am attempting to create. I’m just not sure how to best convert Javascript into Groovy format.

Code Being Used in Keyword section of Katalon:

import internal.GlobalVariable
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.JavascriptExecutor
public class testkeyword {
@Keyword
def executeJavaScript() {
((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript('MyJavascript')
}
}

*The ‘MyJavascript’ is what you see in the original post*

Error that is thrown by Katalon when I save my keyword:



Keyword error.png

Firstly, you can execute your javascript string directly in the WebUI framework using the executeJavaScript method. Follow this link and see how far you get:

Regarding your error, you are supplying single-quotes inside a single-quoted string. Make the outer pair double-quotes:

"document.querySelectorAll('iframe')..."

In general terms,

groovy code here " javascript stuff here 'strings for javascript calls here' "

if that makes sense.

Russ Thomas said:

Firstly, you can execute your javascript string directly in the WebUI framework using the executeJavaScript method. Follow this link and see how far you get:

https://docs.katalon.com/katalon-studio/docs/webui-execute-javascript.html

Regarding your error, you are supplying single-quotes inside a single-quoted string. Make the outer pair double-quotes:

"document.querySelectorAll('iframe')..."

In general terms,

groovy code here " javascript stuff here 'strings for javascript calls here' "

if that makes sense.

That actually allowed me to save the script without any errors, however I am still noticing I am getting an error when the test is ran.

Script that is being utilized:

WebUI.executeJavaScript('document.querySelectorAll(\'iframe\').forEach(function(iframe){iframe.contentWindow.document.body.getElementsByClassName(\'fastcombo\').forEach(function(fastcombo){$(fastcombo).hide();',     [])

Error message being thrown when running Test:

Test Cases/Rollup Charm Test FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to execute JavaScript. (Root cause: org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: SyntaxError: missing ) after argument list  (Session info: chrome=69.0.3497.100)  (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)Command duration or timeout: 0 millisecondsBuild info: version: '3.7.1', revision: '8a0099a', time: '2017-11-06T21:07:36.161Z'System info: host: 'JOE10', ip: '192.168.15.68', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'Driver info: com.kms.katalon.selenium.driver.CChromeDriverCapabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.35.528161 (5b82f2d2aae0ca..., userDataDir: C:\Users\JCOVER~1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 69.0.3497.100, webStorageEnabled: true}Session ID: 93f68091e4f36bf72b41dc798e0e0ae0)	at com.kms.katalon.core.keyword.internal.KeywordMain.stepFailed(KeywordMain.groovy:36)	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.stepFailed(WebUIKeywordMain.groovy:65)	at com.kms.katalon.core.webui.keyword.internal.WebUIKeywordMain.runKeyword(WebUIKeywordMain.groovy:27)	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:53)	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.executeJavaScript(WebUiBuiltInKeywords.groovy:3787)	at com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords$executeJavaScript$3.call(Unknown Source)	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)	at Script1538770190665.run(Script1538770190665.groovy:41)	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:183)	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:108)	at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:294)	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:285)	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:264)	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:256)	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:200)	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:99)	at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:90)	at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)	at TempTestCase1539805482620.run(TempTestCase1539805482620.groovy:22)	at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)	at groovy.lang.GroovyShell.run(GroovyShell.java:518)	at groovy.lang.GroovyShell.run(GroovyShell.java:507)	at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)	at groovy.ui.GroovyMain.run(GroovyMain.java:384)	at groovy.ui.GroovyMain.process(GroovyMain.java:370)	at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)	at groovy.ui.GroovyMain.main(GroovyMain.java:109)	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)	at java.lang.reflect.Method.invoke(Method.java:498)	at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)	at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131))Test Cases/Rollup Charm Test.run:41

The JavaScript contains a syntax error. The error statement says:

Runtime.evaluate threw exception: SyntaxError: missing ) after argument list

so check your parentheses

Advice: there is a simple algorithm you can use to “count” parentheses. It goes like this:

Starting from the the beginning of your piece of code…

At the start, obviously, you have 0 (zero)

Each time you see a single **(** ADD 1

Each time you see a single **)** SUBTRACT 1

When you reach the end, your total should be zero - any other number means they don’t match.

Example:

( ( ( ) ( ) ) )
1 2 3 2 3 2 1 0