Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
17
Help us understand the problem. What is going on with this article?
@g-fujioka

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

More than 3 years have passed since last update.

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

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

17
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
g-fujioka
猫好きRailsエンジニア

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
17
Help us understand the problem. What is going on with this article?