How can I use JavaScript in Katalon Studio?

2019-09-22 10:36发布

问题:

Sometimes, I encounter an element which I'm unable to click with a normal Selenium script so I have to use javascript to execute my script.

How can I use JavaScript in Katalon Studio?

回答1:

I believe you mean, Executng the Javascript code and get the element. You can try this appraoch - https://docs.katalon.com/display/KD/%5BWebUI%5D+Execute+JavaScript



回答2:

((JavascriptExecutor) DriverFactory.webDriver).executeScript('Your JavaScript-Code', argument);


回答3:

You can try using a custom keyword, inside of the package "yourPackage":

@Keyword
static clickJS (TestObject to, int timeout) {
    WebUI.waitForElementVisible(to, timeout)
    try {
        WebUI.click(to)
    }
    catch (Exception e) {
        WebDriver driver = DriverFactory.getWebDriver()
        WebElement element = WebUiCommonHelper.findWebElement(to, timeout)
        JavascriptExecutor executor = ((driver) as JavascriptExecutor)
        executor.executeScript('arguments[0].click()', element)
    }
    throw(e)
}

Then, you can call the function inside your test script with:

CustomKeyords.'yourPackage.yourClass.clickJS'(findTestObject("yourObject"))