Skip to main content

AndroidDevice

[AndroidDevice][AndroidDevice] 代表连接的设备,可以是真实的硬件,也可以是模拟的。可以使用 android.devices() 获取设备。

¥[AndroidDevice] represents a connected device, either real hardware or emulated. Devices can be obtained using android.devices().


方法

¥Methods

close

Added in: v1.9 androidDevice.close

与设备断开连接。

¥Disconnects from the device.

用法

¥Usage

await androidDevice.close();

返回

¥Returns

  • [Promise]<[void]>#

drag

Added in: v1.9 androidDevice.drag

selector 定义的小部件拖向 dest 点。

¥Drags the widget defined by selector towards dest point.

用法

¥Usage

await androidDevice.drag(selector, dest);
await androidDevice.drag(selector, dest, options);

参数

¥Arguments

  • selector [AndroidSelector]#

要拖动的选择器。

¥Selector to drag.

  • dest [Object]#

    • x [number]

    • y [number]

拖动到的点。

¥Point to drag to.

  • options [Object] (optional)

    • speed [number] (optional)#

可选的拖动速度(以每秒像素为单位)。

¥Optional speed of the drag in pixels per second.

  • timeout [number] (optional)#

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns

  • [Promise]<[void]>#

fill

Added in: v1.9 androidDevice.fill

text 填充特定的 selector 输入框。

¥Fills the specific selector input box with text.

用法

¥Usage

await androidDevice.fill(selector, text);
await androidDevice.fill(selector, text, options);

参数

¥Arguments

  • selector [AndroidSelector]#

要填充的选择器。

¥Selector to fill.

  • text [string]#

输入框中要填写的文本。

¥Text to be filled in the input box.

  • options [Object] (optional)

    • timeout [number] (optional)#

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns

  • [Promise]<[void]>#

fling

Added in: v1.9 androidDevice.fling

selector 定义的小部件扔到指定的 direction 中。

¥Flings the widget defined by selector in the specified direction.

用法

¥Usage

await androidDevice.fling(selector, direction);
await androidDevice.fling(selector, direction, options);

参数

¥Arguments

  • selector [AndroidSelector]#

选择器来投掷。

¥Selector to fling.

  • direction "down"|"up"|"left"|"right"#

    投掷方向。

    ¥Fling direction.

  • options [Object] (optional)

    • speed [number] (optional)#

可选的投射速度(以每秒像素为单位)。

¥Optional speed of the fling in pixels per second.

  • timeout [number] (optional)#

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns

  • [Promise]<[void]>#

info

Added in: v1.9 androidDevice.info

返回有关 selector 定义的小部件的信息。

¥Returns information about a widget defined by selector.

用法

¥Usage

await androidDevice.info(selector);

参数

¥Arguments

  • selector [AndroidSelector]#

返回信息的选择器。

¥Selector to return information about.

返回

¥Returns

  • [Promise]<[AndroidElementInfo]>#

installApk

Added in: v1.9 androidDevice.installApk

在设备上安装 apk。

¥Installs an apk on the device.

用法

¥Usage

await androidDevice.installApk(file);
await androidDevice.installApk(file, options);

参数

¥Arguments

  • file [string]|[Buffer]#

apk 文件的路径或 apk 文件内容。

¥Either a path to the apk file, or apk file content.

  • options [Object] (optional)

    • args [Array]<[string]> (optional)#

传递给 shell:cmd package install 调用的可选参数。默认为 -r -t -S

¥Optional arguments to pass to the shell:cmd package install call. Defaults to -r -t -S.

返回

¥Returns

  • [Promise]<[void]>#

launchBrowser

Added in: v1.9 androidDevice.launchBrowser

在设备上启动 Chrome 浏览器,并返回其持久上下文。

¥Launches Chrome browser on the device, and returns its persistent context.

用法

¥Usage

await androidDevice.launchBrowser();
await androidDevice.launchBrowser(options);

参数

¥Arguments

  • options [Object] (optional)

    • acceptDownloads [boolean] (optional)#

是否自动下载所有附件。默认为 true,接受所有下载。

¥Whether to automatically download all the attachments. Defaults to true where all the downloads are accepted.

  • args [Array]<[string]> (optional) Added in: v1.29#
warning

Use custom browser args at your own risk, as some of them may break Playwright functionality.

要传递给浏览器实例的其他参数。Chromium 标志列表可以在 此处 中找到。

¥Additional arguments to pass to the browser instance. The list of Chromium flags can be found here.

  • baseURL [string] (optional)#

当使用 page.goto()page.route()page.waitForURL()page.waitForRequest()page.waitForResponse() 时,它会使用 URL() 构造函数来构建相应的 URL,从而考虑基本 URL。默认情况下取消设置。示例:

¥When using page.goto(), page.route(), page.waitForURL(), page.waitForRequest(), or page.waitForResponse() it takes the base URL in consideration by using the URL() constructor for building the corresponding URL. Unset by default. Examples:

  • baseURL:http://localhost:3000 并导航至 /bar.html 结果为 http://localhost:3000/bar.html

    ¥baseURL: http://localhost:3000 and navigating to /bar.html results in http://localhost:3000/bar.html

  • baseURL:http://localhost:3000/foo/ 并导航至 ./bar.html 结果为 http://localhost:3000/foo/bar.html

    ¥baseURL: http://localhost:3000/foo/ and navigating to ./bar.html results in http://localhost:3000/foo/bar.html

  • baseURL:http://localhost:3000/foo(无尾部斜杠)并导航至 ./bar.html 将导致 http://localhost:3000/bar.html

    ¥baseURL: http://localhost:3000/foo (without trailing slash) and navigating to ./bar.html results in http://localhost:3000/bar.html

    • bypassCSP [boolean] (optional)#

切换绕过页面的内容安全策略。默认为 false

¥Toggles bypassing page's Content-Security-Policy. Defaults to false.

  • colorScheme [null]|"light"|"dark"|"no-preference" (optional)#

模拟 'prefers-colors-scheme' 媒体功能,支持的值为 'light''dark''no-preference'。详细信息请参见 page.emulateMedia()。传递 null 会将模拟重置为系统默认值。默认为 'light'

¥Emulates 'prefers-colors-scheme' media feature, supported values are 'light', 'dark', 'no-preference'. See page.emulateMedia() for more details. Passing null resets emulation to system defaults. Defaults to 'light'.

  • command [string] (optional)#

用于启动而不是默认的 Android Chrome 的可选包名称。

¥Optional package name to launch instead of default Chrome for Android.

  • deviceScaleFactor [number] (optional)#

指定设备比例因子(可以视为 dpr)。默认为 1。了解有关 使用设备比例因子模拟设备 的更多信息。

¥Specify device scale factor (can be thought of as dpr). Defaults to 1. Learn more about emulating devices with device scale factor.

  • extraHTTPHeaders [Object]<[string], [string]> (optional)#

包含随每个请求发送的附加 HTTP 标头的对象。默认为无。

¥An object containing additional HTTP headers to be sent with every request. Defaults to none.

  • forcedColors [null]|"active"|"none" (optional)#

模拟 'forced-colors' 媒体功能,支持的值为 'active''none'。详细信息请参见 page.emulateMedia()。传递 null 会将模拟重置为系统默认值。默认为 'none'

¥Emulates 'forced-colors' media feature, supported values are 'active', 'none'. See page.emulateMedia() for more details. Passing null resets emulation to system defaults. Defaults to 'none'.

  • geolocation [Object] (optional)#

    • latitude [number]

纬度在 -90 到 90 之间。

¥Latitude between -90 and 90.

  • longitude [number]

经度在 -180 到 180 之间。

¥Longitude between -180 and 180.

  • accuracy [number] (optional)

非负精度值。默认为 0

¥Non-negative accuracy value. Defaults to 0.

  • hasTouch [boolean] (optional)#

指定视口是否支持触摸事件。默认为 false。了解有关 移动模拟 的更多信息。

¥Specifies if viewport supports touch events. Defaults to false. Learn more about mobile emulation.

  • httpCredentials [Object] (optional)#

    • username [string]

    • password [string]

    • origin [string] (optional)

限制在特定来源 (scheme://host:port) 上发送 http 凭据。

¥Restrain sending http credentials on specific origin (scheme://host:port).

HTTP 认证 的凭证。如果未指定来源,则用户名和密码将在未经授权的响应时发送到任何服务器。

¥Credentials for HTTP authentication. If no origin is specified, the username and password are sent to any servers upon unauthorized responses.

  • ignoreHTTPSErrors [boolean] (optional)#

发送网络请求时是否忽略 HTTPS 错误。默认为 false

¥Whether to ignore HTTPS errors when sending network requests. Defaults to false.

  • isMobile [boolean] (optional)#

是否考虑 meta viewport 标签并启用触摸事件。isMobile 是设备的一部分,因此你实际上不需要手动设置它。默认为 false,Firefox 不支持。了解有关 移动模拟 的更多信息。

¥Whether the meta viewport tag is taken into account and touch events are enabled. isMobile is a part of device, so you don't actually need to set it manually. Defaults to false and is not supported in Firefox. Learn more about mobile emulation.

  • javaScriptEnabled [boolean] (optional)#

是否在上下文中启用 JavaScript。默认为 true。了解有关 禁用 JavaScript 的更多信息。

¥Whether or not to enable JavaScript in the context. Defaults to true. Learn more about disabling JavaScript.

  • locale [string] (optional)#

指定用户区域设置,例如 en-GBde-DE 等。区域设置会影响 navigator.language 值、Accept-Language 请求标头值以及数字和日期格式规则。默认为系统默认区域设置。在我们的 模拟指南 中了解有关模拟的更多信息。

¥Specify user locale, for example en-GB, de-DE, etc. Locale will affect navigator.language value, Accept-Language request header value as well as number and date formatting rules. Defaults to the system default locale. Learn more about emulation in our emulation guide.

  • logger [Logger] (optional)#

用于 Playwright 日志记录的日志器接收器。

¥Logger sink for Playwright logging.

  • offline [boolean] (optional)#

是否模拟网络离线。默认为 false。了解有关 网络模拟 的更多信息。

¥Whether to emulate network being offline. Defaults to false. Learn more about network emulation.

  • permissions [Array]<[string]> (optional)#

授予此上下文中所有页面的权限列表。详细信息请参见 browserContext.grantPermissions()。默认为无。

¥A list of permissions to grant to all pages in this context. See browserContext.grantPermissions() for more details. Defaults to none.

  • proxy [Object] (optional) Added in: v1.29#

    • server [string]

用于所有请求的代理。支持 HTTP 和 SOCKS 代理,例如 http://myproxy.com:3128socks5://myproxy.com:3128。缩写形式 myproxy.com:3128 被视为 HTTP 代理。

¥Proxy to be used for all requests. HTTP and SOCKS proxies are supported, for example http://myproxy.com:3128 or socks5://myproxy.com:3128. Short form myproxy.com:3128 is considered an HTTP proxy.

  • bypass [string] (optional)

用于绕过代理的可选逗号分隔域,例如 ".com, chromium.org, .domain.com"

¥Optional comma-separated domains to bypass proxy, for example ".com, chromium.org, .domain.com".

  • username [string] (optional)

如果 HTTP 代理需要身份验证,则使用可选的用户名。

¥Optional username to use if HTTP proxy requires authentication.

  • password [string] (optional)

如果 HTTP 代理需要身份验证,则使用可选密码。

¥Optional password to use if HTTP proxy requires authentication.

网络代理设置。

¥Network proxy settings.

  • recordHar [Object] (optional)#

    • omitContent [boolean] (optional)

用于控制是否省略 HAR 中的请求内容的可选设置。默认为 false。已弃用,请改用 content 策略。

¥Optional setting to control whether to omit request content from the HAR. Defaults to false. Deprecated, use content policy instead.

  • content "omit"|"embed"|"attach"(可选)

    ¥content "omit"|"embed"|"attach" (optional)

    用于控制资源内容管理的可选设置。如果指定了 omit,则不会保留内容。如果指定了 attach,资源将作为单独的文件或条目保留在 ZIP 存档中。如果指定了 embed,则根据 HAR 规范将内容内联存储在 HAR 文件中。对于 .zip 输出文件,默认为 attach;对于所有其他文件扩展名,默认为 embed

    ¥Optional setting to control resource content management. If omit is specified, content is not persisted. If attach is specified, resources are persisted as separate files or entries in the ZIP archive. If embed is specified, content is stored inline the HAR file as per HAR specification. Defaults to attach for .zip output files and to embed for all other file extensions.

    • path [string]

将 HAR 文件写入到的文件系统上的路径。如果文件名以 .zip 结尾,则默认使用 content: 'attach'

¥Path on the filesystem to write the HAR file to. If the file name ends with .zip, content: 'attach' is used by default.

  • mode "full"|"minimal"(可选)

    ¥mode "full"|"minimal" (optional)

    当设置为 minimal 时,仅记录从 HAR 路由所需的信息。这会忽略从 HAR 重放时不使用的大小、计时、页面、cookie、安全性和其他类型的 HAR 信息。默认为 full

    ¥When set to minimal, only record information necessary for routing from HAR. This omits sizes, timing, page, cookies, security and other types of HAR information that are not used when replaying from HAR. Defaults to full.

    • urlFilter [string]|[RegExp] (optional)

用于过滤存储在 HAR 中的请求的 glob 或正则表达式模式。当通过上下文选项提供 baseURL 并且传递的 URL 是路径时,它将通过 new URL() 构造函数合并。默认为无。

¥A glob or regex pattern to filter requests that are stored in the HAR. When a baseURL via the context options was provided and the passed URL is a path, it gets merged via the new URL() constructor. Defaults to none.

启用所有页面的 HAR 记录到 recordHar.path 文件中。如果未指定,则不会记录 HAR。确保等待 browserContext.close() 才能保存 HAR。

¥Enables HAR recording for all pages into recordHar.path file. If not specified, the HAR is not recorded. Make sure to await browserContext.close() for the HAR to be saved.

  • recordVideo [Object] (optional)#

    • dir [string]

放置视频的目录路径。

¥Path to the directory to put videos into.

  • size [Object] (optional)

    • width [number]

视频帧宽度。

¥Video frame width.

  • height [number]

视频帧高度。

¥Video frame height.

录制视频的可选尺寸。如果未指定,大小将等于 viewport 缩小以适合 800x800。如果未明确配置 viewport,则视频大小默认为 800x450。如有必要,每页的实际图片将按比例缩小以适合指定的尺寸。

¥Optional dimensions of the recorded videos. If not specified the size will be equal to viewport scaled down to fit into 800x800. If viewport is not configured explicitly the video size defaults to 800x450. Actual picture of each page will be scaled down if necessary to fit the specified size.

启用 recordVideo.dir 目录中所有页面的视频录制。如果未指定,则不会录制视频。请务必等待 browserContext.close() 才能保存视频。

¥Enables video recording for all pages into recordVideo.dir directory. If not specified videos are not recorded. Make sure to await browserContext.close() for videos to be saved.

  • reducedMotion [null]|"reduce"|"no-preference" (optional)#

模拟 'prefers-reduced-motion' 媒体功能,支持的值为 'reduce''no-preference'。详细信息请参见 page.emulateMedia()。传递 null 会将模拟重置为系统默认值。默认为 'no-preference'

¥Emulates 'prefers-reduced-motion' media feature, supported values are 'reduce', 'no-preference'. See page.emulateMedia() for more details. Passing null resets emulation to system defaults. Defaults to 'no-preference'.

  • screen [Object] (optional)#

    • width [number]

页面宽度(以像素为单位)。

¥page width in pixels.

  • height [number]

页面高度(以像素为单位)。

¥page height in pixels.

通过 window.screen 模拟网页内可用的一致窗口屏幕尺寸。仅当设置了 viewport 时才使用。

¥Emulates consistent window screen size available inside web page via window.screen. Is only used when the viewport is set.

  • serviceWorkers "allow"|"block"(可选)#

    ¥serviceWorkers "allow"|"block" (optional)#

    是否允许站点注册 Service Worker。默认为 'allow'

    ¥Whether to allow sites to register Service workers. Defaults to 'allow'.

    • 'allow'服务工作进程 可以注册。

      ¥'allow': Service Workers can be registered.

    • 'block':Playwright 将阻止所有服务工作进程的注册。

      ¥'block': Playwright will block all registration of Service Workers.

    • strictSelectors [boolean] (optional)#

如果设置为 true,则为此上下文启用严格选择器模式。在严格选择器模式下,当多个元素与选择器匹配时,所有暗示单个目标 DOM 元素的选择器操作都会抛出异常。此选项不会影响任何定位器 API(定位器始终是严格的)。默认为 false。请参阅 [Locator][Locator] 了解有关严格模式的更多信息。

¥If set to true, enables strict selectors mode for this context. In the strict selectors mode all operations on selectors that imply single target DOM element will throw when more than one element matches the selector. This option does not affect any Locator APIs (Locators are always strict). Defaults to false. See [Locator] to learn more about the strict mode.

  • timezoneId [string] (optional)#

更改上下文的时区。有关支持的时区 ID 的列表,请参阅 ICU 的 metaZones.txt。默认为系统时区。

¥Changes the timezone of the context. See ICU's metaZones.txt for a list of supported timezone IDs. Defaults to the system timezone.

  • userAgent [string] (optional)#

在此上下文中使用的特定用户代理。

¥Specific user agent to use in this context.

  • videoSize [Object] (optional)#
Deprecated

Use recordVideo instead.

  • width [number]

视频帧宽度。

¥Video frame width.

  • height [number]

视频帧高度。

¥Video frame height.

  • videosPath [string] (optional)#
Deprecated

Use recordVideo instead.

  • viewport [null]|[Object] (optional)#

    • width [number]

页面宽度(以像素为单位)。

¥page width in pixels.

  • height [number]

页面高度(以像素为单位)。

¥page height in pixels.

模拟每个页面的一致视口。默认为 1280x720 视口。使用 null 禁用一致视口模拟。了解有关 视口模拟 的更多信息。

¥Emulates consistent viewport for each page. Defaults to an 1280x720 viewport. Use null to disable the consistent viewport emulation. Learn more about viewport emulation.

note

null value opts out from the default presets, makes viewport depend on the host window size defined by the operating system. It makes the execution of the tests non-deterministic. :::这

返回

¥Returns


longTap

Added in: v1.9androidDevice.longTap

selector 定义的小部件上执行长按。

¥Performs a long tap on the widget defined by selector.

用法

¥Usage

await androidDevice.longTap(selector);
await androidDevice.longTap(selector, options);

参数

¥Arguments

  • selector [AndroidSelector]#

要点击的选择器。

¥Selector to tap on.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


model

Added in: v1.9androidDevice.model

设备型号。

¥Device model.

用法

¥Usage

androidDevice.model();

返回

¥Returns


open

Added in: v1.9androidDevice.open

在设备上的 shell 中启动进程并返回套接字以与启动的进程进行通信。

¥Launches a process in the shell on the device and returns a socket to communicate with the launched process.

用法

¥Usage

await androidDevice.open(command);

参数

¥Arguments

返回

¥Returns


pinchClose

Added in: v1.9androidDevice.pinchClose

在关闭方向上捏住 selector 定义的小部件。

¥Pinches the widget defined by selector in the closing direction.

用法

¥Usage

await androidDevice.pinchClose(selector, percent);
await androidDevice.pinchClose(selector, percent, options);

参数

¥Arguments

  • selector [AndroidSelector]#

选择器捏合。

¥Selector to pinch close.

捏合大小占小部件大小的百分比。

¥The size of the pinch as a percentage of the widget's size.

可选的捏合速度(以每秒像素为单位)。

¥Optional speed of the pinch in pixels per second.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


pinchOpen

Added in: v1.9androidDevice.pinchOpen

在打开方向上捏住 selector 定义的小部件。

¥Pinches the widget defined by selector in the open direction.

用法

¥Usage

await androidDevice.pinchOpen(selector, percent);
await androidDevice.pinchOpen(selector, percent, options);

参数

¥Arguments

  • selector [AndroidSelector]#

选择器捏开。

¥Selector to pinch open.

捏合大小占小部件大小的百分比。

¥The size of the pinch as a percentage of the widget's size.

可选的捏合速度(以每秒像素为单位)。

¥Optional speed of the pinch in pixels per second.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


press

Added in: v1.9androidDevice.press

按下 selector 定义的小部件中的特定 key

¥Presses the specific key in the widget defined by selector.

用法

¥Usage

await androidDevice.press(selector, key);
await androidDevice.press(selector, key, options);

参数

¥Arguments

  • selector [AndroidSelector]#

选择器按下按键。

¥Selector to press the key in.

  • key [AndroidKey]#

要按的键。

¥The key to press.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


push

Added in: v1.9androidDevice.push

将文件复制到设备。

¥Copies a file to the device.

用法

¥Usage

await androidDevice.push(file, path);
await androidDevice.push(file, path, options);

参数

¥Arguments

文件的路径或文件内容。

¥Either a path to the file, or file content.

设备上文件的路径。

¥Path to the file on the device.

可选文件模式,默认为 644rw-r--r--)。

¥Optional file mode, defaults to 644 (rw-r--r--).

返回

¥Returns


screenshot

Added in: v1.9androidDevice.screenshot

返回带有捕获的设备屏幕截图的缓冲区。

¥Returns the buffer with the captured screenshot of the device.

用法

¥Usage

await androidDevice.screenshot();
await androidDevice.screenshot(options);

参数

¥Arguments

保存图片的文件路径。如果 path 是相对路径,则相对于当前工作目录进行解析。如果未提供路径,图片将不会保存到磁盘。

¥The file path to save the image to. If path is a relative path, then it is resolved relative to the current working directory. If no path is provided, the image won't be saved to the disk.

返回

¥Returns


scroll

Added in: v1.9androidDevice.scroll

在指定的 direction 中滚动 selector 定义的小部件。

¥Scrolls the widget defined by selector in the specified direction.

用法

¥Usage

await androidDevice.scroll(selector, direction, percent);
await androidDevice.scroll(selector, direction, percent, options);

参数

¥Arguments

  • selector [AndroidSelector]#

用于滚动的选择器。

¥Selector to scroll.

  • direction "down"|"up"|"left"|"right"#

    滚动方向。

    ¥Scroll direction.

滚动距离占小部件大小的百分比。

¥Distance to scroll as a percentage of the widget's size.

可选的滚动速度(以每秒像素为单位)。

¥Optional speed of the scroll in pixels per second.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


serial

Added in: v1.9androidDevice.serial

设备序列号。

¥Device serial number.

用法

¥Usage

androidDevice.serial();

返回

¥Returns


setDefaultTimeout

Added in: v1.9androidDevice.setDefaultTimeout

此设置将更改所有接受 timeout 选项的方法的默认最大时间。

¥This setting will change the default maximum time for all the methods accepting timeout option.

用法

¥Usage

androidDevice.setDefaultTimeout(timeout);

参数

¥Arguments

最长时间(以毫秒为单位)

¥Maximum time in milliseconds


shell

Added in: v1.9androidDevice.shell

在设备上执行 shell 命令并返回其输出。

¥Executes a shell command on the device and returns its output.

用法

¥Usage

await androidDevice.shell(command);

参数

¥Arguments

要执行的 shell 命令。

¥Shell command to execute.

返回

¥Returns


swipe

Added in: v1.9androidDevice.swipe

在指定的 direction 中滑动 selector 定义的小部件。

¥Swipes the widget defined by selector in the specified direction.

用法

¥Usage

await androidDevice.swipe(selector, direction, percent);
await androidDevice.swipe(selector, direction, percent, options);

参数

¥Arguments

  • selector [AndroidSelector]#

滑动选择器。

¥Selector to swipe.

  • direction "down"|"up"|"left"|"right"#

    滑动方向。

    ¥Swipe direction.

滑动距离占小部件大小的百分比。

¥Distance to swipe as a percentage of the widget's size.

可选的滑动速度(以每秒像素为单位)。

¥Optional speed of the swipe in pixels per second.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


tap

Added in: v1.9androidDevice.tap

点击 selector 定义的小部件。

¥Taps on the widget defined by selector.

用法

¥Usage

await androidDevice.tap(selector);
await androidDevice.tap(selector, options);

参数

¥Arguments

  • selector [AndroidSelector]#

要点击的选择器。

¥Selector to tap on.

点击的可选持续时间(以毫秒为单位)。

¥Optional duration of the tap in milliseconds.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


wait

Added in: v1.9androidDevice.wait

等待特定的 selector 出现或消失,具体取决于 state

¥Waits for the specific selector to either appear or disappear, depending on the state.

用法

¥Usage

await androidDevice.wait(selector);
await androidDevice.wait(selector, options);

参数

¥Arguments

  • selector [AndroidSelector]#

选择器等待。

¥Selector to wait for.

  • state "gone"(可选)#

    ¥state "gone" (optional)#

    可选状态。可以是:

    ¥Optional state. Can be either:

    • default - 等待元素出现。

      ¥default - wait for element to be present.

    • 'gone' - 等待元素不存在。

      ¥'gone' - wait for element to not be present.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


waitForEvent

Added in: v1.9androidDevice.waitForEvent

等待事件触发并将其值传递给谓词函数。当谓词返回真值时返回。

¥Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy value.

用法

¥Usage

await androidDevice.waitForEvent(event);
await androidDevice.waitForEvent(event, optionsOrPredicate);

参数

¥Arguments

事件名称,通常传递到 *.on(event) 中。

¥Event name, same one typically passed into *.on(event).

接收事件数据并在等待应该解决时解析为真值。

¥receives the event data and resolves to truthy value when the waiting should resolve.

等待的最长时间(以毫秒为单位)。默认为 30000(30 秒)。通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 更改默认值。

¥maximum time to wait for in milliseconds. Defaults to 30000 (30 seconds). Pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout().

接收事件的谓词或选项对象。可选的。

¥Either a predicate that receives an event or an options object. Optional.

返回

¥Returns


webView

Added in: v1.9androidDevice.webView

该方法等待直到与 selector 匹配的 AndroidWebView 打开并返回。如果已经有一个打开的 AndroidWebViewselector 匹配,则立即返回。

¥This method waits until AndroidWebView matching the selector is opened and returns it. If there is already an open AndroidWebView matching the selector, returns immediately.

用法

¥Usage

await androidDevice.webView(selector);
await androidDevice.webView(selector, options);

参数

¥Arguments

可选的包标识符。

¥Optional Package identifier.

  • socketName string (optional)

可选的 webview 套接字名称。

¥Optional webview socket name.

最长时间以毫秒为单位,默认为 30 秒,通过 0 禁用超时。可以使用 androidDevice.setDefaultTimeout() 方法更改默认值。

¥Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using the androidDevice.setDefaultTimeout() method.

返回

¥Returns


webViews

Added in: v1.9androidDevice.webViews

当前打开的 WebView。

¥Currently open WebViews.

用法

¥Usage

androidDevice.webViews();

返回

¥Returns


属性

¥Properties

input

Added in: v1.9androidDevice.input

用法

¥Usage

androidDevice.input

类型

¥Type


事件

¥Events

on('close')

Added in: v1.28androidDevice.on('close')

当设备连接关闭时触发。

¥Emitted when the device connection gets closed.

用法

¥Usage

androidDevice.on('close', data => {});

事件数据

¥Event data


on('webview')

Added in: v1.9androidDevice.on('webview')

当检测到新的 WebView 实例时触发。

¥Emitted when a new WebView instance is detected.

用法

¥Usage

androidDevice.on('webview', data => {});

事件数据

¥Event data