-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtest-utils.ts
More file actions
107 lines (100 loc) · 3.4 KB
/
Copy pathtest-utils.ts
File metadata and controls
107 lines (100 loc) · 3.4 KB
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
import { randomBytes } from 'crypto';
import { promises, writeFileSync } from 'fs';
import { join } from 'path';
import { test as baseTest, Page } from '@playwright/test';
const istanbulCLIOutput = join(process.cwd(), '.nyc_output');
const generateUUID = () => randomBytes(16).toString('hex');
/**
* Based on https://github.com/mxschmitt/playwright-test-coverage
*/
export const preFixturesTest = baseTest.extend({
context: async ({ context }, use) => {
await context.addInitScript(() => {
window.addEventListener('beforeunload', () =>
// eslint-disable-next-line
(window as any).collectIstanbulCoverage(
// eslint-disable-next-line
JSON.stringify((window as any).__coverage__),
),
);
});
await promises.mkdir(istanbulCLIOutput, { recursive: true });
await context.exposeFunction(
'collectIstanbulCoverage',
(coverageJSON: string) => {
if (coverageJSON)
writeFileSync(
join(
istanbulCLIOutput,
`playwright_coverage_${generateUUID()}.json`,
),
coverageJSON,
);
},
);
await use(context);
for (const page of context.pages()) {
await page.evaluate(() =>
// eslint-disable-next-line
(window as any).collectIstanbulCoverage(
// eslint-disable-next-line
JSON.stringify((window as any).__coverage__),
),
);
}
},
});
/**
* Playwright `test` extended to:
* - collect code coverage
* - dismiss the guide and notification popovers before each test
*/
export const test = preFixturesTest.extend({
page: async ({ page }, use) => {
/* Mocked to avoid flakiness from the logged 404 error */
await page.route('/_vercel/insights/script.js', async (route) => {
await route.fulfill({});
});
await page.goto('/');
await page.evaluate(
`window.localStorage.setItem('BITAUTH_IDE_GUIDE_POPOVER_DISMISSED', Date.now());
window.localStorage.setItem('BITAUTH_IDE_E2E_TESTING_DISABLE_NOTIFIER', 'true');`,
);
await use(page);
},
});
export const expect = test.expect;
/**
* Load a wallet template from tests/fixtures into the provided `page`.
*
* While we could use Playwright's `extend` behavior to add these fixtures to
* custom `Page`s, this strategy is less verbose and allows tests to use the
* typical `page` variable (less confusing for contributors and reviewers).
*/
export const loadTemplate = async (page: Page, path: string) => {
await page.goto('/');
await page
.getByRole('button', {
name: 'Import or Restore Template → Import or restore a template from a previous session.',
})
.click();
const fileChooserPromise = page.waitForEvent('filechooser');
await page.getByText('Load from file...').click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(path);
await page.getByRole('button', { name: 'Import Template' }).click();
};
/**
* Occasionally, visual difference testing will fail in webkit due to seemingly
* non-deterministic font kerning. This utility can be used to eliminate
* flakiness in tests prone to these failures.
*/
export const fixFontFlakiness = async (page: Page, enable = true) => {
if (!enable) return;
await page
.locator('body')
.evaluate(
(element) => (element.style.textRendering = 'geometricPrecision'),
);
await page.evaluate(() => document.fonts.ready);
};