【问题标题】:Capybara webkit redirects me to example.com in RSpec testsCapybara webkit 在 RSpec 测试中将我重定向到 example.com
【发布时间】:2016-04-01 05:22:48
【问题描述】:

我的 ruby​​ on rails 应用程序有一个 RSPec 测试套件,如下所示:

scenario "Buyer visits people you follow page" do
   ...do stuff
  end

  scenario "Buyer logs out" do
   ...do stuff
  end

  scenario "Buyer creates a new event", :js => true do
   ...do stuff
  end

在每个场景运行之前,都会运行一个后台 sign_in_as 模块:

module SignInHelpers
  def sign_in_as(user = FactoryGirl.create(:user))
    visit welcome_page_url
    save_and_open_page
    fill_in :user_email, with: user.email
    fill_in :user_password, with: "password"
    click_button "Log in"
  end
end

设置 :js=>true 的所有场景都可以正常工作。场景“买家创建一个新事件”javascript 很重要,所以我想使用我的 webkit 来启用 javascript 和 capybara page.execute_script 方法....测试失败:

Buyer Features Buyer creates a new event
     Failure/Error: sign_in_as
     Capybara::ElementNotFound:
       Unable to find field :user_email

这段代码在所有场景之前运行,并且在前面的 8 次测试中都有效,那为什么现在它失败了?

当我保存_and_open_page 时,我意识到测试在 example.com 上!为什么是这样?我觉得如果我找出原因 - 问题就会得到解决!

这是我的 spec_helper.rb:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"

Capybara.javascript_driver = :webkit

Capybara::Webkit.configure do |config|
  # Enable debug mode. Prints a log of everything the driver is doing.
  config.debug = false

  # By default, requests to outside domains (anything besides localhost) will
  # result in a warning. Several methods allow you to change this behavior.


  # Allow pages to make requests to any URL without issuing a warning.
  config.allow_unknown_urls

  # Allow a specifc domain without issuing a warning.
  config.allow_url("localhost")

  # Timeout if requests take longer than 5 seconds
  config.timeout = 10

  # Don't raise errors when SSL certificates can't be validated
  config.ignore_ssl_errors

end



Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|

  config.use_transactional_fixtures = true
  config.mock_with :rspec
  config.expect_with :rspec do |expectations|

    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|

    mocks.verify_partial_doubles = true
  end


=begin

  config.filter_run :focus
  config.run_all_when_everything_filtered = true


  config.example_status_persistence_file_path = "spec/examples.txt"


  config.disable_monkey_patching!


  if config.files_to_run.one?

    config.default_formatter = 'doc'
  end


  config.profile_examples = 10


  config.order = :random


  Kernel.srand config.seed
=end
end

我假设问题与此处的某些内容或此处缺少的某些内容有关,但我无法解决!

**请求的路由文件**

Rails.application.routes.draw do
  resources :invitations

  post 'utilities/getUserLocation', to: 'utilities#getUserLocation'

  resources :geo_ip_request

  resources :events

  get 'sessions/quick', to: 'sessions#quickBuy', as: "quick_buy"
  get 'sessions/:id', to: 'sessions#show', as: "session"


  post 'sessions/quick', to: 'sessions#getSession'
  post 'sessions/checkin', to: 'sessions#checkin'

  resources :tests

  get '/search', to: 'search#search', as: "search_search"

  get 'welcome/index', as: "welcome_page"



  #get 'sessions/show/:id', to: 'sessions#show', as: "session_show"

  devise_for :users, path: "users", path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', sign_up: 'new/(:invitation_token)' }, controllers: {sessions: 'users/sessions', registrations: 'users/registrations',  :omniauth_callbacks => "users/omniauth_callbacks"}


#resources :users

devise_scope :user do 
 get '/users/:id', to: 'users#show', as: 'user'
end

get '/users/eventOwner/:id', to: 'users#showEventOwner', as: 'userShowEventOwner'

post  'events/:id', to: 'events#buyTicket'

get 'users/:id/connect', to: 'users#connect', as: "connect_users"

get 'users/confirm', to: 'users#confirm', as: "confirm_users"

get 'users/followers/:id', to:'users#show_followers', as: "show_followers"
get 'users/followees/:id', to:'users#show_followees', as: "show_followees"
post 'users/follow/:id', to: 'users#follow_user', as:"follow_user" 


   #add route to show users favourites collection

   get 'users/favourites/:id', to: 'users#show_favourites', as: 'show_favourites'
   post 'users/favourites/add', to: 'users#add_favourite', as: 'add_favourite'


   patch 'attendees', to: 'attendees#update', as: "update_attendees"

 #get 'users/:id', to: 'users/users#show'
 get 'charges/error', to: 'charges#error', as: "payment_error"
 get 'charges/:id', to: 'charges#show'
 post 'charges/:id', to: 'charges#show'
 post 'charges/', to: 'charges#show'
 get 'listings/:id', to: 'listings#index', as: "listings_index"
 get 'listings/allsales/:id', to: 'listings#all_sales', as: "listings_all_sales"
 get 'listings/allsessions/:id', to: 'listings#all_sessions', as: "listings_all_sessions"



  #ROOT IS LANDING BUT TO BE CHANGED ONCE WE LAUNCH
  root 'landing_page#index'

  #path for landing page

  get '/', to: 'landing_page#index' , as: "landing_page"

#paths for job spec pdfs - controller has click tracking so we can track download numbers
get 'landing_page/download/SEPDF', to: 'landing_page#downloadSEPDF', as: "downloadSEPDF"
get 'landing_page/download/DMPDF', to: 'landing_page#downloadDMPDF', as: "downloadDMPDF"
get 'landing_page/download/SEINTERN', to: 'landing_page#downloadSEInternPDF', as: "downloadSEInternPDF"
get 'landing_page/download/DMINTERN', to: 'landing_page#downloadDMInternPDF', as: "downloadDMInternPDF"

post '/welcome', to: 'welcome#setUserLocation', as: "ajax_Set_User_Location"


   #add route for internal metrics page

   get '/metrics', to: 'metrics#index', as: 'internal_metrics'

   #add route for customer management system

   get 'cms', to: 'customer_management_system#index', as: 'customer_management_system'


 end

根据 GREP FOR EXAMPLE.COM 的建议进行了更新 对 example.com 执行了 grep - 以下是结果:

http://pastebin.com/WgcHcRAg

正如您所看到的 - 源代码树中没有任何内容(除了一些与此问题无关的邮件视图内容) - 但是在 tmp 和 capybara 中有很多提及......所以这个 example.com 来自哪里?在我看来,它来自某个地方的水豚配置......

【问题讨论】:

  • 你能发布你的路线文件吗?
  • 添加到原帖:)
  • 不确定,但如果我是你,我会 grep 'example.com' 的所有文件。
  • 添加了 grep 内容——除了 capybara 之外没有突出任何明显的东西似乎在 tmp 文件夹中有很多对 example.com 的引用......源代码树中唯一提到 example.com来自与此问题无关的邮件视图内容!

标签: ruby-on-rails ruby rspec capybara capybara-webkit


【解决方案1】:

不要在访问中使用 url 助手,而是使用路径助手。

visit welcome_page_path

当您使用 url 帮助程序时,rails 会在其 default_url_options 设置中指定的任何主机和端口上添加。测试没有 js: true 因为 racktest 驱动忽略了主机名

【讨论】:

  • 啊哈 - 好的,所以实际上修复了它!不过新问题!出于某种原因,背景 - 以“无效的电子邮件或密码”登录失败......奇怪的是考虑到它通过了所有其他之前完美运行的场景。这个新驱动程序是否会导致后台模块 sign_in_as 的行为有所不同?
  • 我猜您仍在使用事务模式进行数据库访问。当 capybara 运行服务器以供真正的浏览器连接时,这不起作用,因为测试和应用程序无法访问相同的数据库连接实例。您可能需要阅读 capybara 自述文件的“事务和数据库设置”部分
  • 完美。我通过将 gem "database_cleaner" 添加到 gem 文件和建议的 rspec 配置与水豚来安装数据库清洁器 :) 非常感谢!
【解决方案2】:

在将 JS 与 Capybara Webkit 一起使用时,我建议使用数据库清理器并将您的事务装置设置为 false。

编辑

spec/rails_helper.rb

config.use_transactional_fixtures = false

您的应用程序没有在您的测试数据库中看到您的用户,因为它被包装在事务性夹具中。

每只水豚README...

由于事务通常不跨线程共享,这将导致您在测试代码中放入数据库的数据对 Capybara 不可见。

您可以在 Rspec/Capybara 部分下的 database_cleaner README 中找到更多信息。

【讨论】:

  • 这如何回答这个问题?
猜你喜欢
  • 2012-10-27
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多