Execute javascript

arguments[0].click()

Edit: Which is a misleading error message because the reference to the click method is typeof === “function”.

Regardless, you still need to add open and close parentheses to effect the call.

Russ Thomas said:

arguments[0].click()

Edit: Which is a misleading error message because the reference to the click method is typeof === “function”.

Regardless, you still need to add open and close parentheses to effect the call.

Can you elaborate? I just basically copied what was out there on another thread. Still getting the hang of the syntax for javascript through JavascriptExecutor…

this is my package custom>clickJS.groovy:

public class clickJS {

@Keyword

def clickUsingJS(TestObject to) {

WebDriver driver = DriverFactory.getWebDriver()

WebElement element = WebUiCommonHelper.findWebElement(to, 30)

JavascriptExecutor executor = ((driver) as JavascriptExecutor)

executor.executeScript(‘arguments[0].click()’, element)

}

}

This is my usage in the test:
CustomKeywords.‘custom.clickJS.clickUsingJS’(findTestObject(‘target object’))

Jonathan

If you’re going to use JS to target an element, go the whole hog and use JS directly on the page to hit the element.

Here’s the issue: Because you’re using Katalon, you ended up with a Test Object. Because you have a Test Object, you need to convert it to a Web Element. Problem: JS doesn’t know what the hell a TO is. So, let’s take a step back…

JS can target the element directly. That, in essence, is what JS is for. You don’t need all that TO stuff getting in your way.

Give JS a CSS selector to the HTML element and then tell it to click the element. It can find the element just fine without any need for Test Object/WebElement.

void jsClicker(selector) {
  String js = '''
    document.querySelector(arguments[0]).click();
  '''
  WebUI.executeJavaScript(js, Arrays.asList(selector))
}

I use the triple-single-quote string so that it’s easy to type out multi-line strings. Like this “wordy” version which does the exact same thing…

void jsClicker(selector) {
  String js = '''
    var selector = arguments[0],
        elem = document.querySelector(selector);
    elem.click();
  '''
  WebUI.executeJavaScript(js, Arrays.asList(selector))
}

(Watch for typos, this code is right out of my head and untested).

Let me know how you get on.

@“Katalon Team” Because this thread is linked under “Top Topics” it keeps coming back. It’s probably the worst thread on how to use JS in groovy. The first four or five message including those from “Katalon Moderator” are so outdated archeologists will be digging them up soon.

Once again I implore you guys to edit this thread down and remove any extraneous/misleading/wrong examples. PLEASE.

5 Likes

Hi @Russ Thomas, I’m very new to Katalon, I need to execute a bit of javascript in my test but in my case what I need to do is set some element text, using your example above, I came up with this

void jsSetElementText(selector){

String js = ‘’’

document.getElementById(‘receipt_item_amount_received_0’).value = “4000”

‘’’

}

however I got the following error
FAILED because (of) (Stack trace: groovy.lang.MissingPropertyException: No such property: js for class: Script1536083801544

can you tell me where I went wrong?

I found a work around for my situation and just in case this issue was not unique to me I just used the following: WebUI.executeJavaScript('document.getElementById(\‘receipt_item_amount_received_0\’).value = “4000” ', null)

Hi Gavin

void jsSetElementText(selector){
 String js = '''
 document.getElementById('receipt_item_amount_received_0').value = "4000" 
'''
}

That method merely builds a string and then does nothing with it.

This might be what you’re trying to achieve…

void jsSetText(String selector, String val) {
  String js = '''
    var selector = arguments[0],
        val = arguments[1],
        elem = document.querySelector(selector);
    elem.value = val;
  '''
  WebUI.executeJavaScript(js, Arrays.asList(selector, val))
}

I have not checked this but it looks okay from here B)

This error TypeError: document.getelementbyid(…) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element. The solution is that you need to put your JavaScript code after the closure of the HTML element or more generally before < /body > tag.

True.

False. This has no bearing on a JavaScript IIFE injected by Katalon into an AUT.


@devalex88 Now I have the means, I’m going to clean up this thread (flagging posts that don’t benefit the user/reader).