Skip to content

Commit 5ae855f

Browse files
committed
Sample showing authentication with Playwright
1 parent 8c976a3 commit 5ae855f

File tree

6 files changed

+99
-7
lines changed

6 files changed

+99
-7
lines changed

2wr-app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
node_modules
33
/dist
44

5+
/tests/_storagestate
56

67
# local env files
78
.env

2wr-app/playwright.config.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
// playwright.config.js
22
// @ts-check
33
/** @type {import('@playwright/test').PlaywrightTestConfig} */
4+
5+
if (
6+
!process.env.TWO_WEEKS_READY_E2E_TEST_USERNAME ||
7+
!process.env.TWO_WEEKS_READY_E2E_TEST_PASSWORD
8+
) {
9+
console.error(
10+
"TWO_WEEKS_READY_E2E_TEST_USERNAME and TWO_WEEKS_READY_E2E_TEST_PASSWORD environment variables must be set for end-to-end tests to run."
11+
);
12+
process.exit(-1);
13+
}
14+
415
const config = {
16+
timeout: 120 * 1000,
17+
webServer: {
18+
command: "npm run serve",
19+
port: 8080,
520
timeout: 120 * 1000,
6-
webServer: {
7-
command: 'npm run serve',
8-
port: 8080,
9-
timeout: 120 * 1000,
10-
},
21+
reuseExistingServer: !process.env.CI
22+
},
23+
globalSetup: require.resolve("./tests/global-setup"),
24+
use: {
25+
// Tell all tests to load signed-in state from 'storageState.json'.
26+
storageState: "./tests/_storagestate/storageState.json"
27+
}
1128
};
12-
module.exports = config;
29+
module.exports = config;

2wr-app/src/components/welcome/welcome-landing.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@
4141
</v-row>
4242
<v-fab-transition>
4343
<v-btn
44+
data-testid="welcome-next-button"
4445
color="green"
4546
dark
4647
absolute
4748
bottom
4849
right
4950
fab
50-
class="mb-12"
51+
class="mb-12 welcome-next-button"
5152
@click="next()"
5253
aria-label="Next"
5354
>
@@ -75,3 +76,8 @@ export default {
7576
},
7677
};
7778
</script>
79+
<style>
80+
.v-btn--fab.welcome-next-button {
81+
z-index: 10
82+
}
83+
</style>

2wr-app/tests/global-setup.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// global-setup.js
2+
const { chromium, expect } = require("@playwright/test");
3+
4+
module.exports = async () => {
5+
const browser = await chromium.launch();
6+
const page = await browser.newPage();
7+
await page.goto("http://localhost:8080/");
8+
9+
// Save not signed-in state to 'newuser-storageState.json'.
10+
await page
11+
.context()
12+
.storageState({ path: "./tests/_storagestate/newuser-storageState.json" });
13+
14+
await page.getByTestId("welcome-next-button").click();
15+
await expect(
16+
page.getByRole("heading", { name: "You and earthquake risk" })
17+
).toBeVisible();
18+
await page.getByTestId("welcome-next-button").click();
19+
await expect(
20+
page.getByRole("heading", { name: "Let's get started!" })
21+
).toBeVisible();
22+
await page.getByTestId("welcome-next-button").click();
23+
24+
await page
25+
.getByLabel("Email address")
26+
.fill(process.env.TWO_WEEKS_READY_E2E_TEST_USERNAME);
27+
await page
28+
.getByLabel("Password")
29+
.fill(process.env.TWO_WEEKS_READY_E2E_TEST_PASSWORD);
30+
await Promise.all([
31+
page.waitForNavigation({ url: "http://localhost:8080/prepare" }),
32+
page.locator('button[name="action"]').click()
33+
]);
34+
35+
// Save signed-in state to 'storageState.json'.
36+
await page
37+
.context()
38+
.storageState({ path: "./tests/_storagestate/storageState.json" });
39+
await browser.close();
40+
};

2wr-app/tests/hazard-info.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test("Navigate to Hazard Info shows Earthquake Harzard info", async ({
4+
page
5+
}) => {
6+
await page.goto("http://localhost:8080/");
7+
8+
await page
9+
.getByRole("link", {
10+
name: "Learn Your Hazards Learn all about common hazards."
11+
})
12+
.click();
13+
14+
await page.getByText("Earthquake").click();
15+
await page.getByRole("button", { name: "Before" }).click();
16+
await expect(
17+
page.getByText("Practice how to drop, cover, and hold on")
18+
).toBeVisible();
19+
await page
20+
.getByRole("banner")
21+
.getByRole("button")
22+
.click();
23+
await expect(
24+
page.getByText("Practice how to drop, cover, and hold on")
25+
).not.toBeVisible();
26+
});

2wr-app/tests/welcome.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { expect, test, chromium } = require("@playwright/test");
22
const { injectAxe, checkA11y } = require("axe-playwright");
33

4+
test.use({ storageState: "./tests/_storagestate/newuser-storageState.json" });
5+
46
test.describe("Welcome page accessibility test", () => {
57
let browser;
68
let page;

0 commit comments

Comments
 (0)