-
Notifications
You must be signed in to change notification settings - Fork 43
/
normalizeScreenshot.test.js
150 lines (122 loc) · 5.28 KB
/
normalizeScreenshot.test.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
import {
assert
} from 'chai';
import glob from 'glob';
import path from 'path';
import _ from 'lodash';
import fsExtra from 'fs-extra';
import normalizeScreenshot from '../../../src/utils/normalizeScreenshot';
import ScreenDimension from '../../../src/utils/ScreenDimension';
import saveBase64Image from '../../../src/utils/saveBase64Image';
import compareImages from '../../helper/compareImages';
import dimensionDesktop from '../../fixture/dimension/desktop-scroll-both.json';
const screenshotDir = path.join(process.cwd(), 'test', 'fixture', 'screenshot');
const tmpPath = path.join(process.cwd(), '.tmp');
async function readAsBase64(file) {
// read binary data
const content = await fsExtra.readFile(file);
// convert binary data to base64 encoded string
return new Buffer(content).toString('base64');
}
describe('normalizeScreenshot', function() {
context('default browser behaviour', function() {
beforeEach(function () {
this.browser = {
isMobile: false,
isIOS: false,
isAndroid: false
};
this.screenDimensions = new ScreenDimension(dimensionDesktop);
this.base64Screenshot = "base64Screenshot";
});
it('just returns the same screenshot', async function () {
const screenshot = await normalizeScreenshot(this.browser, this.screenDimensions, this.base64Screenshot);
assert.strictEqual(screenshot, this.base64Screenshot, 'screenshots should not be transformed');
});
});
context('MacbookProRetina', function() {
const baseDir = path.join(screenshotDir, 'MacbookProRetina');
const files = glob.sync('**/screenshot.png', {cwd: baseDir});
const data = files.map((file) => {
const dir = path.dirname(file);
return {
browserName: dir,
screenshotFile: path.join(baseDir, file),
expectedScreenshotFile: path.join(baseDir, dir, 'expected.png'),
dimensionsFile: path.join(baseDir, dir, 'dimensions.json'),
dir,
};
});
_.map(data, ({browserName, screenshotFile, expectedScreenshotFile, dimensionsFile}) => {
context(browserName, function () {
it('normalizes screenshot', async function () {
const browser = {
isMobile: false,
isIOS: true,
isAndroid: false
};
const dimensions = await fsExtra.readJson(dimensionsFile);
const base64Screenshot = await readAsBase64(screenshotFile);
await readAsBase64(expectedScreenshotFile); // just to check if it exists
const screenDimensions = new ScreenDimension(dimensions);
const normalizedSreenshot = await normalizeScreenshot(browser, screenDimensions, base64Screenshot);
const normalizedSreenshotPath = path.join(tmpPath, 'normalizeScreenshot', browserName, 'normalized.png');
await saveBase64Image(normalizedSreenshotPath, normalizedSreenshot);
await compareImages(normalizedSreenshotPath, expectedScreenshotFile, 0.001);
});
});
});
});
context('iOS', function() {
const iOSDir = path.join(screenshotDir, 'iOS');
const files = glob.sync('**/screenshot.png', {cwd: iOSDir});
const data = files.map((file) => {
const dir = path.dirname(file);
const [ version, device, test ] = _.times(2)
.reduce((f, v, i) => f.concat(path.dirname(f[f.length -1])), [dir])
.reverse()
.map((f) => path.basename(f).replace(/_/g, ' '))
return {
version,
device,
test,
screenshotFile: path.join(iOSDir, file),
expectedScreenshotFile: path.join(iOSDir, dir, 'expected.png'),
dimensionsFile: path.join(iOSDir, dir, 'dimensions.json'),
skipFile: path.join(iOSDir, dir, '.SKIP'),
dir,
};
});
const testData = _.mapValues(_.groupBy(data, "version"), (list) => _.groupBy(list, "device"));
_.mapKeys(testData, (devices, version) => {
context(version, function () {
_.mapKeys(devices, (list, device) => {
context(device, function () {
list.forEach(({ test, screenshotFile, expectedScreenshotFile, dimensionsFile, skipFile, dir }) => {
it(test, async function () {
const browser = {
isMobile: true,
isIOS: true,
isAndroid: false
};
const skip = await fsExtra.exists(skipFile);
if (skip) {
this.skip();
return;
}
const dimensions = await fsExtra.readJson(dimensionsFile);
const base64Screenshot = await readAsBase64(screenshotFile);
const screenDimensions = new ScreenDimension(dimensions, browser);
const normalizedSreenshot = await normalizeScreenshot(browser, screenDimensions, base64Screenshot);
const normalizedSreenshotPath = path.join(tmpPath, 'normalizeScreenshot', dir, 'normalized.png');
await saveBase64Image(normalizedSreenshotPath, normalizedSreenshot);
await readAsBase64(expectedScreenshotFile); // just to check if it exists
await compareImages(normalizedSreenshotPath, expectedScreenshotFile, 0.001);
});
});
});
});
});
});
});
});