28
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Capybara+headless-chrome でフルサイズのスクリーンショットを撮る

Posted at

#Capybaraでスクリーンショットを撮る

page.save_screenshot "ss.png"

ただし、上の書き方だと画面の一部分しか撮ってくれません...

Capybaraでフルサイズのスクリーンショットを撮る

フルサイズのスクリーンショットの撮り方を調べるとfull: trueオプションを発見したので付けてスクリーンショットを撮ってみます。

page.save_screenshot "ss.png", full: true

しかし、headless-chromeだとフルサイズに撮れませんでした...

他の方法がないか調べると以下のサイトを発見。
https://testingrepository.com/full-page-screenshot-with-selenium/

take_full_page_screenshotメソッドを適当な場所に定義して、page.save_screenshottake_full_page_screenshotに置き換えてみます。

spec_helper.rb
def take_full_page_screenshot(path)
  width  = Capybara.page.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);")
  height = Capybara.page.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);")

  window = Capybara.current_session.driver.browser.manage.window
  window.resize_to(width+100, height+100)

  page.save_screenshot path
end

今度はフルサイズで撮ることができました!

28
20
0

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
28
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?