mardi 27 février 2018

Issues with system test setup using Capybara and Selenium on existing Rails 5.1 app

I'm attempting to set up system tests with Capybara and Selenium on an existing Rails 5.1 app that already had capybara based feature tests. Here's what I've done so far.

In the gem file under group :development, :test:

gem 'chromedriver-helper'  
gem 'selenium-webdriver'
gem 'rack_session_access'

In the environments/development.rb and environments/test.rb:

config.action_mailer.default_url_options = { host: 'localhost:3000' } 

In the spec\rails_helper.rb:

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

Capybara.configure do |config|
  config.default_max_wait_time = 10 # seconds
  config.default_driver = :selenium
  config.app_host = 'http://localhost:3000'
  config.server_host = 'localhost'
end

The issues I'm having are both with new systems tests and old feature tests.

With the system tests it appears that Capybara isn't creating a page object as I get undefined local variable or method 'page' Additionally when I duplicate the same test under the feature test directory I don't have this issue.

With the old Capybara feature tests, working with the rackTest driver, a Chrome window opens but I get No route matches [GET] "/rack_session/edit"

config.middleware.use RackSessionAccess::Middleware is already present in the environments/test.rb

Example system test:

require 'rails_helper'

describe User do
  let(:user) { create :user }
  let(:membership) { create :membership, admin: true}
  let(:admin) { create :user, memberships: [membership] }

  context 'viewing the index' do
    it 'directs you to the appropriate page' do
      set_current_user(admin)
      visit root_url
      click_button 'Manage Users'
      expect(page.current_url).to end_with users_path
      expect(page).to have_selector 'h1', text: 'Users'
    end
  end
end

Example feature test:

require 'rails_helper'

describe 'edit an assignment' do
  let(:roster) { create :roster }
  let(:user) { create :user, rosters: [roster] }
  before :each do
    Timecop.freeze Date.new(2018, 1, 10)
    set_current_user(user)
  end
  after :each do
    Timecop.return
  end

  context 'returns the user to the appropriate index page' do
    let(:date_today) { Date.new(2017, 4, 4) }
    let(:start_date) { Date.new(2017, 3, 31) }
    let(:month_date) { date_today.beginning_of_month }
    it 'redirects to the correct URL' do
      visit roster_assignments_url(roster, date: date_today)
      visit new_roster_assignment_url(roster, date: start_date)
      click_button 'Create'
      expect(current_url)
        .to eq roster_assignments_url(roster,
                                      date: month_date)
    end

In the spec_helper:

def set_current_user(user)
  page.set_rack_session user_id: user.id
end

Aucun commentaire:

Enregistrer un commentaire