Skip to content

Commit 93d806c

Browse files
committed
feat: retry added by default
2 parents bb98823 + bd78feb commit 93d806c

File tree

6 files changed

+1908
-3654
lines changed

6 files changed

+1908
-3654
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const config: Config = {
5454
ciBuildId: "SOME_UNIQUE_ID",
5555
};
5656

57-
const browserName = chromium.name();
57+
const browserName = chromium.name();
5858
const vrt = new PlaywrightVisualRegressionTracker(browserName, config);
5959
```
6060

@@ -121,6 +121,7 @@ await vrt.trackPage(page, imageName[, options])
121121
- `imageName` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> name for the taken screenshot image
122122
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> optional configuration with:
123123
- - `diffTollerancePercent` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> specify acceptable difference from baseline, between `0-100`.
124+
- - `comment` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> comment for test run
124125
- - `ignoreAreas` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>>
125126
- - - `x` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> X-coordinate relative of left upper corner
126127
- - - `y` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Y-coordinate relative of left upper corner
@@ -139,6 +140,7 @@ await vrt.trackPage(page, imageName[, options])
139140
- - - `os` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> operating system name, like Windows, Mac, etc.
140141
- - - `device` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> device name, PC identifier, mobile identifier etc.
141142
- - - `viewport` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> viewport size.
143+
- `retryCount` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Maximum time to retry screenshot if case of diff
142144

143145
### Track elementHandle
144146

@@ -150,6 +152,7 @@ await vrt.trackElementHandle(elementHandle, imageName[, options])
150152
- `imageName` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> name for the taken screenshot image
151153
- `options` <[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)> optional configuration with:
152154
- - `diffTollerancePercent` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> specify acceptable difference from baseline, between `0-100`.
155+
- - `comment` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> comment for test run
153156
- - `ignoreAreas` <[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)>>
154157
- - - `x` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> X-coordinate relative of left upper corner
155158
- - - `y` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Y-coordinate relative of left upper corner
@@ -162,3 +165,4 @@ await vrt.trackElementHandle(elementHandle, imageName[, options])
162165
- - - `os` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> operating system name, like Windows, Mac, etc.
163166
- - - `device` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> device name, PC identifier, mobile identifier etc.
164167
- - - `viewport` <[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type)> viewport size.
168+
- `retryCount` <[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type)> Maximum time to retry screenshot if case of diff

lib/playwright.interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ interface BaseTrackOptions {
6666
diffTollerancePercent?: number;
6767
ignoreAreas?: IgnoreArea[];
6868
agent?: Agent;
69+
comment?: string;
6970
}
7071

7172
export interface PageTrackOptions extends BaseTrackOptions {

lib/playwrightVisualRegressionTracker.spec.ts

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe("playwright", () => {
7979
it("track all fields", async () => {
8080
const imageName = "test name";
8181
const trackOptions: PageTrackOptions = {
82+
comment: "Test comment",
8283
diffTollerancePercent: 12.31,
8384
ignoreAreas: [
8485
{
@@ -109,16 +110,20 @@ describe("playwright", () => {
109110
expect(pageMocked.screenshot).toHaveBeenCalledWith(
110111
trackOptions.screenshotOptions
111112
);
112-
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
113-
name: imageName,
114-
imageBase64: screenshot.toString("base64"),
115-
browser: browserName,
116-
viewport: `1800x1600`,
117-
os: trackOptions.agent?.os,
118-
device: trackOptions.agent?.device,
119-
diffTollerancePercent: trackOptions.diffTollerancePercent,
120-
ignoreAreas: trackOptions.ignoreAreas,
121-
});
113+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith(
114+
{
115+
name: imageName,
116+
imageBase64: screenshot.toString("base64"),
117+
browser: browserName,
118+
viewport: `1800x1600`,
119+
os: trackOptions.agent?.os,
120+
device: trackOptions.agent?.device,
121+
diffTollerancePercent: trackOptions.diffTollerancePercent,
122+
ignoreAreas: trackOptions.ignoreAreas,
123+
comment: trackOptions.comment,
124+
},
125+
2
126+
);
122127
});
123128

124129
it("track default fields", async () => {
@@ -128,15 +133,19 @@ describe("playwright", () => {
128133
await playwrightVrt.trackPage(page, imageName);
129134

130135
expect(pageMocked.screenshot).toHaveBeenCalledWith(undefined);
131-
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
132-
name: imageName,
133-
imageBase64: screenshot.toString("base64"),
134-
browser: browserName,
135-
viewport: undefined,
136-
os: undefined,
137-
device: undefined,
138-
diffTollerancePercent: undefined,
139-
});
136+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith(
137+
{
138+
name: imageName,
139+
imageBase64: screenshot.toString("base64"),
140+
browser: browserName,
141+
viewport: undefined,
142+
os: undefined,
143+
device: undefined,
144+
diffTollerancePercent: undefined,
145+
comment: undefined,
146+
},
147+
2
148+
);
140149
});
141150
});
142151

@@ -162,6 +171,7 @@ describe("playwright", () => {
162171
omitBackground: true,
163172
timeout: 12,
164173
},
174+
comment: "Test comment",
165175
};
166176
pageMocked.$ = jest.fn().mockResolvedValueOnce({});
167177
const elementHandle = await page.$("#test");
@@ -179,16 +189,20 @@ describe("playwright", () => {
179189
expect(elementHandleMocked!.screenshot).toHaveBeenCalledWith(
180190
trackOptions.screenshotOptions
181191
);
182-
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
183-
name: imageName,
184-
imageBase64: screenshot.toString("base64"),
185-
browser: browserName,
186-
viewport: trackOptions.agent?.viewport,
187-
os: trackOptions.agent?.os,
188-
device: trackOptions.agent?.device,
189-
diffTollerancePercent: trackOptions.diffTollerancePercent,
190-
ignoreAreas: trackOptions.ignoreAreas,
191-
});
192+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith(
193+
{
194+
name: imageName,
195+
imageBase64: screenshot.toString("base64"),
196+
browser: browserName,
197+
viewport: trackOptions.agent?.viewport,
198+
os: trackOptions.agent?.os,
199+
device: trackOptions.agent?.device,
200+
diffTollerancePercent: trackOptions.diffTollerancePercent,
201+
ignoreAreas: trackOptions.ignoreAreas,
202+
comment: trackOptions.comment,
203+
},
204+
2
205+
);
192206
});
193207

194208
it("track default fields", async () => {
@@ -203,15 +217,19 @@ describe("playwright", () => {
203217
await playwrightVrt.trackElementHandle(elementHandle, imageName);
204218

205219
expect(elementHandleMocked!.screenshot).toHaveBeenCalledWith(undefined);
206-
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith({
207-
name: imageName,
208-
imageBase64: screenshot.toString("base64"),
209-
browser: browserName,
210-
viewport: undefined,
211-
os: undefined,
212-
device: undefined,
213-
diffTollerancePercent: undefined,
214-
});
220+
expect(VisualRegressionTracker.prototype.track).toHaveBeenCalledWith(
221+
{
222+
name: imageName,
223+
imageBase64: screenshot.toString("base64"),
224+
browser: browserName,
225+
viewport: undefined,
226+
os: undefined,
227+
device: undefined,
228+
diffTollerancePercent: undefined,
229+
comment: undefined,
230+
},
231+
2
232+
);
215233
});
216234

217235
it("should throw if no elementHandle", async () => {

lib/playwrightVisualRegressionTracker.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,54 @@ export class PlaywrightVisualRegressionTracker {
2929
async trackPage(
3030
page: Pick<Page, "viewportSize" | "screenshot">,
3131
name: string,
32-
options?: PageTrackOptions
32+
options?: PageTrackOptions,
33+
retryCount = 2
3334
) {
3435
const viewportSize = page.viewportSize();
35-
return this.vrt.track({
36-
name,
37-
imageBase64: (await page.screenshot(options?.screenshotOptions)).toString(
38-
"base64"
39-
),
40-
browser: this.browser,
41-
viewport: viewportSize
42-
? `${viewportSize.width}x${viewportSize.height}`
43-
: undefined,
44-
os: options?.agent?.os,
45-
device: options?.agent?.device,
46-
diffTollerancePercent: options?.diffTollerancePercent,
47-
ignoreAreas: options?.ignoreAreas,
48-
});
36+
return this.vrt.track(
37+
{
38+
name,
39+
imageBase64: (
40+
await page.screenshot(options?.screenshotOptions)
41+
).toString("base64"),
42+
browser: this.browser,
43+
viewport: viewportSize
44+
? `${viewportSize.width}x${viewportSize.height}`
45+
: undefined,
46+
os: options?.agent?.os,
47+
device: options?.agent?.device,
48+
diffTollerancePercent: options?.diffTollerancePercent,
49+
ignoreAreas: options?.ignoreAreas,
50+
comment: options?.comment,
51+
},
52+
retryCount
53+
);
4954
}
5055

5156
async trackElementHandle(
5257
elementHandle: ElementHandle | null,
5358
name: string,
54-
options?: ElementHandleTrackOptions
59+
options?: ElementHandleTrackOptions,
60+
retryCount = 2
5561
) {
5662
if (!elementHandle) {
5763
throw new Error("ElementHandle is null");
5864
}
59-
return this.vrt.track({
60-
name,
61-
imageBase64: (
62-
await elementHandle.screenshot(options?.screenshotOptions)
63-
).toString("base64"),
64-
browser: this.browser,
65-
viewport: options?.agent?.viewport,
66-
os: options?.agent?.os,
67-
device: options?.agent?.device,
68-
diffTollerancePercent: options?.diffTollerancePercent,
69-
ignoreAreas: options?.ignoreAreas,
70-
});
65+
return this.vrt.track(
66+
{
67+
name,
68+
imageBase64: (
69+
await elementHandle.screenshot(options?.screenshotOptions)
70+
).toString("base64"),
71+
browser: this.browser,
72+
viewport: options?.agent?.viewport,
73+
os: options?.agent?.os,
74+
device: options?.agent?.device,
75+
diffTollerancePercent: options?.diffTollerancePercent,
76+
ignoreAreas: options?.ignoreAreas,
77+
comment: options?.comment,
78+
},
79+
retryCount
80+
);
7181
}
7282
}

0 commit comments

Comments
 (0)