2013-04-22 54 views
2

我有一個簡單的登錄系統,在瀏覽器中工作。我最近把我的黃瓜測試切換到使用硒,因爲我需要ajax調用,現在登錄/認證步驟不再通過。該應用程序仍然正常運行在Firefox和鉻。硒與水豚失敗。在瀏覽器中工作

這裏的步驟和定義

特徵/ 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.r b

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 
+0

錯誤信息是什麼? – 2013-04-22 00:20:13

+0

沒有,測試失敗,因爲登錄不起作用。 「Processing by TracksController#index as HTML User Load(0.2ms)SELECT」users「。* FROM」users「WHERE」users「。」session「=''LIMIT 1 重定向到http://127.0.0.1 :42286/login'是可疑的。會話應該正確傳遞,但不是由於某種原因。 – meredrica 2013-04-22 13:01:30

回答

2

解決的辦法是不要將selenium設置爲默認的水豚驅動程序,並用@javascript標記需要ajax的測試。

+0

我遇到過類似的情況;某些AJAX代碼在瀏覽器中可用,但使用PhantomJS或Selenium驅動程序時,Capybara規格會失敗。我很想知道更多關於你如何解決你的問題。你能詳細解釋一下嗎?你是否說測試破裂?因爲Selenium是默認的驅動程序? – 2015-02-07 23:37:43