我一直在使用cucumber + 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 还很陌生,所以请对我提出任何建议,请不要吝啬,说任何帮助都将不胜感激。
最佳答案
好的,开始工作了。这就是我所做的。
env.rb 补充说:
require "selenium-webdriver"
并将“访问 root_url”的步骤更改为:
visit root_path
我想这就是我改变的一切。
老实说,我认为我已经尝试过(以及我见过的所有其他问题/修复的所有其他排列)。不敢相信我已经浪费了 3 天,然后在公开发布问题几个小时后找到了答案。
关于ruby-on-rails-3 - Cucumber + Capybara + Selenium firefox 打开 example.com 而不是我的应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6124921/