pau*_*aul 11 ruby google-chrome watir
之前我把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
Run Code Online (Sandbox Code Playgroud)
输出是:
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'
Run Code Online (Sandbox Code Playgroud)
我在Windows 7上,使用Ruby版本1.9.3p551,我指的是教程http://watirwebdriver.com/chrome/.
如何告诉Watir(和Selenium-WebDriver)chromedriver.exe的位置?
Jus*_* Ko 12
解决方案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('../..', File.dirname(__FILE__)),"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
Run Code Online (Sandbox Code Playgroud)
解决方案2 - 在浏览器初始化期间指定:driver_path
或者,您也可以在初始化浏览器时指定驱动程序路径.这是一个更好的,你不需要有Selenium代码,但如果你在不同的地方初始化浏览器将是重复的.
# Determine the driver path
chromedriver_path = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"browsers","chromedriver.exe")
# Initialize the browser with the driver path
browser = Watir::Browser.new :chrome, driver_path: chromedriver_path
Run Code Online (Sandbox Code Playgroud)
解决方案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
Run Code Online (Sandbox Code Playgroud)
要指定包含二进制文件的文件夹,只需更改ENV['PATH'](以附加目录):
require 'watir'
# Determine the directory containing chromedriver.exe
chromedriver_directory = File.join(File.absolute_path('../..', File.dirname(__FILE__)),"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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16862 次 |
| 最近记录: |