エミュレーション
WebdriverIOでは、emulate
コマンドを使用してWeb APIをエミュレートし、特定のブラウザの動作をエミュレートするためのカスタム値を返すことができます。これにはアプリケーションが明示的にこれらのAPIを使用する必要があることに注意してください。
情報
この機能はブラウザのWebDriver Bidiサポートが必要です。ChromeやEdge、Firefoxの最新バージョンではサポートされていますが、Safariはサポートしていません。更新情報はwpt.fyiをフォローしてください。さらに、クラウドベンダーを使用してブラウザを起動する場合は、そのベンダーもWebDriver Bidiをサポートしていることを確認してください。
テストでWebDriver Bidiを有効にするには、capabilities内にwebSocketUrl: true
が設定されていることを確認してください。
位置情報
ブラウザの位置情報を特定の地域に変更することができます:
await browser.emulate('geolocation', {
latitude: 52.52,
longitude: 13.39,
accuracy: 100
})
await browser.url('https://www.google.com/maps')
await browser.$('aria/Show Your Location').click()
await browser.pause(5000)
console.log(await browser.getUrl()) // 出力: "https://www.google.com/maps/@52.52,13.39,16z?entry=ttu"
これによりnavigator.geolocation.getCurrentPosition
の動作がモンキーパッチされ、あなたが提供した位置情報が返されます。
カラースキーム
ブラウザのデフォルトカラースキーム設定を以下のように変更できます:
await browser.emulate('colorScheme', 'light')
await browser.url('https://webdriver.io')
const backgroundColor = await browser.$('nav').getCSSProperty('background-color')
console.log(backgroundColor.parsed.hex) // 出力: "#efefef"
await browser.emulate('colorScheme', 'dark')
await browser.url('https://webdriver.io')
const backgroundColor = await browser.$('nav').getCSSProperty('background-color')
console.log(backgroundColor.parsed.hex) // 出力: "#000000"
これにより、(prefers-color-scheme: dark)
でカラースキームを照会するときのwindow.matchMedia
の動作がモンキーパッチされます。
ユーザーエージェント
ブラウザのユーザーエージェントを別の文字列に変更できます:
await browser.emulate('userAgent', 'Chrome/1.2.3.4 Safari/537.36')
これによりnavigator.userAgent
の値が変更されます。ブラウザベンダーは徐々にユーザーエージェントを非推奨にしていることに注意してください。