运行 在 rails 项目上使用水豚和 selenium-webdriver 进行的系统测试失败:由于 bridge.rb:44:in `handshake 中的 ArgumentError
Running system test with capybara & selenium-webdriver on a rails project is failing: due to ArgumentError in bridge.rb:44:in `handshake
我有以下水豚配置:
Capybara.register_driver :chrome_headless do |app|
media_tab_screen_size = '1280,800'
chrome_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
loggingPrefs: {
browser: 'ALL',
client: 'ALL',
driver: 'ALL',
server: 'ALL'
},
chromeOptions: {
args: %W[no-sandbox
disable-dev-shm-usage
no-default-browser-check
start-maximized
headless
disable-gpu
window-size=#{media_tab_screen_size}]
}
)
if ENV['HUB_URL']
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: ENV['HUB_URL'],
desired_capabilities: chrome_capabilities)
else
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: chrome_capabilities)
end
end
RSpec.configure do |config|
driven_by :chrome_headless
# Sets host for tests with selenium
Capybara.app_host = "http://#{IPSocket.getaddress(Socket.gethostname)}:3000"
# Capybara.server = :puma # Until your setup is working
Capybara.server_host = IPSocket.getaddress(Socket.gethostname)
Capybara.server_port = 3000
end
当我使用 docker-compose 并通过 selenium/standalone-chrome:88.0
图像 运行 进行系统测试时,当我 运行 测试像这个:
docker-compose run --rm -e RAILS_ENV=test web bin/rails spec:system
ArgumentError:
wrong number of arguments (given 1, expected 0)
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/bridge.rb:44:in `handshake'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/driver.rb:39:in `initialize'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:58:in `new'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:58:in `for'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver.rb:88:in `for'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:83:in `browser'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:104:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:278:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `call'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `visit'
# ./spec/system/home_page_spec.rb:7:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/webmock-3.11.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <main>'
1.2) Failure/Error:
def self.handshake(**opts)
desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
if desired_capabilities.is_a?(Symbol)
unless Capabilities.respond_to?(desired_capabilities)
raise Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
end
desired_capabilities = Capabilities.__send__(desired_capabilities)
end
ArgumentError:
wrong number of arguments (given 1, expected 0)
真不知道是什么原因造成的,是配置问题还是兼容性问题。我尝试调试它无数次并更改 gems 版本但没有任何反应。如果有任何帮助,我将不胜感激。
查看堆栈跟踪中列出的代码,远程驱动程序正在执行
def initialize(opts = {})
listener = opts.delete(:listener)
@bridge = Bridge.handshake(opts)
...
调用远程桥代码
def self.handshake(**opts)
desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
...
从这里我们可以看出,远程驱动程序正在传递一个哈希,但 handshake
需要关键字参数。这在 Ruby < 3.0 中会起作用,因为最后一个 Hash 参数会被解释为关键字参数(在 Ruby 2.7 中会发出弃用警告),但在 Ruby 3.0+ 中不会,所以我假设您使用的是 Ruby 3.0,它与 Selenium 3.x
不兼容
我有以下水豚配置:
Capybara.register_driver :chrome_headless do |app|
media_tab_screen_size = '1280,800'
chrome_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
loggingPrefs: {
browser: 'ALL',
client: 'ALL',
driver: 'ALL',
server: 'ALL'
},
chromeOptions: {
args: %W[no-sandbox
disable-dev-shm-usage
no-default-browser-check
start-maximized
headless
disable-gpu
window-size=#{media_tab_screen_size}]
}
)
if ENV['HUB_URL']
Capybara::Selenium::Driver.new(app,
browser: :remote,
url: ENV['HUB_URL'],
desired_capabilities: chrome_capabilities)
else
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: chrome_capabilities)
end
end
RSpec.configure do |config|
driven_by :chrome_headless
# Sets host for tests with selenium
Capybara.app_host = "http://#{IPSocket.getaddress(Socket.gethostname)}:3000"
# Capybara.server = :puma # Until your setup is working
Capybara.server_host = IPSocket.getaddress(Socket.gethostname)
Capybara.server_port = 3000
end
当我使用 docker-compose 并通过 selenium/standalone-chrome:88.0
图像 运行 进行系统测试时,当我 运行 测试像这个:
docker-compose run --rm -e RAILS_ENV=test web bin/rails spec:system
ArgumentError:
wrong number of arguments (given 1, expected 0)
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/bridge.rb:44:in `handshake'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/driver.rb:39:in `initialize'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:58:in `new'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:58:in `for'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver.rb:88:in `for'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:83:in `browser'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:104:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:278:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `call'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `visit'
# ./spec/system/home_page_spec.rb:7:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/webmock-3.11.2/lib/webmock/rspec.rb:37:in `block (2 levels) in <main>'
1.2) Failure/Error:
def self.handshake(**opts)
desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
if desired_capabilities.is_a?(Symbol)
unless Capabilities.respond_to?(desired_capabilities)
raise Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"
end
desired_capabilities = Capabilities.__send__(desired_capabilities)
end
ArgumentError:
wrong number of arguments (given 1, expected 0)
真不知道是什么原因造成的,是配置问题还是兼容性问题。我尝试调试它无数次并更改 gems 版本但没有任何反应。如果有任何帮助,我将不胜感激。
查看堆栈跟踪中列出的代码,远程驱动程序正在执行
def initialize(opts = {})
listener = opts.delete(:listener)
@bridge = Bridge.handshake(opts)
...
调用远程桥代码
def self.handshake(**opts)
desired_capabilities = opts.delete(:desired_capabilities) { Capabilities.new }
...
从这里我们可以看出,远程驱动程序正在传递一个哈希,但 handshake
需要关键字参数。这在 Ruby < 3.0 中会起作用,因为最后一个 Hash 参数会被解释为关键字参数(在 Ruby 2.7 中会发出弃用警告),但在 Ruby 3.0+ 中不会,所以我假设您使用的是 Ruby 3.0,它与 Selenium 3.x