ruby – 如何指定chromedriver二进制文件的位置

前端之家收集整理的这篇文章主要介绍了ruby – 如何指定chromedriver二进制文件的位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
之前我把Chrome二进制文件chromedriver.exe”放在“C:/ Windows”目录中,Watir从那里选择它.现在我必须将我的项目移动到另一台机器上,因此我无法对可执行路径进行硬编码.我还希望二进制文件与Git上的代码保持一致,而不是让每个测试工程师在发布新版本时手动更新二进制文件.

现在我已经将Chrome二进制文件放在绝对路径上,但是找不到它.这是我尝试过的(hooks.rb):

Before do
    puts "inside hooks in before"
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = File.join(File.absolute_path('../..',File.dirname(__FILE__)),"browsers/chromedriver.exe")
    @browser = Watir::Browser.new :chrome,:profile => profile
  end

输出是:

inside hooks in before

Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver executable. Please download the server from http://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at http://code.google.com/p/selenium/wiki/ChromeDriver.
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:21:in `executable_path'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/service.rb:34:in `default_service'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/chrome/bridge.rb:14:in `initialize'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `new'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver/common/driver.rb:37:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.44.0/lib/selenium/webdriver.rb:67:in `for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.11/lib/watir-webdriver/browser.rb:46:in `initialize'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `new'
C:/Users/Admin/watircukepractice/test_puppies/features/support/hooks.rb:11:in `Before'

我在Windows 7上,使用Ruby版本1.9.3p551,我指的是教程http://watirwebdriver.com/chrome/.

如何告诉Watir(和Selenium-WebDriver)chromedriver.exe的位置?

解决方法

解决方案1 ​​ – Selenium :: WebDriver :: Chrome.driver_path =

有一个Selenium :: WebDriver :: Chrome.driver_path =方法,允许指定chromedriver二进制文件

require 'watir'

# Specify the driver path
chromedriver_path = File.join(File.absolute_path('../..',"browsers","chromedriver.exe")
Selenium::WebDriver::Chrome.driver_path = chromedriver_path

# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close

解决方案2 – 在浏览器初始化期间指定:driver_path

或者,您也可以在初始化浏览器时指定驱动程序路径.这样做更好一点,因为您不需要使用Selenium代码,但如果您在不同的位置初始化浏览器,则会重复这些代码.

# Determine the driver path
chromedriver_path = File.join(File.absolute_path('../..',"chromedriver.exe")

# Initialize the browser with the driver path
browser = Watir::Browser.new :chrome,driver_path: chromedriver_path

解决方案3 – 更新ENV [‘PATH’]

当我最初回答这个问题时,无论出于何种原因,我都无法得到上述解决方案.当Selenium-WebDriver启动驱动程序时,似乎没有使用设置值.虽然第一个解决方案是推荐的方法,但这是另一种选择.

另一种选择是以编程方式将所需目录添加到路径中,该路径存储在ENV [‘PATH’]中.您可以在Selenium :: WebDriver :: Platform中看到二进制文件的位置,方法是检查路径中的任何文件夹中是否存在可执行文件(从版本2.44.0开始):

def find_binary(*binary_names)
  paths = ENV['PATH'].split(File::PATH_SEPARATOR)
  binary_names.map! { |n| "#{n}.exe" } if windows?

  binary_names.each do |binary_name|
    paths.each do |path|
      exe = File.join(path,binary_name)
      return exe if File.executable?(exe)
    end
  end

  nil
end

要指定包含二进制文件文件夹,只需更改ENV [‘PATH’](附加目录):

require 'watir'

# Determine the directory containing chromedriver.exe
chromedriver_directory = File.join(File.absolute_path('../..',"browsers")

# Add that directory to the path
ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{chromedriver_directory}"

# Start the browser as normal
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.close
原文链接:https://www.f2er.com/ruby/274383.html

猜你在找的Ruby相关文章