ruby-on-rails - capybara 用 Selenium 失败了。在浏览器中工作

标签 ruby-on-rails cucumber capybara selenium-webdriver

我有一个可以在浏览器中运行的简单登录系统。我最近将 cucumber 测试切换为使用 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     

features/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

features/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

features/support/helpers/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

日志/测试.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

最佳答案

修复方法是不将 selenium 设置为默认的 capybara 驱动程序,并使用 @javascript 标记需要 ajax 的测试。

关于ruby-on-rails - capybara 用 Selenium 失败了。在浏览器中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16137524/

相关文章:

ruby-on-rails - RSpec - Capybara 和 Puma - 路由错误

ruby-on-rails - SSL_connect returned=1 errno=0 state=SSLv3 读取服务器证书 B : certificate verify failed

ruby-on-rails - Rspec Controller 测试在 #create 与关联后失败

java - 为什么我的 cucumber 测试用例被跳过?

selenium - 使用 Capybara 测试 javascript 警报

ruby - `send_keys` 在 capybara 中做了什么

ruby-on-rails - gem install typhoeus - 未能构建 gem 扩展

ruby-on-rails - 使用 assert_select 查找选择框值?

ruby - 检查 Appium 中是否显示具有不同文本大小写的元素并执行相应的逻辑

ruby-on-rails - 在 Capybara 中取消选中复选框的正确方法