---
name: Execute Async Script
short_description: Inject a snippet of JavaScript into the page for execution in the context of the currently selected frame (Web context only)

description:
  |
    The executed script is assumed to be asynchronous and must signal that is done by invoking the provided callback, which is always provided as the final argument to the function. The value to this callback will be returned to the client.

    Asynchronous script commands may not span page loads. If an unload event is fired while waiting for a script result, an error should be returned to the client.

    The script argument defines the script to execute in teh form of a function body. The function will be invoked with the provided args array and the values may be accessed via the arguments object in the order specified. The final argument will always be a callback function that must be invoked to signal that the script has finished.

    Arguments may be any JSON-primitive, array, or JSON object. JSON objects that define a WebElement reference will be converted to the corresponding DOM element. Likewise, any WebElements in the script result will be returned to the client as WebElement JSON objects.


example_usage:
  java:
    |
      ((JavascriptExecutor) driver).executeAsyncScript("window.setTimeout(arguments[arguments.length - 1], 500);");

  python:
    |
      self.driver.execute_async_script(‘document.title’)
  javascript_wd:
    |
      await driver.safeExecuteAsync('document.title');
  javascript_wdio:
    |
      browser.timeoutsAsyncScript(5000);
      var result = browser.executeAsync(function(a, b, c, d, done) {
          // browser context - you may access neither client nor console
          setTimeout(function() {
              done(a + b + c + d);
          }, 3000);
      }, 1, 2, 3, 4)

      // node.js context - client and console are available
      console.log(result.value); // outputs: 10
  ruby:
    |
      @driver.execute_async_script("document.title")
  php:
    |
      // TODO PHP sample
  csharp:
    |
      // TODO C# sample

client_docs:
  java: "https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#executeAsyncScript-java.lang.String-java.lang.Object...-"
  python: "http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_async_script"
  javascript_wdio: "http://webdriver.io/api/protocol/executeAsync.html"
  javascript_wd: "https://github.com/admc/wd/blob/master/lib/commands.js#L182"
  ruby: "http://www.rubydoc.info/gems/selenium-webdriver/Selenium/WebDriver/Driver:execute_async_script"
  php: "https://github.com/appium/php-client/" # TODO PHP documentation link
  csharp: "https://github.com/appium/appium-dotnet-driver/" # TODO Dotnet documentation link

# Driver support by platform
driver_support:
  ios:
    xcuitest: false
    uiautomation: false
  android:
    uiautomator2: false
    uiautomator: false
  mac:
    mac: false
  windows:
    windows: false
client_support:
  java: false
  python: false
  ruby: false
  php: false
  csharp: false
  javascript_wd: false
  javascript_wdio: false
  

# Information about the HTTP endpoints
endpoint:
  url: /wd/hub/session/:session_id/execute_async
  method: GET
  url_parameters:
    - name: session_id
      description: ID of the session to route the command to
  json_parameters:
    - name: script
      type: string
      description: The script to execute
    - name: args
      type: array
      description: The script arguments
  response:
    - type: any
      description: The script result

# Links to specifications. Should link to at least one specification
specifications: 
  w3c: https://www.w3.org/TR/webdriver/#dfn-execute-async-script
  jsonwp: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidexecute_async
