SeleniumでChromeのデバイス指定を使ってスマホ表示させる方法

SeleniumChromeのデバイス指定を使ってスマホ表示させる

SeleniumChromeを動かす場合、ChromeOptionsを使ってデバイス指定をすることができます。デバイスにスマホ端末を指定することで、スマホ表示でのSelenium操作が出来るようになります。

リファレンス

Seleniumデザインパターン & ベストプラクティス

Seleniumデザインパターン & ベストプラクティス

やり方

# mobile_emulation = { "deviceName" => "Google Nexus 5" }
mobile_emulation = { "deviceName" => "Apple iPhone 6" }
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => { "mobileEmulation" => mobile_emulation })
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps

内容

Selenium::WebDriver::Remote::Capabilitiesクラスのchromeメソッドを使うことでChrome用のdesired_capabilitiesを作ることができます。このとき、chromeOptionsmobileEmulationを指定すれば、デバイスを指定することができます。

指定できるデバイスの一覧のようなものは特に見つけられなかったのですが、Chromeで使っているものはおそらく使えるのではないかと思います。ただし、名称一致は必要なようで、"Apple iPhone 6"は、"iPhone 6"ではエラーになりました。

システムテスト自動化 標準ガイド CodeZine BOOKS

システムテスト自動化 標準ガイド CodeZine BOOKS

関連記事

genius.hateblo.jp

genius.hateblo.jp

genius.hateblo.jp