gpt4 book ai didi

ruby-on-rails - capybara :无法在 Mac 上使用 selenium 驱动程序截取屏幕截图

转载 作者:行者123 更新时间:2023-12-02 15:05:33 25 4
gpt4 key购买 nike

这是我的 capybara 配置:

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

这是我的 Gemfile:

 gem 'capybara'
gem 'capybara-selenium'

但是当测试失败时,我无法创建屏幕截图。请告诉我怎么做。

最佳答案

我现在可以解决这个问题。我想念这一行:

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

关于ruby-on-rails - capybara :无法在 Mac 上使用 selenium 驱动程序截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46988190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com