Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Wallaby driver #74

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ phoenix_test-*.tar
# Temporary files, for example, from tests.
/tmp/

# Local configuration
.envrc

# Ignore assets that are produced by build tools.
/priv/static/assets/
/priv/static/assets/*
!/priv/static/assets/app.css
8 changes: 8 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ config :esbuild,
cd: Path.expand("../test/assets", __DIR__),
env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)}
]

config :wallaby,
otp_app: :phoenix_test,
js_logger: nil,
chromedriver: [
headless: System.get_env("WALLABY_HEADLESS", "t") in ["t", "true"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go in runtime.exs

binary: System.get_env("CHROME_EXECUTABLE", "")
]
32 changes: 32 additions & 0 deletions lib/phoenix_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,12 @@ defmodule PhoenixTest do
LiveView or a static view. You don't need to worry about which type of page
you're visiting.
"""
if Code.ensure_loaded?(Wallaby) do
def visit(%Plug.Conn{assigns: %{phoenix_test_js: true}} = conn, path) do
PhoenixTest.Wallaby.build(conn, path)
end
end

def visit(conn, path) do
conn
|> recycle(all_headers(conn))
Expand All @@ -237,6 +243,32 @@ defmodule PhoenixTest do
Enum.map(conn.req_headers, &elem(&1, 0))
end

@doc """
Helper to run tests with the Javascript (Wallaby) driver via `@tag :js`.

```elixir
setup %{conn: conn} = context do
conn = if context[:js], do: with_js_driver(conn), else: conn
%{conn: conn}
end

@tag :js
test "with wallaby", %{conn: conn} do
visit(conn, "/path")
end

test "without wallaby", %{conn: conn} do
visit(conn, "/path")
end
```

You can also use `@describetag :js` and `@moduletag :js`.
Refer to the [ExUnit docs](https://hexdocs.pm/ex_unit/1.16.3/ExUnit.Case.html#module-tags) for more details.
"""
def with_js_driver(%Plug.Conn{} = conn) do
Plug.Conn.assign(conn, :phoenix_test_js, true)
end

@doc """
Clicks a link with given text and performs the action.

Expand Down
14 changes: 8 additions & 6 deletions lib/phoenix_test/field.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ defmodule PhoenixTest.Field do
alias PhoenixTest.Query
alias PhoenixTest.Utils

@enforce_keys ~w[source_raw parsed label id name value selector]a
defstruct ~w[source_raw parsed label id name value selector]a
@enforce_keys ~w[source_raw parsed label id name value selector raw]a
defstruct ~w[source_raw parsed label id name value selector raw]a

def find_input!(html, input_selectors, label, opts) do
field = Query.find_by_label!(html, input_selectors, label, opts)
Expand All @@ -23,13 +23,13 @@ defmodule PhoenixTest.Field do
id: id,
name: name,
value: value,
selector: Element.build_selector(field)
selector: Element.build_selector(field),
raw: field
}
end

def find_checkbox!(html, input_selector, label, opts) do
field = Query.find_by_label!(html, input_selector, label, opts)

id = Html.attribute(field, "id")
name = Html.attribute(field, "name")
value = Html.attribute(field, "value") || "on"
Expand All @@ -41,7 +41,8 @@ defmodule PhoenixTest.Field do
id: id,
name: name,
value: value,
selector: Element.build_selector(field)
selector: Element.build_selector(field),
raw: field
}
end

Expand All @@ -60,7 +61,8 @@ defmodule PhoenixTest.Field do
id: id,
name: name,
value: value,
selector: Element.build_selector(field)
selector: Element.build_selector(field),
raw: field
}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/phoenix_test/link.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule PhoenixTest.Link do
@moduledoc false

alias PhoenixTest.Element
alias PhoenixTest.Html
alias PhoenixTest.Query
alias PhoenixTest.Utils
Expand All @@ -17,7 +18,7 @@ defmodule PhoenixTest.Link do
raw: link_html,
parsed: link,
id: id,
selector: selector,
selector: Element.build_selector(link),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a required change. Just for consistency with the other modules.

text: text,
href: href
}
Expand Down
Loading
Loading