flexysnap 0.5.2 → 0.5.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/testUtils.js +59 -30
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexysnap",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "description": "Flexible visual snapshot testing for Playwright, built for e-commerce.",
5
5
  "keywords": [
6
6
  "playwright",
package/src/testUtils.js CHANGED
@@ -7,40 +7,54 @@ async function callClosePopups(page) {
7
7
  }
8
8
 
9
9
 
10
- async function gotoWithRetry(page, url) {
10
+ async function attemptNavigation(page, url, timeout) {
11
+ logTimestamp('Loading ' + url);
12
+ let response = null;
13
+ try {
14
+ response = await page.goto(url, {timeout: timeout, waitUntil: "commit"});
15
+ } catch (e) {
16
+ logTimestamp('Error navigating to page');
17
+ throw e
18
+ }
19
+ if (response && response.status() >= 400) {
20
+ logTimestamp(`HTTP error ${response.status()} ${response.statusText()} while loading ${url}`);
21
+ throw new Error('Could not load page.')
22
+ }
23
+ try {
24
+ await page.waitForLoadState('domcontentloaded', {timeout: timeout});
25
+ } catch (e) {
26
+ logTimestamp('Error while waiting for DOMContentLoaded event');
27
+ throw e
28
+ }
29
+ try {
30
+ await page.waitForLoadState('load', {timeout: timeout});
31
+ } catch (e) {
32
+ logTimestamp('Error while waiting for load event');
33
+ throw e
34
+ }
35
+ }
36
+
37
+ async function prepareRetry(page, waitMs) {
38
+ await page.goto('about:blank', { timeout: 5000 }).catch(() => {});
39
+ logTimestamp(`Waiting ${waitMs}ms before retrying`);
40
+ await page.waitForTimeout(waitMs);
41
+ }
42
+
43
+ async function gotoWithRetry(page, url, options = {}) {
44
+ const { baseTimeout = 20000, backoffMs = 3000 } = options;
45
+ const maxAttempts = 3;
11
46
  let error = null;
12
- for (let i = 1; i <= 3; ++i) {
47
+ for (let i = 1; i <= maxAttempts; ++i) {
13
48
  error = null;
14
- let timeout = i * 5000;
15
- logTimestamp('Loading ' + url);
16
- let response = null;
17
- try {
18
- response = await page.goto(url, {timeout: timeout, waitUntil: "commit"});
19
- } catch (e) {
20
- logTimestamp('Error navigating to page');
21
- error = e
22
- continue
23
- }
24
- if (response && response.status() >= 400) {
25
- logTimestamp(`HTTP error ${response.status()} ${response.statusText()} while loading ${url}`);
26
- error = new Error('Could not load page.')
27
- continue
28
- }
49
+ const timeout = baseTimeout * i;
29
50
  try {
30
- await page.waitForLoadState('domcontentloaded', {timeout: timeout});
51
+ await attemptNavigation(page, url, timeout);
52
+ break
31
53
  } catch (e) {
32
- logTimestamp('Error while waiting for DOMContentLoaded event');
33
54
  error = e
34
- continue
55
+ if (i < maxAttempts)
56
+ await prepareRetry(page, backoffMs * i);
35
57
  }
36
- try {
37
- await page.waitForLoadState('load', {timeout: timeout});
38
- } catch (e) {
39
- logTimestamp('Error while waiting for load event');
40
- error = e
41
- continue
42
- }
43
- break
44
58
  }
45
59
  if (error)
46
60
  throw error
@@ -62,11 +76,26 @@ function setCacheableUrlPattern(pattern) {
62
76
  cacheableUrlPatterns.push(pattern)
63
77
  }
64
78
 
79
+ const cacheBustedResourceTypes = ['document', 'xhr', 'fetch'];
80
+
81
+ function isTestedSiteUrl(url) {
82
+ return baseUrl !== '' && url.href.includes(baseUrl);
83
+ }
84
+
65
85
  test.beforeEach(async ({ context }) => {
66
86
  startTimer();
67
87
  logTimestamp('Test execution started');
68
- await context.route('**/*', route => {
88
+ context.on('requestfailed', request => {
89
+ const errorText = request.failure()?.errorText;
90
+ logTimestamp(`Request failed: ${request.method()} ${request.url()} - ${errorText}`);
91
+ });
92
+ await context.route(isTestedSiteUrl, route => {
69
93
  const req = route.request();
94
+
95
+ if (!cacheBustedResourceTypes.includes(req.resourceType())) {
96
+ return route.continue();
97
+ }
98
+
70
99
  const url = new URL(req.url());
71
100
  const path = url.pathname.toLowerCase();
72
101
 
@@ -75,7 +104,7 @@ test.beforeEach(async ({ context }) => {
75
104
  return route.continue();
76
105
  }
77
106
 
78
- if (req.method() !== 'GET' || path.includes('/wp-content/') || !url.href.includes(baseUrl)) {
107
+ if (req.method() !== 'GET') {
79
108
  return route.continue();
80
109
  }
81
110
  url.searchParams.set('_cb', Date.now().toString());