Selenium: Execute Async Script, callback doesn't get called inside window.setTimeout

Created on 21 Jul 2016  路  4Comments  路  Source: SeleniumHQ/selenium

Meta -

OS: Arch Linux (os.version: '4.6.4-1-ARCH')
Selenium Version: 2.53.0
Browser: chromium, chrome, firefox
Browser Version:

Expected Behavior -

const webdriver = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

// Tried with firefox, google-chrome and chromium
let options = new chrome.Options();
options.setChromeBinaryPath('/usr/bin/google-chrome-stable');

let driver = new webdriver.Builder()
        .forBrowser('firefox')
        .usingServer('http://localhost:4444/wd/hub')
        //.setChromeOptions(options)
        .build();


driver.get('http://google.com');
driver.executeAsyncScript(() => {
    var callback = arguments[arguments.length - 1];
    window.setTimeout(() => {
        callback({hamid: 'hamid'});
    }, 500)
})

It's supposed to call the callback after setTimeout, but got an error.

Actual Behavior -

17:20:05.585 INFO - Executing: [execute async script: return (() => {
    var callback = arguments[arguments.length - 1];
    window.setTimeout(() => {
        callback({hamid: hamid});
    }, 500)
}).apply(null, arguments);, []])
17:20:05.718 WARN - Exception thrown
org.openqa.selenium.TimeoutException: Timed out waiting for async script result after 1ms
Command duration or timeout: 129 milliseconds
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'

All 4 comments

you have to first set the execute script timeout, it defaults to 0

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_Timeouts.html#setScriptTimeout

webdriver.manage().timeouts().setScriptTimeout(10000); // 10 seconds

I think the documentation may be off a bit, i don't believe it's accurate that 0 means 'indefinite' looks like 0 means, must return immediately :)

The documentation of executeAsyncScript doesn't mention it, even on the examples.

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_WebDriver.html#executeAsyncScript

Thank you.

@jleyba might want to update some of those docs?

Was this page helpful?
0 / 5 - 0 ratings