Nguyen Khoi Nguyen
Katalon Apprentice
11/20/2016

Execute javascript

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?

Upvote
Quote

Comments

  • Katalon Studio
    Katalon Apprentice
    11/21/2016
    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.
    Upvote
    Quote
  • nguyen van nguyen
    Katalon Apprentice
    11/21/2016
    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
    Upvote
    Quote
  • Katalon Studio
    Katalon Apprentice
    11/22/2016
    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”)
    Upvote
    Quote
  • Nguyen Khoi Nguyen
    Katalon Apprentice
    11/23/2016
    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 ?
    Upvote
    Quote
  • Katalon Studio
    Katalon Apprentice
    11/24/2016
    Hi,

    Sorry for missing information. You need to import DriverFactory class if you its DriverFactory:
    import com.kms.katalon.core.webui.driver.DriverFactory;
    Thanks
    Upvote
    Quote
  • malkit singh
    Katalon Apprentice
    01/09/2017
    Hi,

    I imported DriverFactory as you mentioned but still I am getting unable to resolve class JavascriptExecutor error.
    Upvote
    Quote
  • Vinh Nguyen
    Katalon Expert
    01/18/2017
    Please add this import to solve your case:
    import org.openqa.selenium.JavascriptExecutor;
    Upvote
    Quote
  • John Mathews
    Katalon Apprentice
    08/22/2017
    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')
    Upvote
    Quote
  • Vinh Nguyen
    Katalon Expert
    08/22/2017
    What is the error message from your side?
    Upvote
    Quote
  • John Mathews
    Katalon Apprentice
    08/23/2017
    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
    Upvote
    Quote
  • Robert Wierzbicki
    Katalon Apprentice
    10/21/2017

    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.

    Upvote
    Quote
  • Balraj Alladi
    Katalon Apprentice
    10/31/2017

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

    Upvote
    Quote
  • Jhovanny Hernández
    Katalon Apprentice
    02/08/2018
    Thanks Balraj Alladi, I used your method and it works!
    Upvote
    Quote
  • tod2020@mail.ru
    Katalon Apprentice
    02/08/2018
    edited February 8
    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(".content
    div");
    var result = [];
    for (var i = 0; i<everyChild.length; i++) {
        result[i]=everyChild[i].getAttribute('id');
    }
     console.log(result);

    Upvote
    Quote
  • P Satish Patnaik
    Katalon Apprentice
    02/21/2018
    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 ??
    Upvote
    Quote
  • Russ Thomas
    Katalon Expert
    04/10/2018
    edited April 10
    @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".


    Upvote
    Quote
Sign In or Register to comment.