Execute javascript


#1

Sometimes, I’ve met the element which is unable to click with the normal script by selenium so I have to use javascript to execute my script.

How can I write javascript on Katalon tool?


#2

the error message is this:

Test Cases/IconBooking FAILED because (of) org.openqa.selenium.WebDriverException: document.getElementById(…) is null

Note: IconBooking is the name of my test case


#3

Hi All,

I did both the imports as said above and used the following script but I get error: Can someone help please?

((JavascriptExecutor) driver).executeScript(“document.getElementById(‘txtdepartdate’).removeAttribute(‘readonly’,0);”);
WebUI.setText(findTestObject(‘Page_ICON Electronic Booking Form/img_imgdepartdate’), ‘18-Aug-2017’)


#4

Hi,

I imported DriverFactory as you mentioned but still I am getting unable to resolve class JavascriptExecutor error.


#5

Hello follow your direction, but unfortunately I’ve still got error message. Here my demo script

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘m.sofitel.com’)

WebUI.click(findTestObject(‘Sofitel/Login_button’))

WebUI.delay(5)

((JavascriptExecutor)ChromeDriver()).executeScript(“document.getElementById(\‘createaccount\’).click()”)

WebUI.delay(5)

WebUI.closeBrowser()

Katalo is unable to execute my script with error message showing “Test Cases/New Test Case FAILED because (of) Variable ‘DriverFactory’ is not defined for test case.”

Do I need to create a class DirverFactory or Can I call from Katalon ?


#6

1. Follow your guidance, I’ve tried but It didn’t work out. I imported the library java javascript:

import org.openqa.selenium.JavascriptExecutor and write directly in my script

((JavascriptExecutor)WebUI).executeScript(“myjavascript”)

2. When I execute it, Katalon showing the error message:

Test Cases/Web UI/Demo/New Test Case FAILED because (of) org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object ‘class com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords’ with class ‘java.lang.Class’ to class ‘org.openqa.selenium.JavascriptExecutor’

Did I do something wrong or I need to do something else ?

Would you please to help me


#7

What is the error message from your side?


#8

Please add this import to solve your case:

import org.openqa.selenium.JavascriptExecutor;


#9

Hi,

Sorry for missing information. You need to import DriverFactory class if you its DriverFactory:

import com.kms.katalon.core.webui.driver.DriverFactory;

Thanks


#10

Hi,

This segment of code is incorrect because you can’t directly refer to WebUI class

((JavascriptExecutor)WebUI).executeScript(“myjavascript”)

The current’s instance of web driver must be used instead.
Please change it to :

((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript(“myjavascript”)


#11

Hi,

You can create ‘Execute Javascript’ custom keyword and use it in your test script directly:

@Keyword
def executeJavaScript(String javascript) {
((JavascriptExecutor) DriverFactory.getWebDriver()).executeScript(javascript)
}

We will consider adding a ticket to allow javascript execution as part of built-in keyword later.


#12

What Katalon Studio wrote above works for me but also needed to add:
import org.openqa.selenium.JavascriptExecutor
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory

As a useless example in my test case I put
CustomKeywords.‘com.example.WebUICustomKeywords.executeJavaScript’(‘window.alert(“test”); delay = 5000; var start = new Date().getTime(); while (new Date().getTime() < start + delay);’)

This shows an alert message of ‘test’ for 5 seconds before moving on.


#13

You can use this custom function and can call any Test object which you want to click.

image.png


#14

Thanks Balraj Alladi, I used your method and it works!


#15

How can i execute this Java-code(several line) for get All ID from class named “content” in page and get Katalon-Array?

var everyChild =document.querySelectorAll(".contentdiv");
var result = [];
for (var i = 0;i<everyChild.length; i++) {
   result[i]=everyChild[i].getAttribute('id');
}
 console.log(result);

#16

Hi,Balraj Alladi

Can you give us the full program implemented using the custom function which you used so that we can follow the exact way and try to replicate?I am facing issue for a long time in date picker of android application, but not getting the solution, can anyone help me out in this ??


#17

@Katalon Team

Since this thread appears in the “Top Topics” listing, it should be pruned back to the original question and a single answer containing:

Use WebUI.executeJavaScript

https://docs.katalon.com/display/KD/[WebUI]+Execute+JavaScript

The rest of this thread is at best “noise” and at worst “wrong”.


#18

Hi,
I got this message on step click an object to show innerpop.

Test Cases/Setting/Login - Add FAILED because (of) Unable to click on object ‘Object Repository/Page_Sys (1)/div_ADD’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page_Sys (1)/div_ADD’ located by ‘By.xpath: //div[@id = ‘ADD’ and @title = ‘Add’]’ not found)

I already put delay after the click item.

and use this script to show the inner pop

WebUI.executeJavaScript(‘top.InnerPop’, [’?xfid=hrm.setting.add’])

WebUI.delay(1)

but the inner pop still didn’t show up.

what should I do to resolve this?

Thanks.


#19

Hello,

I am facing one similar kind of issue while clicking on hyperlik from the webpage using katalon studio. I create test script using “Selenium C# in Visual studio” and able to run easily. So i don’t know what is the issue in Katalon script

Steps:
1. open Url : https://jqueryui.com/
2. Maximize the window
3. Click on “Draggable” link option under “Interactions”

I am facing below issue

Issue :

Test Cases/Functional Test/IframeTest FAILED because (of) org.codehaus.groovy.runtime.InvokerInvocationException: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Iframe/Page_Droppable jQuery UI/a_Draggable’ located by ‘By.xpath: //a[@href = ‘http://jqueryui.com/draggable/’ and (text() = ‘Draggable’ or . = ‘Draggable’)]’ not found

utility.UtilityClass.invokeMethod:0

Test Cases/Functional Test/IframeTest.run:36

Please do the needful and guide me on that


#20

I’m getting the following error when trying to use the javascript click:

Test Cases/test1 FAILED because (of) org.openqa.selenium.WebDriverException: unknown error: arguments[0].click is not a function