-
Notifications
You must be signed in to change notification settings - Fork 659
/
capturer.js
177 lines (134 loc) · 5.88 KB
/
capturer.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { join as joinPath, dirname, basename } from 'path';
import { generateThumbnail } from 'testcafe-browser-tools';
import { cropScreenshot } from './crop';
import { isInQueue, addToQueue } from '../utils/async-queue';
import WARNING_MESSAGE from '../notifications/warning-message';
import escapeUserAgent from '../utils/escape-user-agent';
import correctFilePath from '../utils/correct-file-path';
import { readPngFile, stat, writePng } from '../utils/promisified-functions';
export default class Capturer {
constructor (baseScreenshotsPath, testEntry, connection, pathPattern, fullPage, warningLog) {
this.enabled = !!baseScreenshotsPath;
this.baseScreenshotsPath = baseScreenshotsPath;
this.testEntry = testEntry;
this.provider = connection.provider;
this.browserId = connection.id;
this.warningLog = warningLog;
this.pathPattern = pathPattern;
this.fullPage = fullPage;
}
static _getDimensionWithoutScrollbar (fullDimension, documentDimension, bodyDimension) {
if (bodyDimension > fullDimension)
return documentDimension;
if (documentDimension > fullDimension)
return bodyDimension;
return Math.max(documentDimension, bodyDimension);
}
static _getCropDimensions (cropDimensions, pageDimensions) {
if (!cropDimensions || !pageDimensions)
return null;
const { dpr } = pageDimensions;
const { top, left, bottom, right } = cropDimensions;
return {
top: Math.round(top * dpr),
left: Math.round(left * dpr),
bottom: Math.round(bottom * dpr),
right: Math.round(right * dpr)
};
}
static _getClientAreaDimensions (pageDimensions) {
if (!pageDimensions)
return null;
const { innerWidth, documentWidth, bodyWidth, innerHeight, documentHeight, bodyHeight, dpr } = pageDimensions;
return {
width: Math.floor(Capturer._getDimensionWithoutScrollbar(innerWidth, documentWidth, bodyWidth) * dpr),
height: Math.floor(Capturer._getDimensionWithoutScrollbar(innerHeight, documentHeight, bodyHeight) * dpr)
};
}
static async _isScreenshotCaptured (screenshotPath) {
try {
const stats = await stat(screenshotPath);
return stats.isFile();
}
catch (e) {
return false;
}
}
_joinWithBaseScreenshotPath (path) {
return joinPath(this.baseScreenshotsPath, path);
}
_incrementFileIndexes (forError) {
if (forError)
this.pathPattern.data.errorFileIndex++;
else
this.pathPattern.data.fileIndex++;
}
_getCustomScreenshotPath (customPath) {
const correctedCustomPath = correctFilePath(customPath);
return this._joinWithBaseScreenshotPath(correctedCustomPath);
}
_getScreenshotPath (forError) {
const path = this.pathPattern.getPath(forError);
this._incrementFileIndexes(forError);
return this._joinWithBaseScreenshotPath(path);
}
_getThumbnailPath (screenshotPath) {
const imageName = basename(screenshotPath);
const imageDir = dirname(screenshotPath);
return joinPath(imageDir, 'thumbnails', imageName);
}
async _takeScreenshot ({ filePath, pageWidth, pageHeight, fullPage = this.fullPage }) {
await this.provider.takeScreenshot(this.browserId, filePath, pageWidth, pageHeight, fullPage);
}
async _capture (forError, { pageDimensions, cropDimensions, markSeed, customPath, fullPage } = {}) {
if (!this.enabled)
return null;
const screenshotPath = customPath ? this._getCustomScreenshotPath(customPath) : this._getScreenshotPath(forError);
const thumbnailPath = this._getThumbnailPath(screenshotPath);
if (isInQueue(screenshotPath))
this.warningLog.addWarning(WARNING_MESSAGE.screenshotRewritingError, screenshotPath);
await addToQueue(screenshotPath, async () => {
const clientAreaDimensions = Capturer._getClientAreaDimensions(pageDimensions);
const { width: pageWidth, height: pageHeight } = clientAreaDimensions || {};
const takeScreenshotOptions = {
filePath: screenshotPath,
pageWidth,
pageHeight,
fullPage
};
await this._takeScreenshot(takeScreenshotOptions);
if (!await Capturer._isScreenshotCaptured(screenshotPath))
return;
const image = await readPngFile(screenshotPath);
const croppedImage = await cropScreenshot(image, {
markSeed,
clientAreaDimensions,
path: screenshotPath,
cropDimensions: Capturer._getCropDimensions(cropDimensions, pageDimensions)
});
if (croppedImage)
await writePng(screenshotPath, croppedImage);
await generateThumbnail(screenshotPath, thumbnailPath);
});
const testRunId = this.testEntry.testRuns[this.browserId].id;
const userAgent = escapeUserAgent(this.pathPattern.data.parsedUserAgent.prettyUserAgent);
const quarantineAttempt = this.pathPattern.data.quarantineAttempt;
const takenOnFail = forError;
const screenshot = {
testRunId,
screenshotPath,
thumbnailPath,
userAgent,
quarantineAttempt,
takenOnFail
};
this.testEntry.screenshots.push(screenshot);
return screenshotPath;
}
async captureAction (options) {
return await this._capture(false, options);
}
async captureError (options) {
return await this._capture(true, options);
}
}