Open mattheworiordan opened 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.
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
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
@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?
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
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
After do |scenario| scenarioname = scenario.name.gsub(/\s+/, '').tr('/', '_')
if scenario.failed? take_screenshot(scenario_name.downcase!, 'failed') else
take_screenshot(scenario_name.downcase!, 'passed') end end
What?
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,