forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaywright.config.ts
139 lines (120 loc) · 3.9 KB
/
playwright.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// NOTE: load env files before other imports
// eslint-disable-next-line import/order
import {SANITY_E2E_SESSION_TOKEN} from './test/e2e/env'
import os from 'os'
import path from 'path'
import {defineConfig, devices} from '@playwright/test'
// Paths
const TESTS_PATH = path.join(__dirname, 'test', 'e2e', 'tests')
const HTML_REPORT_PATH = path.join(__dirname, 'test', 'e2e', 'report')
const ARTIFACT_OUTPUT_PATH = path.join(__dirname, 'test', 'e2e', 'results')
// OS-specific browsers to include
const OS_BROWSERS =
os.platform() === 'darwin' ? [{name: 'webkit', use: {...devices['Desktop Safari']}}] : []
// Read environment variables
const CI = readBoolEnv('CI', false)
const E2E_DEBUG = readBoolEnv('SANITY_E2E_DEBUG', false)
const PROJECT_ID = 'ppsg7ml5'
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
globalSetup: require.resolve('./test/e2e/globalSetup'),
testDir: TESTS_PATH,
/* Maximum time one test can run for. */
timeout: 30 * 1000,
retries: 1,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 1000 * 60 * 5,
},
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: CI
? [['github'], ['html', {outputFolder: HTML_REPORT_PATH}]]
: [['list'], ['html', {outputFolder: HTML_REPORT_PATH}]],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 10000,
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
baseURL: 'http://localhost:3333/',
headless: readBoolEnv('SANITY_E2E_HEADLESS', !E2E_DEBUG),
storageState: getStorageStateForProjectId(PROJECT_ID),
viewport: {width: 1728, height: 1000},
contextOptions: {reducedMotion: 'reduce'},
video: {mode: 'on-first-retry'},
},
/* Configure projects for major browsers */
projects: E2E_DEBUG
? [
{
name: 'chromium',
use: {...devices['Desktop Chrome']},
},
]
: [
{
name: 'chromium',
use: {...devices['Desktop Chrome']},
},
{
name: 'firefox',
use: {...devices['Desktop Firefox']},
},
...OS_BROWSERS,
],
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
outputDir: ARTIFACT_OUTPUT_PATH,
/* Run your local dev server before starting the tests */
webServer: {
command: 'yarn dev',
port: 3333,
reuseExistingServer: !CI,
},
})
/**
* Read an environment variable, parsing the response as a boolean, using loose
* constraints (`true`, `1`, `yes` are all considered true, everything else is false)
*
* @param flag - The environment variable to read, eg `SOME_FLAG`
* @param defaultValue - The default value to use if it is not set
* @returns A boolean value
* @internal
*/
function readBoolEnv(flag: string, defaultValue: boolean) {
const value = process.env[flag]
if (value === undefined) {
return defaultValue
}
return value === 'true' || value === '1' || value === 'yes'
}
/**
* Returns a storage state with an auth token injected to the localstorage for the given project ID
*
* @param projectId - The ID of the project the auth token belongs to
* @returns A storage state object
* @internal
*/
function getStorageStateForProjectId(projectId: string) {
return {
cookies: [],
origins: [
{
origin: 'http://localhost:3333',
localStorage: [
{
name: `__studio_auth_token_${projectId}`,
value: JSON.stringify({
token: SANITY_E2E_SESSION_TOKEN,
time: new Date().toISOString(),
}),
},
],
},
],
}
}