2011-05-25 49 views
5

我一直在使用黃瓜+ webrat,但需要額外的Rails 3兼容性和JavaScript測試,所以改爲水豚+硒。目前我在Rails 3.0.7,Cucumber 0.10.2,caybara 1.0.0.beta1。黃瓜+水豚+硒firefox打開example.com不是我的應用程序

問題:當我運行javascript場景時,firefox會打開「http://www.iana.org/domains/example/」或「www.example.com」而不是我的rails應用程序。

我確定這只是我錯過的一個小設置,但我無法找到它,這讓我難倒了幾天。

無論如何,我的Gemfile中寫道:

group :development, :test do 
    gem 'cucumber' 
    gem 'cucumber-rails', ">= 0.3.2" 
    gem 'pickle' 
    gem "launchy" 
    gem "rspec-rails" 
    gem "Selenium" 
    gem "selenium-client" 
    gem "selenium-webdriver" 
    gem "database_cleaner" 
    gem "factory_girl_rails" 
    gem "capybara" 
    gem "escape_utils" 
end 

的特點是:

@wip 
    @javascript 
    Scenario: Calculate 1 recipe and be deducted a credit 
    Given the following page records 
     | name | permalink | 
     | home | home  | 
    And the following role records 
     | name  | 
     | SuperAdmin | 
     | Admin  | 
     | Customer | 
    When I register "adam" with password "password" 
    And I go to the index 
    Then I should see "5 credits" 

打開Firefox窗口步驟:

When /^I am logged in as "([^"]*)" with password "([^"]*)"$/ do |username, password| 
    unless username.blank? 
    visit(root_path) 
    fill_in "user_session_username", :with => username 
    fill_in "user_session_password", :with => password 
    click_button "login" 
    page.should have_content("Successfully logged in!") 
    end 
end 

我的功能/支持/ env的。 rb:

require 'cucumber/rails' 
    require 'factory_girl' 
    require 'ruby-debug' 
    require 'selenium/client' 
    # require 'capybara/cucumber' commented out because it doesn't want to work with it in 
    Capybara.default_driver = :selenium 

    Capybara.server_boot_timeout = 50 
    Capybara.default_selector = :css 

    begin 
     DatabaseCleaner.strategy = :truncation 
    rescue NameError 
     raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it." 
    end 

命令提示符說:

When I register "adam" with password "password"  # features/step_definiti 
ons/web_steps.rb:229 
     no link with title, id or text 'register' found (Capybara::ElementNotFound 
) 
     (eval):2:in `click_link' 
     ./features/step_definitions/web_steps.rb:232:in `/^I register "([^"]*)" wi 
th password "([^"]*)"$/' 
     features\credit_behaviour.feature:30:in `When I register "adam" with passw 
ord "password"' 

我是相當新的Rails和紅寶石所以容易對我的任何建議,請,他說,任何幫助將不勝感激。

回答

14

好的,讓它工作。這就是我所做的。

env.rb補充說:

require "selenium-webdriver" 

,並改變了我的步驟與來訪root_url'到:

visit root_path 

我想這就是我的一切變化。

我真的以爲我試過了(以及其他每個問題/修復的其他所有變化)。不能相信我已經浪費了3天,然後在公開發布問題幾個小時後找到答案。

+0

這是否意味着您在每次測試中都使用「路徑」而不是「url」?或者你找到了更好的方法(也許調整主機或什麼?) – 2011-09-09 12:58:26

+0

是的我正在使用_path而不是_url。我不確定這是否是「正確」的方式,但現在就行得通。 – RickyD 2011-09-10 03:55:37

+0

根據jnicklas,你想確保你正在使用_path而不是_url,因爲它可能有意想不到的後果。 – Galaxy 2012-08-29 23:57:44