Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set screen size for chrome headless system test in rails 5.1?

In order to work with and test a new responsive frontend for my site, I'm trying to use Rails' new system tests (specs) with javascript and Chrome headless. I cannot figure out a way to set the screen size of the browser in the spec, though.

Here's my setup in spec/rails_helper.rb

config.before(:each, type: :system, js: true) do
  driven_by :selenium_chrome_headless, screen_size: [1900, 800]
end

I then create the screenshot with:

page.driver.save_screenshot(the_uri)

But the size of the rendered screenshot still is the default, which is much smaller. Ideally, I'd like to see the entire rendered page, but at this point, I'd be happy with just using the dimensions I've provided.

Ideas on what I'm missing here?

like image 995
croceldon Avatar asked Nov 08 '17 14:11

croceldon


People also ask

What is the maximum window size in chrome headless mode?

The default window size and display size in headless mode is 800x600 on all platforms. So the maximized window size is not applicable for chrome-headless and needs to be explicitly set by users, if required. It worked for me and I've tested this with 6000x6000 window size. below answer helped. link

Why can't I see what size my screen is in headless mode?

The problem is that headless mode is meant to be used on computers without screens, so there's no way for it to figure out what size your screen is even if you have one. The only way is for you to pass that information to the browser with --window-size. The default window size and display size in headless mode is 800x600 on all platforms.

What is system testing in rails?

In Rails jargon, system testing refers to "testing an application as a whole system". That is done by using a browser in the tests. Instead of testing separate parts, with system tests, we can test a whole 'workflow', just like what a user goes through while interacting with our app, including the JavaScript parts.

Does Minitest work with rails?

These Minitest features are in the test runner introduced in Rails 5.0. There's no need for the database_cleaner gem anymore; Rails does the cleanup for us ('transactional tests'). Since Rails uses Selenium instead of the Capybara default, we don't need capybara-webkit and we don't need to do any extras to include testing of Javascript elements.


2 Answers

Had same issue.

Changed this line

driven_by :selenium_chrome_headless, screen_size: [1400, 1400]

to this

driven_by :selenium, using: :headless_chrome, screen_size: [1400, 1400]

to fix the issue.

like image 98
Andrey Avatar answered Nov 02 '22 00:11

Andrey


You simply need to redefine the driver which passes the headless and screen size arguments.

Capybara.register_driver :selenium_chrome_headless do |app|
  options = Selenium::WebDriver::Chrome::Options.new

  [
    "headless",
    "window-size=1280x1280",
    "disable-gpu" # https://developers.google.com/web/updates/2017/04/headless-chrome
  ].each { |arg| options.add_argument(arg) }

  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

RSpec.configure do |config|
  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome_headless
  end
end
like image 36
Daniel Westendorf Avatar answered Nov 02 '22 01:11

Daniel Westendorf