Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headless_chrome issue #211

Open
mattheworiordan opened this issue Jul 26, 2017 · 7 comments
Open

headless_chrome issue #211

mattheworiordan opened this issue Jul 26, 2017 · 7 comments
Labels

Comments

@mattheworiordan
Copy link
Owner

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
Copy link

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
Copy link

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
Copy link

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
Copy link
Owner Author

@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
Copy link

Vilariano commented Nov 19, 2018

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
file_path = "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|
scenario_name = 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
Copy link

What?

@Vilariano
Copy link

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: [email protected]

abs,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants