-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof of concept: playwright e2e tests (#3001)
playwright e2e tests references #3009 references #2993 references #1759 * build assets in e2e pipeline We want to run the e2e tests with the latest assets without the need to commit them to the repository. Otherwise we would not see test failures from changes to the js code as early as possible.
- Loading branch information
Showing
18 changed files
with
1,204 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ phoenix_live_view-*.tar | |
|
||
node_modules | ||
|
||
/test/e2e/test-results/ | ||
/playwright-report/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# End-to-end tests | ||
|
||
This directory contains end-to-end tests that use the [Playwright](https://playwright.dev/) | ||
test framework. | ||
These tests use all three web engines (Chromium, Firefox, Webkit) and test the interaction | ||
with an actual LiveView server. | ||
|
||
## Running the tests | ||
|
||
To run the tests, ensure that the npm dependencies are installed by running `npm install` in | ||
the root of the repository. Then, run `npm run e2e:test` to run the tests. | ||
|
||
This will execute the `npx playwright test` command in the `test/e2e` directory. Playwright | ||
will start a LiveView server using the `MIX_ENV=e2e mix run test/e2e/test_helper.exs` command. | ||
|
||
Playwright supports an [interactive UI mode](https://playwright.dev/docs/test-ui-mode) that | ||
can be used to debug the tests. To run the tests in this mode, run `npm run e2e:test -- --ui`. | ||
|
||
Tests can also be run in headed mode by passing the `--headed` flag. This is especially useful | ||
in combination with running only specific tests, for example: | ||
|
||
```bash | ||
npm run e2e:test -- tests/streams.spec.js:9 --project chromium --headed | ||
``` | ||
|
||
To step through a single test, pass `--debug`, which will automatically run the test in headed | ||
mode: | ||
|
||
```bash | ||
npm run e2e:test -- tests/streams.spec.js:9 --project chromium --debug | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// playwright.config.js | ||
// @ts-check | ||
const { devices } = require("@playwright/test"); | ||
|
||
/** @type {import("@playwright/test").PlaywrightTestConfig} */ | ||
const config = { | ||
forbidOnly: !!process.env.CI, | ||
retries: process.env.CI ? 2 : 0, | ||
reporter: process.env.CI ? [["github"], ["html"]] : "list", | ||
use: { | ||
trace: "retain-on-failure", | ||
screenshot: "only-on-failure", | ||
baseURL: "http://localhost:4000/", | ||
ignoreHTTPSErrors: true, | ||
}, | ||
webServer: { | ||
command: "npm run e2e:server", | ||
url: "http://127.0.0.1:4000/health", | ||
reuseExistingServer: !process.env.CI, | ||
stdout: "pipe", | ||
stderr: "pipe", | ||
}, | ||
projects: [ | ||
{ | ||
name: "chromium", | ||
use: { ...devices["Desktop Chrome"] }, | ||
}, | ||
{ | ||
name: "firefox", | ||
use: { ...devices["Desktop Firefox"] }, | ||
}, | ||
{ | ||
name: "webkit", | ||
use: { ...devices["Desktop Safari"] }, | ||
} | ||
], | ||
outputDir: "test-results" | ||
}; | ||
|
||
module.exports = config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
Application.put_env(:phoenix_live_view, Phoenix.LiveViewTest.E2E.Endpoint, | ||
http: [ip: {127, 0, 0, 1}, port: 4000], | ||
# TODO: switch to bandit when Phoenix 1.7 is used | ||
# adapter: Bandit.PhoenixAdapter, | ||
server: true, | ||
live_view: [signing_salt: "aaaaaaaa"], | ||
secret_key_base: String.duplicate("a", 64), | ||
render_errors: [ | ||
# TODO: uncomment when LV Phoenix 1.7 is used | ||
# formats: [ | ||
# html: Phoenix.LiveViewTest.E2E.ErrorHTML, | ||
# ], | ||
view: Phoenix.LiveViewTest.E2E.ErrorHTML, | ||
layout: false | ||
], | ||
pubsub_server: Phoenix.LiveViewTest.E2E.PubSub, | ||
debug_errors: false | ||
) | ||
|
||
defmodule Phoenix.LiveViewTest.E2E.ErrorHTML do | ||
def render(template, _), do: Phoenix.Controller.status_message_from_template(template) | ||
end | ||
|
||
defmodule Phoenix.LiveViewTest.E2E.Layout do | ||
use Phoenix.Component | ||
|
||
def render("live.html", assigns) do | ||
~H""" | ||
<script src="/assets/phoenix/phoenix.min.js"></script> | ||
<script src="/assets/phoenix_live_view/phoenix_live_view.js"></script> | ||
<script> | ||
let liveSocket = new window.LiveView.LiveSocket("/live", window.Phoenix.Socket) | ||
liveSocket.connect() | ||
</script> | ||
<style> | ||
* { font-size: 1.1em; } | ||
</style> | ||
<%= @inner_content %> | ||
""" | ||
end | ||
end | ||
|
||
defmodule Phoenix.LiveViewTest.E2E.Router do | ||
use Phoenix.Router | ||
import Phoenix.LiveView.Router | ||
|
||
pipeline :browser do | ||
plug(:accepts, ["html"]) | ||
end | ||
|
||
live_session :default, layout: {Phoenix.LiveViewTest.E2E.Layout, :live} do | ||
scope "/" do | ||
pipe_through(:browser) | ||
|
||
live "/stream", Phoenix.LiveViewTest.StreamLive | ||
live "/stream/reset", Phoenix.LiveViewTest.StreamResetLive | ||
live "/stream/reset-lc", Phoenix.LiveViewTest.StreamResetLCLive | ||
live "/healthy/:category", Phoenix.LiveViewTest.HealthyLive | ||
|
||
live "/upload", Phoenix.LiveViewTest.E2E.UploadLive | ||
live "/form", Phoenix.LiveViewTest.E2E.FormLive | ||
end | ||
end | ||
end | ||
|
||
defmodule Phoenix.LiveViewTest.E2E.Endpoint do | ||
use Phoenix.Endpoint, otp_app: :phoenix_live_view | ||
|
||
socket("/live", Phoenix.LiveView.Socket) | ||
|
||
plug Plug.Static, from: {:phoenix, "priv/static"}, at: "/assets/phoenix" | ||
plug Plug.Static, from: {:phoenix_live_view, "priv/static"}, at: "/assets/phoenix_live_view" | ||
plug Plug.Static, from: System.tmp_dir!(), at: "/tmp" | ||
|
||
plug :health_check | ||
|
||
plug Phoenix.LiveViewTest.E2E.Router | ||
|
||
defp health_check(%{request_path: "/health"} = conn, _opts) do | ||
conn |> Plug.Conn.send_resp(200, "OK") |> Plug.Conn.halt() | ||
end | ||
|
||
defp health_check(conn, _opts), do: conn | ||
end | ||
|
||
{:ok, _} = | ||
Supervisor.start_link( | ||
[ | ||
Phoenix.LiveViewTest.E2E.Endpoint, | ||
{Phoenix.PubSub, name: Phoenix.LiveViewTest.E2E.PubSub} | ||
], | ||
strategy: :one_for_one | ||
) | ||
|
||
Process.sleep(:infinity) |
Oops, something went wrong.