Skip to content

Commit

Permalink
Fix default host in setup, move teardown to helper file
Browse files Browse the repository at this point in the history
* Override integration test default host

Integration tests automatically set the default host to
'http://example.com'. This works fine for integration tests because they
are not real browser sessions, but doesn't work fine for system tests
because they are real browser sessions.

We can override this by setting the `host!` in `before_setup. The
`Capybara.always_include_port` will allow the test to look at
`127.0.0.1:port capybara picks` and properly redirect the test.

Any application can override this by setting the `host!` in
their system test helper. Generally though, applications are going to be
using localhost.

In this commit I also moved the setup and teardown into their own module
for tidiness.

* Move teardown settings into system test case

These configuration options can be put into the system test case file
instead of the generated system tests helper file. This is an
implementation detail and therefore shouldn't be generated with the
template.
  • Loading branch information
eileencodes committed Feb 20, 2017
1 parent 983275e commit 161cf89
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
2 changes: 2 additions & 0 deletions actionpack/lib/action_dispatch/system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "action_dispatch/system_testing/server"
require "action_dispatch/system_testing/browser"
require "action_dispatch/system_testing/test_helpers/screenshot_helper"
require "action_dispatch/system_testing/test_helpers/setup_and_teardown"

module ActionDispatch
class SystemTestCase < IntegrationTest
Expand Down Expand Up @@ -92,6 +93,7 @@ class SystemTestCase < IntegrationTest
# and Rails, any driver that is supported by Capybara is supported by system
# tests as long as you include the required gems and files.
include Capybara::DSL
include SystemTesting::TestHelpers::SetupAndTeardown
include SystemTesting::TestHelpers::ScreenshotHelper

def self.start_application # :nodoc:
Expand Down
9 changes: 9 additions & 0 deletions actionpack/lib/action_dispatch/system_testing/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ def register
end

def setup
set_server
set_port
end

def set_server
Capybara.server = :rails_puma
end

def set_port
Capybara.always_include_port = true
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module ActionDispatch
module SystemTesting
module TestHelpers
module SetupAndTeardown # :nodoc:
DEFAULT_HOST = "127.0.0.1"

def before_setup
host! DEFAULT_HOST
super
end

def after_teardown
super
take_failed_screenshot
Capybara.reset_sessions!
end
end
end
end
end
9 changes: 8 additions & 1 deletion actionpack/test/dispatch/system_testing/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
require "action_dispatch/system_testing/server"

class ServerTest < ActiveSupport::TestCase
setup do
ActionDispatch::SystemTesting::Server.new.run
end

test "initializing the server port" do
server = ActionDispatch::SystemTesting::Server.new.run
assert_includes Capybara.servers, :rails_puma
end

test "port is always included" do
assert Capybara.always_include_port, "expected Capybara.always_include_port to be true"
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]

teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]

teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,4 @@

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium, using: :chrome, screen_size: [1400, 1400]

teardown do
take_failed_screenshot
Capybara.reset_sessions!
end
end

0 comments on commit 161cf89

Please sign in to comment.