Unable to execute javascript

Hi there,

I want to execute this script in a Web UI test but I try to do it via javascript executor then it generates error. What the right way to do it?

document.querySelector(‘flt-glass-pane’).shadowRoot.querySelector(‘flt-semantics-placeholder’).click()

1 Like

Hi there,

Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.

Thanks!

These articles tell us that we can not test a Flutter application using Selenium Webdriver including Katalon Studio.

I found a site where I could find some Flutter applications in action

I looked at the Flutter applications with Chrome DevTools and found that these are impossible to test using Katalon Studio.

I’m actually able to do it. Only problem I’m getting is to execute this script. So, if I execute this in console of devtools then semantics gets visible on flutter web app. I just need to find a way to execute this script via Katalon Studio.

I just need to find a way to execute this script via Katalon Studio.

Is this what you want?

Yes, exactly. I’m trying this but when I try to execute it. It says ‘Unable to execute javascript’

WebUI.executeJavaScript(‘document.querySelector('flt-glass-pane').shadowRoot.querySelector('flt-semantics-placeholder')’, (arguments[0]).click())

1 Like

I do not understad your code. It does not make sense to me.

WebUI.executeJavaScript(
        ‘document.querySelector('flt-glass-pane').shadowRoot.querySelector('flt-semantics-placeholder')’,
        (arguments[0]).click()
//      ^^^^^^^^^^^^^^
)

What is arguments[0] here? Groovy language would not understand arguments[0] at all.

This is what I was thinking that something is wrong with my syntax. I have attached script as well as javascript executor input option. Can you please guide me what’s the right way to put it there?


What happens when you put that in the JavaScript console? Post a screenshot.

I would show you an runnable example Test Case script that calls WebUI.executeJavascript() keyword. Copy&paste the following as a new Test Case in your Katalon Studio and run it. It should work.

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement

TestObject makeTestObject(String selector) {
	return makeTestObject(selector, selector)
}
TestObject makeTestObject(String id, String cssSelector) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty('css', ConditionType.EQUALS, cssSelector)
	return tObj	
}

System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox")
String url = 'http://demoaut.katalon.com'
String anchorMakeAppointment = "a[id *= 'btn-make-appointment']"

WebUI.openBrowser(url)
WebUI.waitForElementPresent(makeTestObject(anchorMakeAppointment), 10)

//WebUI.click(makeTestObject(anchorMakeAppointment))
String js = "document.querySelector(\"${anchorMakeAppointment}\").click();"
WebUI.comment(js)
WebUI.executeJavaScript(js, null);

WebUI.verifyElementPresent(makeTestObject("input[id *= 'txt-username'"), 10)
WebUI.delay(3)
WebUI.closeBrowser()

I used the Katalon’s site http://demoaut.katalon.com as a target. You would want to change the target URL and the javascript code as you want. Please try it for yourself.

You haven’t shared the URL of your target, so that others in this forum would not be able to do any more.

If you want to use javascript to do something I think there are 2 ways

1 Use the keyword of katalon:
WebUI.executeJavaScript(“document.querySelector(‘flt-glass-pane’).shadowRoot.querySelector(‘flt-semantics-placeholder’).click()”, null);

2 Write a function in java and execute javascript (in fact, it is the same as katalon’s method, which calls selenium native methods).

public void executJS(String js) {
((JavascriptExecutor)driver).executeScript(js);
}

I hope it helps