2016-06-19 25 views
1

嘗試寫對谷歌一個簡單的例子水豚,但得到未定義的方法`參觀」的硒:: webdriver的::驅動

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,因爲我的硒火狐設置不同步(隨着時間的推移常見問題 - 它無法在60秒內啓動firefox)。但鉻作品和瀏覽器出現。

這個簡單的紅寶石唯一的例子確實然而工作,所以看起來像某種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

你不應該直接使用的驅動程序 - 你應該呼籲會話訪問(如果您如果你讓Capybara管理你應該在頁面上調用它的會話,那麼你可以管理你自己的會話,你可以在你使用的任何變量上調用它。

Firefox無法爲你工作的原因是因爲Firefox 47與硒打破了一些問題 - https://github.com/SeleniumHQ/selenium/issues/2110 - 很快它會在47.0.1發佈中修復,或者你可以恢復到46。如果你想堅持使用chrome你應該在你的投機/ 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 
相關問題