@spscommerce/e2e-core 2.3.6 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Dockerfile +1 -1
- package/Example.test.ts +29 -0
- package/README.md +4 -2
- package/lib/globalSetup.js +43 -37
- package/lib/globalSetup.js.map +1 -1
- package/lib/matchersSetup.js +1 -1
- package/lib/matchersSetup.js.map +1 -1
- package/package.json +9 -9
- package/playwright.config.js +2 -2
- package/src/globalSetup.ts +43 -39
- package/src/matchersSetup.ts +1 -1
package/Dockerfile
CHANGED
package/Example.test.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { test, expect } from '@playwright/test';
|
|
2
|
+
import expectExt from './src/matchersSetup';
|
|
3
|
+
import { enableLogger, disableLogger } from './src/loggerSetup';
|
|
4
|
+
|
|
5
|
+
test.describe('SPS_Example', () => {
|
|
6
|
+
|
|
7
|
+
test.beforeEach(async ({ page }, testInfo) => {
|
|
8
|
+
process.env.CI && await enableLogger(testInfo.titlePath, page);
|
|
9
|
+
await page.goto(`https://test.commerce.spscommerce.com/fulfillment/`);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test.afterEach(async () => {
|
|
13
|
+
process.env.CI && await disableLogger();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('First_test', async ({ page }) => {
|
|
17
|
+
await page.waitForTimeout(5000);
|
|
18
|
+
expect(
|
|
19
|
+
true,
|
|
20
|
+
`Custom error message.`
|
|
21
|
+
).toBeTruthy();
|
|
22
|
+
expectExt(
|
|
23
|
+
[1, 2, 3, 4],
|
|
24
|
+
// false,
|
|
25
|
+
`Custom error message.`
|
|
26
|
+
).toIncludeAllMembers([1, 2, 3]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
});
|
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ export USER_PASSWORD="EXAMPLE_PASSWORD"
|
|
|
20
20
|
5. Create .env file in the tests-e2e/ directory with an appropriate process.env variables listed below in the table. E.g.:
|
|
21
21
|
```
|
|
22
22
|
TEST_TIMEOUT=75000
|
|
23
|
+
EXPECT_TIMEOUT=10000
|
|
23
24
|
ACTION_TIMEOUT=45000
|
|
24
25
|
WORKERS=6
|
|
25
26
|
REPO_NAME=fulfillment-ui-v2
|
|
@@ -31,6 +32,7 @@ VIEWPORT={"width": 1920, "height": 1080}
|
|
|
31
32
|
| BROWSER | use + as a separator, e.g edge+firefox | chromium | chromium, firefox, webkit, edge |
|
|
32
33
|
| ENV | auth state is invoked for the defined env | test | test, prod |
|
|
33
34
|
| TEST_TIMEOUT | global timeout for each test | 30000 | [number_in_ms] |
|
|
35
|
+
| EXPECT_TIMEOUT | global timeout for each expect | 5000 | [number_in_ms] |
|
|
34
36
|
| ACTION_TIMEOUT | global timeout for each PW action, like click() | 30000 | [number_in_ms] |
|
|
35
37
|
| SLOWMO | slows down the actions by the specified ms | 0 | [number_in_ms] |
|
|
36
38
|
| WORKERS | number of workers (test threads) | 1 | [number] |
|
|
@@ -99,7 +101,7 @@ You can observe the PW trace file in 3 ways:
|
|
|
99
101
|
## Dockerfile
|
|
100
102
|
PW provides an official Docker image. These image includes all the dependencies needed to run browsers in a Docker container, and also include the browsers themselves. Here is an example of the Dockerfile:
|
|
101
103
|
```
|
|
102
|
-
FROM mcr.microsoft.com/playwright:v1.
|
|
104
|
+
FROM mcr.microsoft.com/playwright:v1.28.0-focal
|
|
103
105
|
|
|
104
106
|
ARG BROWSER
|
|
105
107
|
|
|
@@ -184,11 +186,11 @@ Additional info over this framework could be found on [Confluence](https://atlas
|
|
|
184
186
|
Put in a PR against this repo if you fix a bug or come up with an improvement!
|
|
185
187
|
|
|
186
188
|
## Used tools
|
|
187
|
-
- [@alex_neo/jest-expect-message](https://github.com/alexneo2003/jest-expect-message) - gives ability to add a custom error message to the assertions. We use this forked lib since there is a [bug](https://github.com/mattphillips/jest-expect-message/issues/39) in the original one.
|
|
188
189
|
- [@playwright/test](https://github.com/microsoft/playwright) - browser automation and test runner tool
|
|
189
190
|
- [@typescript-eslint/eslint-plugin](https://github.com/typescript-eslint/typescript-eslint) - ESLint plugin which provides lint rules for TypeScript codebases
|
|
190
191
|
- [@typescript-eslint/parser](https://github.com/typescript-eslint/typescript-eslint) - ESLint parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code
|
|
191
192
|
- [dotenv](https://github.com/motdotla/dotenv) - loads environment variables from a .env file into process.env
|
|
192
193
|
- [eslint](https://github.com/eslint/eslint) - static code analysis tool
|
|
194
|
+
- [jest-expect-message](https://github.com/mattphillips/jest-expect-message) - gives ability to add a custom error message to the assertions
|
|
193
195
|
- [jest-extended](https://github.com/jest-community/jest-extended) - adds additional matchers to default ones making it easy to test everything 🙌
|
|
194
196
|
- [typescript](https://github.com/Microsoft/TypeScript) - language for application-scale JavaScript that adds optional types
|
package/lib/globalSetup.js
CHANGED
|
@@ -29,17 +29,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
29
29
|
const fs_1 = __importDefault(require("fs"));
|
|
30
30
|
const path_1 = __importDefault(require("path"));
|
|
31
31
|
const test_1 = require("@playwright/test");
|
|
32
|
-
const domain = 'commerce.spscommerce.com';
|
|
33
|
-
let baseUrl = 'https://test.' + domain;
|
|
34
|
-
let storageFile = path_1.default.join(process.cwd(), `results-e2e/auth/storageStateTest.json`);
|
|
35
|
-
let traceFile = path_1.default.join(process.cwd(), 'results-e2e/auth/globalSetupTest.zip');
|
|
36
|
-
if (process.env.ENV && process.env.ENV.includes('prod')) {
|
|
37
|
-
baseUrl = 'https://' + domain;
|
|
38
|
-
storageFile = path_1.default.join(process.cwd(), `results-e2e/auth/storageStateProd.json`);
|
|
39
|
-
traceFile = path_1.default.join(process.cwd(), 'results-e2e/auth/globalSetupProd.zip');
|
|
40
|
-
}
|
|
41
32
|
async function globalSetup() {
|
|
42
|
-
// Remove custom sub-folders for test artifacts
|
|
33
|
+
// Remove custom sub-folders for test artifacts except auth folder
|
|
43
34
|
const resultsFolder = path_1.default.join(process.cwd(), 'results-e2e/');
|
|
44
35
|
if (fs_1.default.existsSync(resultsFolder)) {
|
|
45
36
|
for (const subFolder of fs_1.default.readdirSync(resultsFolder)) {
|
|
@@ -48,36 +39,51 @@ async function globalSetup() {
|
|
|
48
39
|
}
|
|
49
40
|
}
|
|
50
41
|
}
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
await page.goto(baseUrl);
|
|
62
|
-
await page.waitForURL(/https:\/\/auth.*/);
|
|
63
|
-
await page.fill('input[type="email"]', process.env.USER_EMAIL);
|
|
64
|
-
await page.click('button#btn-next');
|
|
65
|
-
await page.fill('input[type="password"]', process.env.USER_PASSWORD);
|
|
66
|
-
await page.click('button#btn-login');
|
|
67
|
-
await page.waitForURL(/^((?!login).)*$/);
|
|
68
|
-
if (page.url().includes('select-org')) {
|
|
69
|
-
await page.click('button[type="submit"]');
|
|
70
|
-
}
|
|
71
|
-
await page.waitForURL(/home/);
|
|
72
|
-
await page.context().storageState({ path: storageFile });
|
|
42
|
+
// Invoke auth state if needed
|
|
43
|
+
if (process.env.SKIP_AUTH !== 'true') {
|
|
44
|
+
const domain = 'commerce.spscommerce.com';
|
|
45
|
+
let baseUrl = 'https://test.' + domain;
|
|
46
|
+
let storageFile = path_1.default.join(process.cwd(), `results-e2e/auth/storageStateTest.json`);
|
|
47
|
+
let traceFile = path_1.default.join(process.cwd(), 'results-e2e/auth/globalSetupTest.zip');
|
|
48
|
+
if (process.env.ENV && process.env.ENV.includes('prod')) {
|
|
49
|
+
baseUrl = 'https://' + domain;
|
|
50
|
+
storageFile = path_1.default.join(process.cwd(), `results-e2e/auth/storageStateProd.json`);
|
|
51
|
+
traceFile = path_1.default.join(process.cwd(), 'results-e2e/auth/globalSetupProd.zip');
|
|
73
52
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
53
|
+
// Launch browser and log in if storageState.json does not exist OR auth expires soon
|
|
54
|
+
const currentTS = new Date().getTime();
|
|
55
|
+
if (!fs_1.default.existsSync(storageFile) ||
|
|
56
|
+
(fs_1.default.existsSync(storageFile) &&
|
|
57
|
+
((JSON.parse(fs_1.default.readFileSync(storageFile, 'utf8')).origins[0].localStorage.find(o => o.name === 'expires_at').value - currentTS) / 60000) < 30)) {
|
|
58
|
+
const browser = await test_1.chromium.launch({ headless: false }); // { headless: false }
|
|
59
|
+
const context = await browser.newContext();
|
|
60
|
+
await context.tracing.start({ screenshots: true, snapshots: true });
|
|
61
|
+
const page = await context.newPage();
|
|
62
|
+
try {
|
|
63
|
+
await page.goto(baseUrl);
|
|
64
|
+
await page.waitForURL(/https:\/\/auth.*/);
|
|
65
|
+
await page.fill('input[type="email"]', process.env.USER_EMAIL);
|
|
66
|
+
await page.click('button#btn-next');
|
|
67
|
+
await page.fill('input[type="password"]', process.env.USER_PASSWORD);
|
|
68
|
+
await page.click('button#btn-login');
|
|
69
|
+
await page.waitForURL(/^((?!login).)*$/);
|
|
70
|
+
if (page.url().includes('select-org')) {
|
|
71
|
+
await page.click('button[type="submit"]');
|
|
72
|
+
}
|
|
73
|
+
await page.waitForURL(/home/);
|
|
74
|
+
await page.context().storageState({ path: storageFile });
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
console.log(e);
|
|
78
|
+
await context.tracing.stop({ path: traceFile });
|
|
79
|
+
throw new Error('Error occurred while running globalSetup.ts file.');
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
await browser.close();
|
|
83
|
+
}
|
|
78
84
|
}
|
|
79
|
-
await browser.close();
|
|
80
85
|
}
|
|
86
|
+
// Set something up once before running all tests
|
|
81
87
|
if (process.env.SETUP_PATH) {
|
|
82
88
|
if (fs_1.default.existsSync(process.env.SETUP_PATH)) {
|
|
83
89
|
await Promise.resolve().then(() => __importStar(require(process.env.SETUP_PATH)));
|
package/lib/globalSetup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"globalSetup.js","sourceRoot":"","sources":["../src/globalSetup.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,2CAA4C;
|
|
1
|
+
{"version":3,"file":"globalSetup.js","sourceRoot":"","sources":["../src/globalSetup.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,2CAA4C;AAE7B,KAAK,UAAU,WAAW;IACrC,kEAAkE;IAClE,MAAM,aAAa,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,cAAc,CAAC,CAAC;IAC/D,IAAI,YAAE,CAAC,UAAU,CAAC,aAAa,CAAC,EAAE;QAC9B,KAAK,MAAM,SAAS,IAAI,YAAE,CAAC,WAAW,CAAC,aAAa,CAAC,EAAE;YACnD,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE;gBAAE,YAAE,CAAC,MAAM,CAAC,aAAa,GAAG,SAAS,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;aAAE;SAC/G;KACJ;IACD,8BAA8B;IAC9B,IAAI,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,MAAM,EAAE;QAClC,MAAM,MAAM,GAAG,0BAA0B,CAAC;QAC1C,IAAI,OAAO,GAAG,eAAe,GAAG,MAAM,CAAC;QACvC,IAAI,WAAW,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,wCAAwC,CAAC,CAAC;QACrF,IAAI,SAAS,GAAG,cAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,sCAAsC,CAAC,CAAC;QAEjF,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,IA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AAC,UAAU,aAAa,CAAC,CAAC;SAC3D;KACJ;IACD,OAAO;AACX,CAAC;AA7DD,8BA6DC"}
|
package/lib/matchersSetup.js
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const test_1 = require("@playwright/test");
|
|
7
|
-
const withMessage_js_1 = __importDefault(require("
|
|
7
|
+
const withMessage_js_1 = __importDefault(require("jest-expect-message/dist/withMessage.js"));
|
|
8
8
|
const expect = (0, withMessage_js_1.default)(test_1.test.expect);
|
|
9
9
|
const matchers = require('jest-extended');
|
|
10
10
|
expect.extend(matchers);
|
package/lib/matchersSetup.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"matchersSetup.js","sourceRoot":"","sources":["../src/matchersSetup.ts"],"names":[],"mappings":";;;;;AAAA,2CAAwC;AACxC,
|
|
1
|
+
{"version":3,"file":"matchersSetup.js","sourceRoot":"","sources":["../src/matchersSetup.ts"],"names":[],"mappings":";;;;;AAAA,2CAAwC;AACxC,6FAAkE;AAElE,MAAM,MAAM,GAAG,IAAA,wBAAW,EAAC,WAAI,CAAC,MAAM,CAAC,CAAC;AAExC,MAAM,QAAQ,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;AAC1C,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAExB,kBAAe,MAAM,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spscommerce/e2e-core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "An automation test framework which is based on Playwright.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
"author": "SPS Commerce",
|
|
25
25
|
"license": "UNLICENSED",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@
|
|
28
|
-
"@
|
|
29
|
-
"@typescript-eslint/
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"jest-extended": "
|
|
34
|
-
"typescript": "
|
|
27
|
+
"@playwright/test": "1.28.0",
|
|
28
|
+
"@typescript-eslint/eslint-plugin": "5.30.0",
|
|
29
|
+
"@typescript-eslint/parser": "5.30.0",
|
|
30
|
+
"dotenv": "16.0.3",
|
|
31
|
+
"eslint": "7.32.0",
|
|
32
|
+
"jest-expect-message": "1.0.4",
|
|
33
|
+
"jest-extended": "1.2.0",
|
|
34
|
+
"typescript": "4.7.4"
|
|
35
35
|
}
|
|
36
36
|
}
|
package/playwright.config.js
CHANGED
|
@@ -93,7 +93,7 @@ const config = {
|
|
|
93
93
|
timeout: +process.env.TEST_TIMEOUT || 30000,
|
|
94
94
|
/* Maximum time expect() should wait for the condition to be met */
|
|
95
95
|
expect: {
|
|
96
|
-
timeout: 5000
|
|
96
|
+
timeout: +process.env.EXPECT_TIMEOUT || 5000,
|
|
97
97
|
},
|
|
98
98
|
/* Fail the build on CI if you accidentally left test.only in the source code */
|
|
99
99
|
forbidOnly: !!process.env.CI,
|
|
@@ -114,7 +114,7 @@ const config = {
|
|
|
114
114
|
globalTeardown: require.resolve('./lib/globalTeardown'),
|
|
115
115
|
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions */
|
|
116
116
|
use: {
|
|
117
|
-
storageState:
|
|
117
|
+
storageState: !process.env.SKIP_AUTH ? storageStatePath : undefined,
|
|
118
118
|
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit) */
|
|
119
119
|
actionTimeout: +process.env.ACTION_TIMEOUT || 30000,
|
|
120
120
|
/* Collect screenshot after each test failure. See https://playwright.dev/docs/test-configuration#automatic-screenshots */
|
package/src/globalSetup.ts
CHANGED
|
@@ -2,55 +2,59 @@ import fs from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { chromium } from '@playwright/test';
|
|
4
4
|
|
|
5
|
-
const domain = 'commerce.spscommerce.com';
|
|
6
|
-
let baseUrl = 'https://test.' + domain;
|
|
7
|
-
let storageFile = path.join(process.cwd(), `results-e2e/auth/storageStateTest.json`);
|
|
8
|
-
let traceFile = path.join(process.cwd(), 'results-e2e/auth/globalSetupTest.zip');
|
|
9
|
-
|
|
10
|
-
if (process.env.ENV && process.env.ENV.includes('prod')) {
|
|
11
|
-
baseUrl = 'https://' + domain;
|
|
12
|
-
storageFile = path.join(process.cwd(), `results-e2e/auth/storageStateProd.json`);
|
|
13
|
-
traceFile = path.join(process.cwd(), 'results-e2e/auth/globalSetupProd.zip');
|
|
14
|
-
}
|
|
15
|
-
|
|
16
5
|
export default async function globalSetup(): Promise<void> {
|
|
17
|
-
// Remove custom sub-folders for test artifacts
|
|
6
|
+
// Remove custom sub-folders for test artifacts except auth folder
|
|
18
7
|
const resultsFolder = path.join(process.cwd(), 'results-e2e/');
|
|
19
8
|
if (fs.existsSync(resultsFolder)) {
|
|
20
9
|
for (const subFolder of fs.readdirSync(resultsFolder)) {
|
|
21
10
|
if (!subFolder.includes('auth')) { fs.rmSync(resultsFolder + subFolder, { recursive: true, force: true }); }
|
|
22
11
|
}
|
|
23
12
|
}
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
13
|
+
// Invoke auth state if needed
|
|
14
|
+
if (process.env.SKIP_AUTH !== 'true') {
|
|
15
|
+
const domain = 'commerce.spscommerce.com';
|
|
16
|
+
let baseUrl = 'https://test.' + domain;
|
|
17
|
+
let storageFile = path.join(process.cwd(), `results-e2e/auth/storageStateTest.json`);
|
|
18
|
+
let traceFile = path.join(process.cwd(), 'results-e2e/auth/globalSetupTest.zip');
|
|
19
|
+
|
|
20
|
+
if (process.env.ENV && process.env.ENV.includes('prod')) {
|
|
21
|
+
baseUrl = 'https://' + domain;
|
|
22
|
+
storageFile = path.join(process.cwd(), `results-e2e/auth/storageStateProd.json`);
|
|
23
|
+
traceFile = path.join(process.cwd(), 'results-e2e/auth/globalSetupProd.zip');
|
|
24
|
+
}
|
|
25
|
+
// Launch browser and log in if storageState.json does not exist OR auth expires soon
|
|
26
|
+
const currentTS = new Date().getTime();
|
|
27
|
+
if (!fs.existsSync(storageFile) ||
|
|
28
|
+
(fs.existsSync(storageFile) &&
|
|
29
|
+
((JSON.parse(fs.readFileSync(storageFile, 'utf8')).origins[0].localStorage.find(o => o.name === 'expires_at').value - currentTS) / 60000) < 30)
|
|
30
|
+
) {
|
|
31
|
+
const browser = await chromium.launch({ headless: false }); // { headless: false }
|
|
32
|
+
const context = await browser.newContext();
|
|
33
|
+
await context.tracing.start({ screenshots: true, snapshots: true });
|
|
34
|
+
const page = await context.newPage();
|
|
35
|
+
try {
|
|
36
|
+
await page.goto(baseUrl);
|
|
37
|
+
await page.waitForURL(/https:\/\/auth.*/);
|
|
38
|
+
await page.fill('input[type="email"]', process.env.USER_EMAIL);
|
|
39
|
+
await page.click('button#btn-next');
|
|
40
|
+
await page.fill('input[type="password"]', process.env.USER_PASSWORD);
|
|
41
|
+
await page.click('button#btn-login');
|
|
42
|
+
await page.waitForURL(/^((?!login).)*$/);
|
|
43
|
+
if (page.url().includes('select-org')) {
|
|
44
|
+
await page.click('button[type="submit"]');
|
|
45
|
+
}
|
|
46
|
+
await page.waitForURL(/home/);
|
|
47
|
+
await page.context().storageState({ path: storageFile });
|
|
48
|
+
} catch (e) {
|
|
49
|
+
console.log(e);
|
|
50
|
+
await context.tracing.stop({ path: traceFile });
|
|
51
|
+
throw new Error('Error occurred while running globalSetup.ts file.');
|
|
52
|
+
} finally {
|
|
53
|
+
await browser.close();
|
|
44
54
|
}
|
|
45
|
-
await page.waitForURL(/home/);
|
|
46
|
-
await page.context().storageState({ path: storageFile });
|
|
47
|
-
} catch (e) {
|
|
48
|
-
console.log(e);
|
|
49
|
-
await context.tracing.stop({ path: traceFile });
|
|
50
|
-
throw new Error('Error occurred while running globalSetup.ts file.');
|
|
51
55
|
}
|
|
52
|
-
await browser.close();
|
|
53
56
|
}
|
|
57
|
+
// Set something up once before running all tests
|
|
54
58
|
if (process.env.SETUP_PATH) {
|
|
55
59
|
if (fs.existsSync(process.env.SETUP_PATH)) {
|
|
56
60
|
await import(process.env.SETUP_PATH);
|
package/src/matchersSetup.ts
CHANGED