emulate
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 | 字串 |
colorScheme | 'light' | 'dark' |
onLine | 布林值 |
clock | FakeTimerInstallOpts |
用法
browser.emulate(scope, options)
參數
名稱 | 類型 | 詳細資訊 |
---|---|---|
scope | 字串 | 您想要模擬的瀏覽器功能,可以是 clock 、geolocation 、userAgent 、colorScheme 或 onLine |
options | EmulationOptions | 特定範圍的模擬選項 |
範例
example.js
loading...
example.js
loading...