这是我的Capybara配置:
Version used: Gephi 0.9.2
Java version: 8_151
Operating System: Ubuntu 16.04
orientdb: 2.2.29 community version
这是我的Gemfile:
require 'selenium/webdriver'
# Configure Capypara with javascript client
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end
Capybara.javascript_driver = :headless_chrome
#Capybara.javascript_driver = :chrome
# Take screenshot for every feature test failed
RSpec.configure do |config|
config.after(:each, :type => :feature) do |example|
if example.exception
meta = example.metadata
name = "test-failure-#{File.basename(meta[:file_path])}-#{meta[:line_number]}.png"
# Save screenshots in CI
screenshot_root_path = ENV["CIRCLE_ARTIFACTS"] || Rails.root.join("tmp", "capybara")
screenshot_path = [screenshot_root_path, 'feature_tests', name].join("/")
page.save_screenshot(screenshot_path, full: true)
puts "Screenshot Taken: #{screenshot_path}\n"
end
end
end
但是当测试失败时,我无法创建屏幕截图。请告诉我怎么做。
答案 0 :(得分:2)
我现在可以解决这个问题。我想念这一行:
Capybara.current_driver = :headless_chrome
所以完整的设置应该是:
# Configure Capypara with javascript client
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
Capybara.register_driver :headless_chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: { args: %w(headless disable-gpu) }
)
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end
Capybara.current_driver = :headless_chrome # add more here
Capybara.javascript_driver = :headless_chrome
这是自定义代码处理捕获截图和保存网页:
# Take screenshot for every feature test failed
def file_path(example, filetype)
meta = example.metadata
name = "feature-#{File.basename(meta[:file_path])}-line:#{meta[:line_number]}.#{filetype}"
screenshot_root_path = ENV["CIRCLE_ARTIFACTS"] || Rails.root.join("tmp", "capybara")
screenshot_path = [screenshot_root_path, 'feature_tests', name].join("/")
puts "Screenshot Taken: #{screenshot_path}\n"
screenshot_path
end
RSpec.configure do |config|
config.before(:each, :type => :feature) do
Capybara.page.current_window.resize_to(1024, 768)
end
config.after(:each, :type => :feature) do |example|
if example.exception
page.save_screenshot(file_path(example, 'png'), full: true)
save_page(file_path(example, 'html'))
end
end
end