Execute Driver Script
Run a WebdriverIO script against the current session, allowing execution of many commands in one Appium request. //对当前会话运行WebdriverIO脚本,允许在一个Appium请求中执行多个命令。
Example Usage
Java | String script = "const el = await driver.$('~foo');\n" + "await el.click();" driver.executeDriverScript(script, new ScriptOptions().withTimeout(200)); |
Python | import textwrap script = """ const el = await driver.$('~foo'); await el.click(); """ response = driver.execute_driver(script=textwrap.dedent(script)) |
Javascript | // webdriver.io example const script = ' const el = await driver.$('~foo'); await el.click(); ' ;await driver.executeDriver(script); // wd example const script = <br/>const el = await driver.$('~foo');<br/>await el.click();<br/> ;await driver.executeDriver(script, {timeout: 200}); |
Ruby | # ruby_lib example script = <<-SCRIPT const status = await driver.status(); return status; SCRIPT driver.execute_driver script: script # ruby_lib_core example @driver.screenshot_as(:base64) |
C# | // TODO C# sample |
PHP | // 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. //允许多命令批处理在一起一次执行,在executeScript模型上
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). //此命令接受三个参数,每个客户端都可以以自己的方式收集):脚本:由脚本本身组成的字符串超时:表示终止运行驱动程序脚本的进程之前等待的毫秒数的数字。默认值相当于1小时。*type:表示脚本语言/API的字符串。目前只支持一种类型webdriverio(这是默认的)
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. //代码必须用Javascript编写并且可以访问包含三个对象*driver的上下文:WebdriverIO driver对象,一个自定义的console对象具有log、warn、error方法
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. //这种使用WebdriverIO代码的方法的优点是,您可以访问完整的编程语言和appiumapi,并且可以使用您需要的任何语言或API特性,包括循环、条件和显式等待。此处无法枚举WebdriverIO API,请访问WebdriverIO文档以获取更多信息
Support
Appium Server
Appium Clients
Language | Support | Documentation |
---|---|---|
Java | All | appium.github.io |
Python | None | appium.github.io |
Javascript (WebdriverIO) | All | |
Javascript (WD) | All | github.com |
Ruby | None | www.rubydoc.info |
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
) //脚本结果。它将有两个字段:结果和日志。结果将是脚本的返回值。日志将包含脚本从控制台对象记录的任何内容。(任何)