Cucumber + Capybara + Selenium firefox打开example.com而不是我的应用程序

时间:2011-05-25 13:05:41

标签: ruby-on-rails-3 selenium cucumber capybara

我一直在使用黄瓜+ webrat,但需要额外的Rails 3兼容性和javascript测试,所以改为capybara + selenium。目前我在Rails 3.0.7,Cucumber 0.10.2,caybara 1.0.0.beta1。

问题:当我运行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和Ruby都相当新,所以请提出任何建议,对我很轻松,并说非常感谢任何帮助。

1 个答案:

答案 0 :(得分:14)

好的,搞定了。这就是我所做的。

env.rb补充道:

require "selenium-webdriver"

并将'visit root_url'更改为:

visit root_path

我认为这就是我改变的一切。

老实说,我认为我已经尝试过(以及我见过的所有其他问题/解决方案的所有其他排列)。无法相信我已浪费了3天,然后在公开发布问题几个小时后找到答案。