尝试使用chromedriver运行测试的问题

时间:2018-08-07 10:55:27

标签: ruby selenium-webdriver cucumber selenium-chromedriver watir

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

我正在使用selenium-webdriver 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

对此可以提供的任何帮助将不胜感激

编辑:更新为完全错误stacktrace:

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:我现在使用的是Watir,默认情况下使用的是Chrome。我正在通过命令提示符中的irb使用Interactive Ruby Console。

我运行的唯一命令是:

 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就像魅力一样运行

4 个答案:

答案 0 :(得分:0)

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

请更新或安装最新的gem。

答案 1 :(得分:0)

尝试使用chromedriver-helper宝石。

答案 2 :(得分:0)

使用webdrivers.gem自动使驱动程序保持最新。 PATH可能变得很复杂,因此webdrivers gem会忽略它,并指定Selenium应独立查找驱动程序的位置。它也不会像chromedriver-helper那样使用垫片,因此不会对任何系统设置造成影响。只需将gem添加到您的gemfile / gemspec中,然后将require 'webdrivers'添加到您的代码中即可。

答案 3 :(得分:0)

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

tasklist /v | find "chromedriver"

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

require 'watir'            
chromedriver_path = 'c:\tmp\asd\chromedriver.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:\tmp\asd>chromedriver.exe
Starting ChromeDriver 2.41.578737 on port 9515

然后运行测试。

我发现在每次测试之前设置chromedriver的路径非常有用,这样,您可以在文件夹中放置数十个chromedriver,然后将参数传递给要使用的chromedriver测试。

希望它能正常工作,或者至少会给您带来新的错误。

编辑:

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

chromedriver.exe --version

gem list watir

gem list selenium-webdriver

卸载旧版本的gem(如果有的话),然后转到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。