Skip to content

Commit

Permalink
Add a new integration test helper
Browse files Browse the repository at this point in the history
Add an integration test helper for use in integration tests for pages
 that have been transitioned to the GOV.UK Design System.

Has a minimal implementation for now, which is copied from the legacy
 integration test helper.
  • Loading branch information
mtaylorgds committed Jul 16, 2024
1 parent 6325b26 commit 7c43814
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
4 changes: 2 additions & 2 deletions test/integration/downtime_integration_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "legacy_integration_test_helper"
require "integration_test_helper"

class DowntimeIntegrationTest < LegacyJavascriptIntegrationTest
class DowntimeIntegrationTest < JavascriptIntegrationTest
setup do
setup_users

Expand Down
4 changes: 2 additions & 2 deletions test/integration/downtime_with_invalid_dates_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "legacy_integration_test_helper"
require "integration_test_helper"

class DowntimeWithInvalidDates < LegacyPublisherIntegrationTest
class DowntimeWithInvalidDates < PublisherIntegrationTest
setup do
setup_users

Expand Down
62 changes: 62 additions & 0 deletions test/integration_test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require "test_helper"
require "capybara/rails"
require "capybara-select-2"
require "support/govuk_test"

class PublisherIntegrationTest < ActionDispatch::IntegrationTest
include Capybara::DSL
include CapybaraSelect2
include CapybaraSelect2::Helpers
include Warden::Test::Helpers

teardown do
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver
GDS::SSO.test_user = nil
end

def setup_users
# This may not be the right way to do things. We rely on the gds-sso
# having a strategy that uses the first user. We probably want some
# tests that cover the oauth interaction properly
@author = FactoryBot.create(:user, :govuk_editor, name: "Author", email: "[email protected]")
@reviewer = FactoryBot.create(:user, :govuk_editor, name: "Reviewer", email: "[email protected]")
end

def login_as(user)
GDS::SSO.test_user = user
super(user)
end
end

class JavascriptIntegrationTest < PublisherIntegrationTest
setup do
Capybara.current_driver = Capybara.javascript_driver
end

# Get a single user by their name. If the user doesn't exist, return nil.
def get_user(name)
User.where(name:).first
end

# Set the given user to be the current user
# Accepts either a User object or a user's name
def login_as(user)
unless user.is_a?(User)
user = get_user(user)
end
clear_cookies
GDS::SSO.test_user = user
end

def clear_cookies
browser = Capybara.current_session.driver.browser
if browser.respond_to?(:clear_cookies)
# Rack::MockSession
browser.clear_cookies
elsif browser.respond_to?(:manage) && browser.manage.respond_to?(:delete_all_cookies)
# Selenium::WebDriver
browser.manage.delete_all_cookies
end
end
end

0 comments on commit 7c43814

Please sign in to comment.