未定义的方法`访问' for Selenium :: WebDriver :: Driver

时间:2016-06-19 14:46:33

标签: selenium selenium-webdriver rspec webdriver capybara

尝试针对谷歌编写一个简单的水豚示例,但要获取

undefined method `visit' for #<Selenium::WebDriver::Driver:0x000000055f8cc8>

我可以用

driver.get("http://www.google.com/")

但我无法使用

driver.visit("http://www.google.com/")

我有:

require "rspec"
require 'selenium-webdriver'
require "capybara"
require "capybara/rspec"
require "capybara/dsl"

RSpec.configure do |config|
  config.include Capybara::DSL
end

Capybara.configure do |config|
  config.run_server = false
  config.default_driver = :selenium
  config.app_host = 'https://www.google.com'
end

describe "Google Search", type: :feature do

  it "Tests Google" do
    driver = Selenium::WebDriver.for :chrome
    driver.visit "http://www.google.com/"   <-- Error
    fill_in('input', with: '123')
    find_element('input', "Google Search").click
    driver.quit
  end

end

请注意,我必须使用chrome,因为我的selenium firefox设置不同步(随着时间的推移常见问题 - 它无法在60秒内启动firefox)。但Chrome工作正常,浏览器出现了。

这个简单的ruby示例确实工作但是看起来像某种rspec设置问题

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
driver.get "http://google.com"

element = driver.find_element :name => "q"
element.send_keys "Cheese!"
element.submit

puts "Page title is #{driver.title}"

wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { driver.title.downcase.start_with? "cheese!" }

puts "Page title is #{driver.title}"
driver.quit

1 个答案:

答案 0 :(得分:1)

您不应该直接使用该驱动程序 - 您应该在会话中呼叫访问(如果您正在管理自己的会话,您可以在您使用的任何变量上调用它,如果您让Capybara管理你应该在页面上调用的会话。)

Firefox不适合你的原因是因为Firefox 47打破了硒的问题 - https://github.com/SeleniumHQ/selenium/issues/2110 - 很快就会在47.0.1版本中修复,或者你可以恢复到46。如果你想要坚持使用chrome你应该在spec / rails_helper中使用chrome注册一个驱动程序版本并指定

Capybara.register_driver :selenium_chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end

Capybara.default_driver = :selenium_chrome  # for most people this would normally be assigned to javascript_driver, but since you're using selenium for all tests we can just assign to default_driver

然后你会做

describe "Google Search", type: :feature do

  it "Tests Google" do
    page.visit "http://www.google.com/"  #technically the page may not be required here but it can prevent method name collisions with other libraries
    page.fill_in('input', with: '123')  
    find_element('input', "Google Search").click  # I'm guessing this is your own defined method since Capybara doesn't have a find_element method? 
  end    
end