无法在Mac上设置ruby-selenium Webdriver

时间:2015-10-11 13:20:37

标签: ruby macos rvm homebrew

我一直在使用selenium IDE。现在我们决定使用Selenium webdriver和Ruby。我对如何设置我的Mac感到困惑, Mac Pro Yosemite 10.10.5

在我的终端中,我运行了这些命令:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ brew doctor
Your system is ready to brew.
$ brew install ruby
  ==> Summary
  /usr/local/Cellar/openssl/1.0.2d_1: 464 files, 18M
==> Installing ruby
==> Downloading https://homebrew.bintray.com/bottles/ruby-2.2.3.yosemite.bottle.100.0%
==> Pouring ruby-2.2.3.yosemite.bottle.tar.gz
    /usr/local/Cellar/ruby/2.2.3: 1080 files, 20M

$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]

$ sudo gem install selenium-webdriver
   Done installing documentation for websocket, ffi, childprocess, rubyzip, multi_json, selenium-webdriver after 25 seconds
6 gems installed

我将从IDE录制的脚本导出到Ruby脚本中,其中:export为ruby/rspec/webdriver。我将示例脚本保存为文档中的Exam.rb

当我运行rspec Exam.rb时,我收到以下错误:

/usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load': cannot load such file -- /Users/xxxx/Documents/Exam.rb (LoadError)     
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'        
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'        
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'     
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'       
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'      
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'      
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'       
    from /usr/local/lib/ruby/gems/2.2.0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'     
    from /usr/local/bin/rspec:23:in `load'      
    from /usr/local/bin/rspec:23:in `<main>'        

当我嘲笑ruby版本时:

ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]

1 个答案:

答案 0 :(得分:0)

你有红宝石后(我建议 R uby V ersion M anager或 rbenv

您需要安装gem selenium-webdriver

之后,您需要driver来控制所需的浏览器,在我的情况下我安装ChromeDriver,它只是一个二进制文件(我将它添加到我的家中并编辑了环境变量$ PATH以及二进制文件的路径)这就是所有的一切都在运行,你可以运行这个litlle脚本来验证它:

require "selenium-webdriver"

driver = Selenium::WebDriver.for :chrome
driver.navigate.to "http://www.google.com"
element = driver.find_element(:name, 'q')
element.send_keys "Hello Selenium WebDriver!"
element.submit
puts driver.title

在Zhimin Zhan的红宝石中查阅本书:Selenium WebDriver Recipes