Skip to content

Commit

Permalink
Only spawn a browser when using the feature macro, not normal `test…
Browse files Browse the repository at this point in the history
…`s (#795)

* Only spawn a browser when using the `feature` macro, not normal `test`s

This fixes an unintended behavior where having `use Wallaby.Feature` at the top of your module would cause a browser to be spawned for every test in the module, not just `feature` tests. The result is that if you had a large test module, most of which used the normal `test` macro and one of which used the `feature` macro, every test would take a minimum of a third of a second or more.

I also took the liberty to check and see in the `setup` block if we already had a `:session`, and if so, avoid spawning a second. I don't *think* anyone was relying on the behavior where a second browser was being spawned if you had `use Wallaby.Feature` at both the top of the module and a `describe` block.

Resolves #794

* Bump upload-artifact version to fix deprecation error

* Address PR feedback
  • Loading branch information
s3cur3 authored Jan 3, 2025
1 parent c144c92 commit 4e87275
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ jobs:
- name: Run Tests
run: mix test || mix test --failed || mix test --failed

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: always()
with:
name: Selenium Logs
Expand Down Expand Up @@ -210,7 +210,7 @@ jobs:
- name: Run Tests
run: mix test || mix test --failed || mix test --failed

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: always()
with:
name: Selenium Logs
Expand Down Expand Up @@ -250,7 +250,7 @@ jobs:
- name: Run Tests
run: mix test || mix test --failed || mix test --failed

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
if: always()
with:
name: Selenium Logs
Expand Down
4 changes: 4 additions & 0 deletions integration_test/cases/feature/use_feature_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ defmodule Wallaby.Integration.Browser.UseFeatureTest do
feature "reads capabilities from session attribute", %{session: %{capabilities: capabilities}} do

Check failure on line 34 in integration_test/cases/feature/use_feature_test.exs

View workflow job for this annotation

GitHub Actions / Selenium V4 (27.x, 1.17.0-rc.1)

feature reads capabilities from session attribute (Wallaby.Integration.Browser.UseFeatureTest)
assert capabilities.test == @expected_capabilities.test
end

test "does not set up a session for non-feature tests", context do
refute is_map_key(context, :session)
end
end
36 changes: 20 additions & 16 deletions lib/wallaby/feature.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ defmodule Wallaby.Feature do
import Wallaby.Feature

setup context do
metadata = unquote(__MODULE__).Utils.maybe_checkout_repos(context[:async])

start_session_opts =
[metadata: metadata]
|> unquote(__MODULE__).Utils.put_create_session_fn(context[:create_session_fn])

get_in(context, [:registered, :sessions])
|> unquote(__MODULE__).Utils.sessions_iterable()
|> Enum.map(fn
opts when is_list(opts) ->
unquote(__MODULE__).Utils.start_session(opts, start_session_opts)

i when is_number(i) ->
unquote(__MODULE__).Utils.start_session([], start_session_opts)
end)
|> unquote(__MODULE__).Utils.build_setup_return()
if context[:test_type] == :feature do
metadata = unquote(__MODULE__).Utils.maybe_checkout_repos(context[:async])

start_session_opts =
[metadata: metadata]
|> unquote(__MODULE__).Utils.put_create_session_fn(context[:create_session_fn])

get_in(context, [:registered, :sessions])
|> unquote(__MODULE__).Utils.sessions_iterable()
|> Enum.map(fn
opts when is_list(opts) ->
unquote(__MODULE__).Utils.start_session(opts, start_session_opts)

i when is_number(i) ->
unquote(__MODULE__).Utils.start_session([], start_session_opts)
end)
|> unquote(__MODULE__).Utils.build_setup_return()
else
:ok
end
end
end
end
Expand Down

0 comments on commit 4e87275

Please sign in to comment.