12
10

More than 5 years have passed since last update.

Capybara(selenium)でブラウザのサイズを変更する

Last updated at Posted at 2013-12-02

Seleniumのテストでランダムに時々失敗するテストがあった

失敗する対象は jquery timepicker で良く起こった
(他にもドラッグアンドドロップ、ポップオーバー、折りたたみメニューなどもありそう)

環境によって失敗する要因としては次のようなものが考えられる

  • マシンによってブラウザのサイズが変わる(MacBook Airだと失敗するとか)
  • 平行しているプロセスの影響でマシンのパワーが圧迫を受けていてjsの動きが変わる

そこで、ブラウザのサイズを大きくすることでかなり改善した

  def ensure_browser_size(width = 1280, height = 720)
    Capybara.current_session.driver.browser.manage.window.resize_to(width, height)
  end

seleniumのときだけ有効にする
注意点は一度 visit を実行して、その後にサイズ変更する必要がある

  visit '/hoge'
  ...
  ensure_browser_size if Capybara.current_driver == :selenium

Capybara.current_driver == :selenium の条件をメソッドの中に入れても良いかも

参考:How do I make sure the window size is always reset ...

Capybarawebkitの場合の参考
Responsive layout tests with Capybara and RSpec

when :webkit
  handle = Capybara.current_session.driver.current_window_handle
  Capybara.current_session.driver.resize_window_to(handle, width, height)

Poltergeistの場合の参考
Poltergeist resize

12
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10