Skip to content

Commit 7ea6d22

Browse files
committed
refactor: add login to config.js for e2e tests
1 parent 5258670 commit 7ea6d22

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

test/config.ts

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,64 @@
1-
import { ChromiumEnv, FirefoxEnv, WebKitEnv, test, setConfig, PlaywrightOptions } from "@playwright/test"
1+
import {
2+
ChromiumEnv,
3+
FirefoxEnv,
4+
WebKitEnv,
5+
test,
6+
setConfig,
7+
PlaywrightOptions,
8+
Config,
9+
globalSetup,
10+
} from "@playwright/test"
11+
import * as crypto from "crypto"
12+
import path from "path"
13+
import { PASSWORD } from "./utils/constants"
14+
import * as wtfnode from "./utils/wtfnode"
215

3-
setConfig({
4-
testDir: "e2e", // Search for tests in this directory.
5-
timeout: 30000, // Each test is given 30 seconds.
16+
// Playwright doesn't like that ../src/node/util has an enum in it
17+
// so I had to copy hash in separately
18+
const hash = (str: string): string => {
19+
return crypto.createHash("sha256").update(str).digest("hex")
20+
}
21+
22+
const cookieToStore = {
23+
sameSite: "Lax" as const,
24+
name: "key",
25+
value: hash(PASSWORD),
26+
domain: "localhost",
27+
path: "/",
28+
expires: -1,
29+
httpOnly: false,
30+
secure: false,
31+
}
32+
33+
globalSetup(async () => {
34+
console.log("\n🚨 Running Global Setup for Jest End-to-End Tests")
35+
console.log("👋 Please hang tight...")
36+
37+
if (process.env.WTF_NODE) {
38+
wtfnode.setup()
39+
}
40+
41+
const storage = {
42+
cookies: [cookieToStore],
43+
}
44+
45+
// Save storage state and store as an env variable
46+
// More info: https://playwright.dev/docs/auth?_highlight=authe#reuse-authentication-state
47+
process.env.STORAGE = JSON.stringify(storage)
48+
console.log("✅ Global Setup for Jest End-to-End Tests is now complete.")
649
})
750

51+
const config: Config = {
52+
testDir: path.join(__dirname, "e2e"), // Search for tests in this directory.
53+
timeout: 30000, // Each test is given 30 seconds.
54+
}
55+
56+
if (process.env.CI) {
57+
config.retries = 2 // Retry failing tests 2 times
58+
}
59+
60+
setConfig(config)
61+
862
const options: PlaywrightOptions = {
963
headless: true, // Run tests in headless browsers.
1064
video: "retain-on-failure",

test/e2e/globalSetup.test.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants"
66
test.describe("globalSetup", () => {
77
// Create a new context with the saved storage state
88
// so we don't have to logged in
9-
const storageState = JSON.parse(STORAGE) || {}
10-
const options = {
11-
contextOptions: {
9+
const options: any = {}
10+
11+
// TODO@jsjoeio
12+
// Fix this once https://github.com/microsoft/playwright-test/issues/240
13+
// is fixed
14+
if (STORAGE) {
15+
const storageState = JSON.parse(STORAGE) || {}
16+
options.contextOptions = {
1217
storageState,
13-
},
18+
}
1419
}
1520
test("should keep us logged in using the storageState", options, async ({ page }) => {
1621
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })

0 commit comments

Comments
 (0)