mattheworiordan / capybara-screenshot

Automatically save screen shots when a Capybara scenario fails
MIT License
1.02k stars 168 forks source link

headless_chrome issue #211

Open mattheworiordan opened 7 years ago

mattheworiordan commented 7 years ago
capybara-screenshot could not detect a screenshot driver for 'headless_chrome'. Saving with default with unknown results.

An error occurred in an after hook
  NoMethodError: undefined method `render' for #<Capybara::Selenium::Driver:0x007ffbb58e8498>
  occurred at /app/vendor/bundle/ruby/2.2.0/gems/capybara-screenshot-1.0.5/lib/capybara-screenshot.rb:101:in `block (2 levels) in <top (required)>'
sfcgeorge commented 7 years ago

The issue is that capybara-screnshot decides what driver you're using based on what you call it, not what it actually is. Headless Chrome is just Selenium, so if you call your driver :selenium it works just fine. If you call it :headless_chrome then it doesn't recognise it and reverts to :default driver.

So, to run Chrome headless and have it work with screenshots you'll need something like this in your capybara config, Solution:

# NOTE: must be called `:selenium` not `:chrome_headless`
Capybara.register_driver :selenium do |app|
  options = Selenium::WebDriver::Chrome::Options.new(
    # It's the `headless` arg that make Chrome headless
    # + you also need the `disable-gpu` arg due to a bug
    args: %w[headless disable-gpu window-size=1366,768],
  )

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

Alternative solution (this is unnecessary if you do the above) you can register a new screenshot driver for whatever you called your driver. Could be :headless_chrome, could be :headless_mc_headface.

Capybara::Screenshot.register_driver(:headless_chrome) do |driver, path|
  driver.browser.save_screenshot(path)
end

I think this can be closed as not a bug as registering drivers is documented. But it's useful to have for people like me Googling who also thought it was a bug at first, not realising that what you call your driver matters for some reason.

It might be worth opening a new issue for drivers being determined from their name, rather than inferrer as what they actually are. I don't know the reasoning behind the way it is.

damontgomery commented 7 years ago

Since Capybara has added support for Headless Chrome and calls it :selenium_chrome_headless, here is what you can use in a /support/*.rb file,

# Add support for Headless Chrome screenshots.
Capybara::Screenshot.register_driver(:selenium_chrome_headless) do |driver, path|
  driver.browser.save_screenshot(path)
end
romenigld commented 6 years ago

I have this error:

An error occurred while loading ./spec/features/deleting_tags_spec.rb.
Failure/Error:
  Capybara::Screenshot.register_driver(:selenium_chrome_headless) do |driver, path|
    driver.browser.save_screenshot(path)
  end

NameError:
  uninitialized constant Capybara::Screenshot
mattheworiordan commented 6 years ago

@romenigld that's very odd, it looks like the gem capybara-screenshot is not being loaded correctly / installed correctly. have you followed the steps at https://github.com/mattheworiordan/capybara-screenshot#installation?

Vilariano commented 6 years ago

In env.rb =>

Metodo que chama o chrome em modulo headless

Capybara.register_driver :selenium do |app| Capybara::Selenium::Driver.new( app, browser: :chrome, desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome( 'chromeOptions' => { 'args' => ['--incognito', '--headless', 'disable-gpu', '--disable-cache', '--disable-infobars'] } ) ) end

in helper.rb =>

Metodo para tirar screenshot e embeda no relatorio html

module Helper def take_screenshot(file_name, result) date_path = Time.now.strftime('%F').to_s hora_path = Time.now.strftime('%H%M%S%p').to_s filepath = "results/screenshots/test#{result}/run_#{date_path}" screenshot = "#{file_path}/#{hora_path}.png" page.save_screenshot(screenshot) page.save_screenshot(full: true) embed(screenshot, 'image/png', 'Ver_Evidencia') end end

in hooks.rb =>

Depois de cada scenario ele tira uma screnshot e remove todos os espaços e virgulas

After do |scenario| scenarioname = scenario.name.gsub(/\s+/, '').tr('/', '_')

Se meu senario falhar tira um print e salva no caminho que defino em helper.rb

if scenario.failed? take_screenshot(scenario_name.downcase!, 'failed') else

Se meu senario passar tira um print e salva no caminho que defino em helper.rb

take_screenshot(scenario_name.downcase!, 'passed') end end

nakilon-idwell commented 6 years ago

What?

Vilariano commented 5 years ago

Link: https://github.com/Vilariano/TesteGoogle.git

If you already have cucumber installed and configured, just download the file to run on your machine. Note: I recommend installing Gems from this project before (bundler install).

Any questions send me an email! E-mail: aejvilarinao128@gmail.com

abs,