使用Ruby selenium-webdriver 3.142.6
我的测试有效,但在运行开始时会显示警告
from unittest import TestCase
from unittest.mock import patch
from shutil import copy
def do_some_things(src, dst):
copy(src, dst)
class TestDoSomething(TestCase):
def test_do_somethings(self):
with patch('test_do_some_things.copy') as mm:
do_some_things('~/test.txt', '~/test/test.txt')
mm.called_once()
print(mm.call_count)
根据Ruby文档Selenium :: WebDriver :: Service是私有API。
在网络上搜索与警告有关的文章时,会引用Capybara,RoR和chromedriver-help,但我都没有使用。我已经尝试过'gem install webdrivers',但是失败了。
关于采取的任何建议:忽略弃用消息,或使用私有Service类(如果可以的话,如何使用它),或尝试用webdrivers或其他替换selenium-webdriver?
以下是成功创建带有警告的Selenium驱动程序的代码
WARN Selenium [DEPRECATION] :driver_path is deprecated. Use :service with an instance of Selenium::WebDriver::Service instead.
宝石文件
require "selenium-webdriver"
require "test/unit"
require "byebug"
...
class GoogleTest < Test::Unit::TestCase
def setup
case $browser_arg
when 'chrome'
browser = :chrome
driver_path = '/selenium_browser_drivers/chromedriver'
when 'firefox'
browser = :firefox
driver_path = '/selenium_browser_drivers/geckodriver'
else
raise ArgumentError.new("Unexpected browser argument '#{$browser_arg}'")
exit
end
@se = Selenium::WebDriver.for browser, :driver_path => driver_path
end
...
Gemfile.lock
source 'https://rubygems.org'
gem 'selenium-webdriver', '~> 3.142.0'
gem 'test-unit', '~> 3.3.0'
gem 'byebug', '~> 9.1.0'
命令gem列表中的输出
GEM
remote: https://rubygems.org/
specs:
byebug (9.1.0)
childprocess (3.0.0)
power_assert (1.1.5)
rubyzip (2.0.0)
selenium-webdriver (3.142.6)
childprocess (>= 0.5, < 4.0)
rubyzip (>= 1.2.2)
test-unit (3.3.4)
power_assert
PLATFORMS
ruby
DEPENDENCIES
byebug (~> 9.1.0)
selenium-webdriver (~> 3.142.0)
test-unit (~> 3.3.0)
BUNDLED WITH
2.0.2
答案 0 :(得分:0)
即使在实例化驱动程序变量之前,也可以通过显式分配driver_path变量来避免此错误。看下面的代码
require 'selenium-webdriver'
Selenium::WebDriver::Chrome::Service.driver_path = "C:/Ruby26/chromedriver.exe"
driver = Selenium::WebDriver.for :chrome
driver.manage.timeouts.implicit_wait = 10
driver.navigate.to("https://www.google.com/")
driver.find_element(name: 'q').send_keys 'raja'
此代码不发出警告。因为我在创建驱动器之前分配了值Selenium::WebDriver::Chrome::Service.driver_path = "C:/Ruby26/chromedriver.exe"
。