我有一个在浏览器中工作的简单登录系统。我最近将我的黄瓜测试切换为使用 selenium,因为我需要 ajax 调用,现在登录/身份验证步骤不再通过。该应用程序在 Firefox 和 Chromium 中仍可正常运行。
这是步骤和定义
features/authentication.features
Feature: Require Authentication
In order to restrict access to the app
A User
Must be logged in
Scenario: Accessing Tracks
Given I am not logged in
And I visit the "Tracks" page
Then I should see the "Welcome" page
And I should see "You need to be logged in to access this page"
Scenario: Accessing Tracks when Logged in
Given I am logged in
And I visit the "Tracks" page
Then I should see the "Tracks" page
特征/step_definitions/register_and_login_steps.rb
When(/^I log in as "(.*?)"$/) do |name|
create_and_login(name)
end
When(/^I log in$/) do
create_and_login('tester')
end
When(/^I log out$/) do
visit(logout_path)
end
When(/^I should be logged out$/) do
page.should have_title "Welcome"
page.should have_text "Please log in"
end
When(/^I am( not)? logged in$/) do |negative|
if negative
visit(logout_path)
else
create_and_login('anyone')
page.should have_title 'Welcome'
end
end
特征/step_definitions/should_see_steps.rb
When(/I should( not)? see the "(.*?)" page$/) do |negative, page_title|
if negative
page.should_not have_title page_title
else
page.should have_title page_title
end
end
When(/^I should( not)? see "(.*?)"$/) do |negative, text|
if negative
page.should_not have_text text
else
page.should have_text text
end
end
When(/^I visit the "(.*?)" page$/) do |page|
path = page.downcase + "_path"
visit_path(path)
end
功能/支持/帮助者/user.rb
def password_for(user)
user + '_password_'
end
def create_user(name)
return if User.exists?(name: name)
user = User.create!(name: name,
password: password_for(name),
password_confirmation: password_for(name))
end
def create_and_login(name)
create_user(name)
visit(logout_path)
visit(login_path)
fill_in 'Name', with: name
fill_in 'Password', with: password_for(name)
click_button 'Login'
end
Gemfile(仅限测试组)
group :test do
gem 'guard-spork', '~> 1.5.0'
gem 'rb-inotify', '~> 0.9.0'
gem 'spork', '~> 0.9.2'
gem 'guard-rspec','~> 2.5.0'
gem 'cucumber-rails' , '~> 1.3.0'
gem 'database_cleaner','~> 0.9.1'
gem 'guard-cucumber', '~> 1.3.2'
gem 'capybara', '~> 2.1.0'
gem 'selenium-webdriver', '~>2.31.0'
end
日志/test.log
Connecting to database specified by database.yml
(0.3ms) begin transaction
Started GET "/logout" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#destroy as HTML
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (26.4ms)
Rendered layouts/_player.html.haml (0.9ms)
Completed 200 OK in 64ms (Views: 63.6ms | ActiveRecord: 0.0ms)
Started GET "/assets/application.js" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Served asset /application.js - 200 OK (8ms)
Started GET "/assets/application.css" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Served asset /application.css - 200 OK (3ms)
Started GET "/tracks" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by TracksController#index as HTML
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."session" = '' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 62ms (ActiveRecord: 1.6ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (1.8ms)
Rendered layouts/_player.html.haml (0.1ms)
Completed 200 OK in 4ms (Views: 3.8ms | ActiveRecord: 0.0ms)
(0.1ms) rollback transaction
(0.0ms) begin transaction
User Exists (1.1ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
(0.0ms) SAVEPOINT active_record_1
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
User Exists (0.0ms) SELECT 1 AS one FROM "users" WHERE "users"."session" IS NULL LIMIT 1
Binary data inserted for `string` type on column `password_digest`
Binary data inserted for `string` type on column `session`
SQL (0.3ms) INSERT INTO "users" ("admin", "created_at", "name", "password_digest", "session", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["admin", nil], ["created_at", Sun, 21 Apr 2013 23:57:49 UTC +00:00], ["name", "anyone"], ["password_digest", "$2a$10$2kPN1wqnXI/G9b/1KMR2x.7yCHCaKwftE7PXm/q4u9Q9bcWCTenMG"], ["session", "91e2394dcdc86ada3836b258ad6bd2c850f99e03"], ["updated_at", Sun, 21 Apr 2013 23:57:49 UTC +00:00]]
(0.0ms) RELEASE SAVEPOINT active_record_1
Started GET "/logout" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#destroy as HTML
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 0ms (ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:49 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.9ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 2ms (Views: 1.9ms | ActiveRecord: 0.0ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.9ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 2ms (Views: 2.0ms | ActiveRecord: 0.0ms)
Started POST "/sessions" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "session"=>{"name"=>"anyone", "password"=>"[FILTERED]"}, "commit"=>"Login"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'anyone' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (0.8ms)
Rendered layouts/_player.html.haml (0.1ms)
Completed 200 OK in 2ms (Views: 1.8ms | ActiveRecord: 0.0ms)
Started GET "/tracks" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by TracksController#index as HTML
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."session" = '' LIMIT 1
Redirected to http://127.0.0.1:42286/login
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 2ms (ActiveRecord: 0.2ms)
Started GET "/login" for 127.0.0.1 at 2013-04-22 01:57:50 +0200
Processing by SessionsController#new as HTML
Rendered sessions/new.html.haml within layouts/application (27.5ms)
Rendered layouts/_player.html.haml (0.0ms)
Completed 200 OK in 29ms (Views: 28.9ms | ActiveRecord: 0.0ms)
(0.1ms) rollback transaction