1

I have an RSPec test suite for my ruby on rails application as follows:

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 

Before each of these scenarios runs, a background sign_in_as module runs :

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 

All scenarios where I dont set :js=>true work fine. The scenario "Buyer creates a new event" javascript is important, so I want to use my webkit which will enable javascript and the capybara page.execute_script method.... the test fails with :

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

This code runs before all scenarios and works in the previous 8 tests, so why does it fail now?

When I save_and_open_page I realise somehow the test is sitting on example.com! Why is this? I feel if I find out why this is - the problem gets fixed!

This is my 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 

I am assuming the problem is related to something in here, or something that is missing from here, but I cannot work it out!

**Requested routes file **

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 

UPDATED AS PER SUGGESTION TO GREP FOR EXAMPLE.COM A grep was performed for example.com - here are the results:

http://pastebin.com/WgcHcRAg

As you can see - nothing in the source tree (apart from some mailer view content which is irrelevant to this issue) - but a lot of mentions in tmp and capybara....so where is this example.com coming from? It seems to me it is coming from capybara configuration somewhere...

4
  • 1
    Can you post your routes file? Commented Jul 7, 2015 at 14:19
  • Added to the original post :) Commented Jul 7, 2015 at 14:24
  • 1
    Not sure but if I were you I would grep all files for 'example.com'. Commented Jul 7, 2015 at 15:18
  • Added grep contents - doesn't highlight anything obvious apart from capybara seems to somehow have many references to example.com in the tmp folder....the only mentions of example.com in the source tree are from mailer view content which is not relevant to this problem! Commented Jul 7, 2015 at 17:28

2 Answers 2

7

Don't use url helpers with visit, instead use the path helpers.

visit welcome_page_path 

When you use the url helper rails is adding on whatever host and port are specified in its default_url_options setting. The tests worked without js: true because the racktest driver ignores hostnames

Sign up to request clarification or add additional context in comments.

3 Comments

Aha - Ok so that actually fixed it! New problem though! For some reason the background - sign in as is failing with "Invalid email or password"...odd considering it passes every other previous scenario which works perfectly. Is there something about this new driver which would cause the background module sign_in_as to act differently?
I would guess you are still using transaction mode for database access. That doesnt work when capybara is running a server for real browsers to connect to since the tests and app don't have access to the same database connection instance. You probably need to read the "Transactions and database setup" section of the capybara readme
Perfect. I installed database cleaner by adding gem "database_cleaner" to the gem file and the suggested config for rspec with capybara :) Thank you very much!
0

When using JS with Capybara Webkit, I recommend using a Database cleaner and turning your transactional fixtures to false.

EDIT

spec/rails_helper.rb

config.use_transactional_fixtures = false 

Your application isn't seeing your user in your test database because it is wrapped in a transactional fixture.

Per capybara README...

Since transactions are usually not shared across threads, this will cause data you have put into the database in your test code to be invisible to Capybara.

You can find more information in the database_cleaner README under the Rspec/Capybara section.

1 Comment

How does this answer the question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.