flexysnap 0.5.1 → 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 +66 -34
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flexysnap",
3
- "version": "0.5.1",
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
@@ -6,29 +6,58 @@ async function callClosePopups(page) {
6
6
  await closePopups(page);
7
7
  }
8
8
 
9
- function logIfErrorResponse(response, url) {
9
+
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
+ }
10
19
  if (response && response.status() >= 400) {
11
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
12
34
  }
13
35
  }
14
36
 
15
- async function gotoWithRetry(page, url) {
16
- let response;
17
- try {
18
- logTimestamp('Loading ' + url);
19
- response = await page.goto(url, { timeout: 5000, waitUntil: "commit" });
20
- } catch (firstError) {
21
- logTimestamp(firstError);
22
- logTimestamp('Retry loading ' + url);
23
- response = await page.goto(url, { timeout: 5000, waitUntil: "commit" });
24
- }
25
- logIfErrorResponse(response, url);
26
- try {
27
- await waitForCompleteLoad(page);
28
- } catch(e) {
29
- logTimestamp(await page.pageErrors());
30
- throw e;
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;
46
+ let error = null;
47
+ for (let i = 1; i <= maxAttempts; ++i) {
48
+ error = null;
49
+ const timeout = baseTimeout * i;
50
+ try {
51
+ await attemptNavigation(page, url, timeout);
52
+ break
53
+ } catch (e) {
54
+ error = e
55
+ if (i < maxAttempts)
56
+ await prepareRetry(page, backoffMs * i);
57
+ }
31
58
  }
59
+ if (error)
60
+ throw error
32
61
  }
33
62
 
34
63
 
@@ -47,11 +76,26 @@ function setCacheableUrlPattern(pattern) {
47
76
  cacheableUrlPatterns.push(pattern)
48
77
  }
49
78
 
79
+ const cacheBustedResourceTypes = ['document', 'xhr', 'fetch'];
80
+
81
+ function isTestedSiteUrl(url) {
82
+ return baseUrl !== '' && url.href.includes(baseUrl);
83
+ }
84
+
50
85
  test.beforeEach(async ({ context }) => {
51
86
  startTimer();
52
87
  logTimestamp('Test execution started');
53
- 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 => {
54
93
  const req = route.request();
94
+
95
+ if (!cacheBustedResourceTypes.includes(req.resourceType())) {
96
+ return route.continue();
97
+ }
98
+
55
99
  const url = new URL(req.url());
56
100
  const path = url.pathname.toLowerCase();
57
101
 
@@ -60,7 +104,7 @@ test.beforeEach(async ({ context }) => {
60
104
  return route.continue();
61
105
  }
62
106
 
63
- if (req.method() !== 'GET' || path.includes('/wp-content/') || !url.href.includes(baseUrl)) {
107
+ if (req.method() !== 'GET') {
64
108
  return route.continue();
65
109
  }
66
110
  url.searchParams.set('_cb', Date.now().toString());
@@ -94,21 +138,9 @@ function logTimestamp(eventName) {
94
138
  return { currentTime, elapsedMs, elapsedSec };
95
139
  }
96
140
 
97
- async function waitForCompleteLoad(page) {
98
- try {
99
- await page.waitForLoadState('domcontentloaded', { timeout: 5000 });
100
- } catch (e) {
101
- logTimestamp(e);
102
- logTimestamp('Retry waiting for DOMContentLoaded event after 5 seconds');
103
- await page.waitForLoadState('domcontentloaded', { timeout: 5000 });
104
- }
105
- try {
106
- await page.waitForLoadState('load', { timeout: 5000 });
107
- } catch (e) {
108
- logTimestamp(e);
109
- logTimestamp('Retry waiting for load event after 5 seconds');
110
- await page.waitForLoadState('load', { timeout: 5000 });
111
- }
141
+ async function waitForCompleteLoad(page, timeout = 5000) {
142
+ await page.waitForLoadState('domcontentloaded', { timeout: timeout });
143
+ await page.waitForLoadState('load', { timeout: timeout });
112
144
  }
113
145
 
114
146