模拟
WebdriverIO 允许您使用 emulate
命令模拟 Web API。这些 Web API 可以完全按照您指定的方式运行。支持以下作用域:
geolocation
:模拟地理位置 APIuserAgent
:模拟用户代理colorScheme
:模拟颜色方案onLine
:模拟在线状态device
:模拟特定的移动或桌面设备clock
:模拟系统时钟
emulate
命令返回一个可以调用以重置模拟的函数。当您想在测试或一系列测试后重置模拟时,这非常有用。
在模拟指南中阅读更多相关信息。
信息
除了 clock
作用域外,无法在不重新加载页面的情况下更改模拟值。
信息
此功能需要浏览器支持 WebDriver Bidi。虽然最新版本的 Chrome、Edge 和 Firefox 都支持此功能,但 Safari 不支持。有关更新,请关注 wpt.fyi。 此外,如果您使用云供应商来启动浏览器,请确保您的供应商也支持 WebDriver Bidi。
EmulationOptions
对象可以根据作用域具有以下属性:
作用域 | 选项 |
---|---|
geolocation | { latitude: number, longitude: number } |
userAgent | string |
colorScheme | 'light' | 'dark' |
onLine | boolean |
clock | FakeTimerInstallOpts |
用法
browser.emulate(scope, options)
参数
名称 | 类型 | 详情 |
---|---|---|
scope | string | 您想要模 拟的浏览器功能,可以是 clock 、geolocation 、userAgent 、colorScheme 或 onLine |
options | EmulationOptions | 特定作用域的模拟选项 |
示例
example.js
loading...
example.js
loading...
返回值
- <Function>
returns
: 一个重置模拟的函数