Edit this Doc Execute Driver Script

Run a WebdriverIO script against the current session, allowing execution of many commands in one Appium request.

Example Usage

String script = "const el = await driver.$('~foo');\n"
              + "await el.click();"
driver.executeDriverScript(script, new ScriptOptions().withTimeout(200));

# TODO fill out once client code is written

// webdriver.io example
const script = `
  const el = await driver.$('~foo');
  await el.click();
`;
await driver.executeDriver(script);

// wd example
const script = `
  const el = await driver.$('~foo');
  await el.click();
`;
await driver.executeDriver(script, {timeout: 200});

# ruby_lib example
# TODO fill out once client code is written

# ruby_lib_core example
# TODO fill out once client code is written

// TODO C# sample

// TODO PHP sample

Description

One downside of Appium's client-server architecture is that each command must travel across a network with potentially high latency. This is especially the case in situations where the Appium session takes place on a service provider's host machine rather than locally.

This command enables the batching of many commands together, to be executed in one go on the Appium server. The way this is accomplished is on the model of executeScript: the client will send in a string representing code to be executed. The Appium server will execute that code in the context of the current session, and return any values specified by the script.

There are three parameters accepted by this command (which may be collected by each client in its own way): * script: the string consisting of the script itself * timeout: a number representing the number of milliseconds to wait before killing the process running the driver script. Default is equivalent to 1 hour. * type: a string representing the script language/API. Currently only one type, webdriverio, is supported (and it is the default).

Not just any code can run in this context. The code must be written in Javascript, and it will have access to a context with three objects * driver: a WebdriverIO driver object. It may be assumed this driver has already connected with the Appium server and is ready to run commands. The version of WebdriverIO used is the one installed according to the specification in appium-base-driver's package.json file. * console: a custom console object, with methods log, warn, and error, so that logging may take place. * Promise: a Promise library (Bluebird), to make asynchronous work easier.

The code will be placed inside an async function, as below, so you are free to use await:

(async function (driver, console, Promise) {
  // --> your script here <--
})()

Any errors will result in an error response to the call to this command. Any return values will be wrapped up and sent back to your client in the following form:

{result: <return value>, logs: {log: [], warn: [], error: []}}

Using this response object you can gather the return value as well as the output of any log statements you made.

The advantage of this approach of using WebdriverIO code is that you have access to a full programming language and Appium API, and can use any language or API features you need, including loops, conditionals, and explicit waits. The WebdriverIO API cannot be enumerated here, so visit the WebdriverIO documentation for more info.

Support

Appium Server

Platform Driver Platform Versions Appium Version Driver Version
iOS XCUITest 9.3+ 1.6.0+ All
UIAutomation 8.0 to 9.3 All All
Android UiAutomator2 ?+ 1.6.0+ All
Espresso ?+ 1.9.0+ All
UiAutomator 4.3+ All All
Mac Mac ?+ 1.6.4+ All
Windows Windows 10+ 1.6.0+ All

Appium Clients

Language Support Documentation
Java All javadoc.io
Python None
Javascript (WebdriverIO) None
Javascript (WD) None
Ruby None
PHP None
C# None

HTTP API Specifications

Endpoint

POST /session/:session_id/appium/execute_driver

URL Parameters

name description
session_id ID of the session to route the command to

JSON Parameters

name type description
script string The webdriverio script to execute
type string The name of the script type. Currently only 'webdriverio' is supported
timeout number The number of ms Appium should wait for the script to finish before killing it due to timeout

Response

The script result. It will have two fields: result and logs. Result will be the return value of the script. Logs will contain the content of anything logged from the console object by the script. (any)

See Also