尝试使用chromedriver运行测试时出现问题



我目前在尝试使用chromedriver运行Ruby测试时遇到问题。

我使用的是硒网络驱动程序3.14.0,黄瓜3.1.2,Windows 10

每当我使用firefox运行测试时,它们的加载和运行都很好,没有任何问题。然而,一旦我将其中一个更改为使用chromedriver并尝试运行它,我就会收到以下错误:

unable to connect to chromedriver 127.0.0.1:9515

我的PATH上有chromedriver,我正在使用以下代码尝试初始化ruby代码中的驱动程序:

require 'selenium-webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-popup-blocking')
options.add_argument('--disable-translate')
driver1 = Selenium::WebDriver.for :chrome, options: options
Given (/^we navigate to the Timesheets area$/) do
driver1.navigate.to "test"
driver1.find_element(:id, 'EmailAddress').send_keys("test")
driver1.find_element(:id, 'Password').send_keys("test")
driver1.find_element(:css, 'button').click
wait = Selenium::WebDriver::Wait.new(:timeout => 5)
begin
element = wait.until { driver1.find_element(:class => "name") }
end
driver1.find_element(:css, 'i.fa.fa-clock-o').click
begin
element = wait.until { driver1.find_element(:class => "pull-left") }
expect(element.text).to eq('Timesheets')
end
end
en (/^we click the Add New Timesheet Button$/) do
driver1.find_element(:css, 'a.pull-right').click
end
Then (/^we should be taken to the Add New Timesheet Page$/) do
wait = Selenium::WebDriver::Wait.new(:timeout => 5)
begin
element = wait.until { driver1.find_element(:css => "h1.pull-left") }
expect(element.text).to eq('Add Timesheet')
ensure
driver1.quit
end
end

将不胜感激能在这方面提供的任何帮助

编辑:更新为完整错误堆栈:

unable to connect to chromedriver 127.0.0.1:9515 
(Selenium::WebDriver::Error::WebDriverError)
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/common/service.rb:142:in `connect_until_stable'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3 
3.14.0/lib/selenium/webdriver/common/service.rb:72:in `block in start'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/common/socket_lock.rb:39:in `locked'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/common/service.rb:69:in `start'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/chrome/driver.rb:43:in `initialize'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver- 
3.14.0/lib/selenium/webdriver.rb:86:in `for'
C:/Ruby25-x64/features/step_definitions/timesheet_steps.rb:9:in `<top 
(required)>'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/glue/registry_and_more.rb:107:in `load'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/glue/registry_and_more.rb:107:in `load_code_file'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime/support_code.rb:144:in `load_file'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime/support_code.rb:85:in `block in load_files!'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime/support_code.rb:84:in `each'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime/support_code.rb:84:in `load_files!'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime.rb:272:in `load_step_definitions'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/runtime.rb:68:in `run!'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber- 
3.1.2/lib/cucumber/cli/main.rb:34:in `execute!'
C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/cucumber-3.1.2/bin/cucumber:9:in `<top 
(required)>'
C:/Ruby25-x64/bin/cucumber:23:in `load'
C:/Ruby25-x64/bin/cucumber:23:in `<main>'

编辑2:我现在使用的是默认使用Chrome的Watir。我在命令提示符中使用irb来使用交互式Ruby控制台。

我运行的唯一命令是:

require "Watir"
b = Watir::Browser.new

然后我得到下面的堆栈:

Traceback (most recent call last):
12: from C:/Ruby25-x64/bin/irb.cmd:19:in `<main>'
11: from (irb):3
10: from (irb):3:in `new'
9: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/watir-6.12.0/lib/watir/browser.rb:48:in `initialize'
8: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver.rb:86:in `for'
7: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `for'
6: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/driver.rb:44:in `new'
5: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/chrome/driver.rb:43:in `initialize'
4: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/service.rb:69:in `start'
3: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/socket_lock.rb:39:in `locked'
2: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/service.rb:72:in `block in start'
1: from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.14.0/lib/selenium/webdriver/common/service.rb:142:in `connect_until_stable'
Selenium::WebDriver::Error::WebDriverError (unable to connect to chromedriver 127.0.0.1:9515)

我是不是错过了什么,因为我在Path中设置了ChromeDriver,所以在我的脑海中,它应该可以毫无问题地运行,因为Firefox运行起来就像一个迷人的

使用webdrivers.gem自动更新驱动程序。PATH可能会变得复杂,因此webdrivers gem会忽略它,并指定Selenium应该独立查找驱动程序的位置。它也不像chromedriver助手那样使用垫片,所以它不会干扰任何系统设置。只需将gem添加到您的gemfile/gemspec中,然后在代码中添加require 'webdrivers'

问题是关于旧版本的Chromedriver。一些Selenium版本无法处理旧版本的chromedriver。

请更新或安装最新的gem。

尝试使用chromedriver助手gem。

请检查是否有chromedriver实例已经在运行,然后将其杀死。如果是,您可以尝试在命令提示符下运行此命令,或者在任务管理器中手动检查

tasklist /v | find "chromedriver"

然后在你的测试中设置chromedriver的路径,这在Windows 10 上对我有效

require 'watir'            
chromedriver_path = 'c:tmpasdchromedriver.exe'
#interestingly, this also works
#chromedriver_path = "/tmp/asd/chromedriver.exe"
Selenium::WebDriver::Chrome.driver_path= chromedriver_path  
begin    
puts "Opening browser." 
b = Watir::Browser.new :chrome     
b.goto "www.google.com"  
puts "Navigate to google, title is #{b.title}"
b.text_field(:id => "lst-ib").set "hope this works"
puts "setting search parameters "
rescue => e
puts "error happened: #{e.backtrace.inspect}, #{e.class}, #{e.message}"  
ensure
b.close unless b.nil? 
end

如果这不起作用,再次检查是否有任何chromedriver正在运行,然后从命令行手动运行它,它应该会产生这样的输出

C:tmpasd>chromedriver.exe
Starting ChromeDriver 2.41.578737 on port 9515

然后运行测试。

我发现在每次测试之前设置chromedriver的路径很有用,这样你就可以在一个文件夹中有几十个chromedrivers,然后将参数传递给测试,使用哪个chromedriver。

希望它能起作用,或者至少能给出新的错误。

编辑:

在下载chromedriver.exe的文件夹中,键入以下

chromedriver.exe --version
gem list watir
gem list selenium-webdriver

卸载旧版本的gems(如果有的话(,然后转到irb并逐个键入

require 'watir' 
Selenium::WebDriver::Chrome.driver_path= '/tmp/asd/chromedriver.exe'
Selenium::WebDriver::Chrome.path = '/Program Files (x86)/Google/Chrome/Application/chrome.exe'
b = Watir::Browser.new :chrome

当然,把你的路径粘贴在这里,你会得到什么错误,还有chrome、driver、ruby、watir、selenium和windows的版本。我不确定黄瓜是否与此有关,如果你有问题,即使是irb也不应该。

这只是一种预感,但如果有任何异常,也要检查ENV变量或PATH。

相关内容

  • 没有找到相关文章

最新更新