-
Notifications
You must be signed in to change notification settings - Fork 3
/
screenshots.js
62 lines (56 loc) · 1.45 KB
/
screenshots.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
// @flow
import fs from 'fs-promise';
import imageDiff from 'image-diff';
import pngCrop from 'png-crop';
export function compareScreenshots(
actualImage: string,
expectedImage: string,
diffImage: string,
): Promise<boolean> {
return new Promise((resolve) => {
imageDiff({
actualImage,
expectedImage,
diffImage,
}, (err, imagesAreSame) => {
if (err) {
throw err;
} else {
resolve(imagesAreSame);
}
});
});
}
export function saveScreenshot(
screenshotPath: string,
screenshotData: string,
): Promise<string> {
return fs.outputFile(screenshotPath, screenshotData, 'base64');
}
export function cropScreenshot(
screenshotPath: string,
windowSize: size,
elementSize: size,
elementLocation: location,
): Promise<string> {
return new Promise((resolve) => {
const cropWidth = elementSize.width < windowSize.width;
const cropHeight = elementSize.height < windowSize.height;
if (cropWidth || cropHeight) {
const config = {
width: cropWidth ? elementSize.width : windowSize.width,
height: cropHeight ? elementSize.height : windowSize.height,
top: Math.floor(elementLocation.y),
left: Math.floor(elementLocation.x),
};
pngCrop.crop(screenshotPath, screenshotPath, config, (err) => {
if (err) {
throw err;
}
resolve(screenshotPath);
});
} else {
resolve(screenshotPath);
}
});
}