forked from NimaSoroush/differencify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration.test.js
284 lines (282 loc) · 8.85 KB
/
integration.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import Differencify from '../index';
const differencify = new Differencify({ debug: true });
describe('Differencify', () => {
beforeAll(async () => {
await differencify.launchBrowser({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
});
afterAll(async () => {
await differencify.cleanup();
});
it('simple', async () => {
await differencify
.init()
.newPage()
.setViewport({ width: 1600, height: 1200 })
.goto('http://example.com/')
.waitFor(1000)
.screenshot()
.toMatchSnapshot()
.close()
.end();
}, 30000);
it('simple unchained', async () => {
const target = differencify.init({ chain: false });
const page = await target.newPage();
await page.goto('http://example.com/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image = await page.screenshot();
const result = await target.toMatchSnapshot(image);
await page.close();
expect(result).toEqual(true);
}, 30000);
it('Launch new browser per test', async () => {
await differencify
.init()
.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] })
.newPage()
.setViewport({ width: 1600, height: 1200 })
.goto('http://example.com/')
.waitFor(1000)
.screenshot()
.toMatchSnapshot()
.close()
.end();
}, 30000);
it('Launch new browser per test when unchained', async () => {
const target = differencify.init({ chain: false });
await target.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await target.newPage();
await page.goto('http://example.com/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image = await page.screenshot();
const result = await target.toMatchSnapshot(image);
await page.close();
await target.close();
expect(result).toEqual(true);
}, 30000);
it('Using result function', async () => {
await differencify
.init()
.newPage()
.setViewport({ width: 1600, height: 1200 })
.goto('http://example.com/')
.waitFor(1000)
.title()
.result((title) => {
expect(title).toEqual('Example Domain');
})
.screenshot()
.toMatchSnapshot()
.close()
.end();
}, 30000);
it('Using toMatchSnapshot callback for result details', async () => {
await differencify
.init()
.newPage()
.setViewport({ width: 1600, height: 1200 })
.goto('http://example.com/')
.waitFor(1000)
.title()
.screenshot()
.toMatchSnapshot((resultDetail) => {
expect(resultDetail).toEqual({
testConfig: {
chain: true,
imageType: 'png',
isJest: true,
isUpdate: false,
testId: 6,
testName: 'Differencify Using toMatchSnapshot callback for result details',
testNameProvided: false,
testPath: '/differencify/src/integration.tests/integration.test.js',
},
testResult: {
diffPercent: 0,
distance: 0,
matched: true,
snapshotPath: '/differencify/src/integration.tests/__image_snapshots__/Differencify Using toMatchSnapshot callback for result details.snap.png',
},
});
})
.close()
.end();
}, 30000);
it('Using toMatchSnapshot callback for result details when unchained', async () => {
const target = differencify.init({ chain: false });
await target.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const page = await target.newPage();
await page.goto('http://example.com/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image = await page.screenshot();
await target.toMatchSnapshot(image, (resultDetail) => {
expect(resultDetail).toEqual({
testConfig: {
chain: false,
imageType: undefined,
isJest: true,
isUpdate: false,
newWindow: true,
testId: 7,
testName: 'Differencify Using toMatchSnapshot callback for result details when unchained',
testNameProvided: false,
testPath: '/differencify/src/integration.tests/integration.test.js',
},
testResult: {
diffPercent: 0,
distance: 0,
matched: true,
snapshotPath: '/differencify/src/integration.tests/__image_snapshots__/Differencify Using toMatchSnapshot callback for result details when unchained.snap.png',
},
});
});
await page.close();
await target.close();
}, 30000);
it('Context switching when chained', async () => {
await differencify
.init()
.newPage()
.tracing
.start({ path: 'trace.json' })
.page
.setViewport({ width: 1600, height: 1200 })
.goto('http://example.com/')
.waitFor(1000)
.keyboard
.press('Space')
.tracing
.stop()
.page
.screenshot()
.toMatchSnapshot()
.close()
.end();
}, 30000);
it('Calling Puppeteer specific functions when chained: console', async () => {
await differencify
.init()
.newPage()
.on('console', (msg) => {
for (let i = 0; i < msg.args.length; i += 1) {
expect(`${msg.args[i]}`).toEqual('JSHandle:hello');
}
})
.evaluate(() => console.log('hello'))
.close()
.end();
}, 30000);
it('Calling Puppeteer specific functions when chained: dialog', async () => {
await differencify
.init()
.newPage()
.on('dialog', async (dialog) => {
expect(dialog.message()).toEqual('1');
await dialog.dismiss();
})
.evaluate(() => alert('1'))
.close()
.end();
}, 30000);
it('Continue on chained object', async () => {
await differencify
.init()
.newPage()
.goto('http://example.com/')
.mainFrame()
.then
.url()
.result((url) => {
expect(url).toEqual('http://example.com/');
})
.close()
.end();
}, 30000);
it('Multiple toMatchSnapshot on chained object', async () => {
await differencify
.init()
.newPage()
.goto('http://example.com/')
.waitFor(1000)
.screenshot()
.toMatchSnapshot()
.result((result) => {
expect(result).toEqual(true);
})
.goto('http://example.com/')
.waitFor(1000)
.screenshot()
.toMatchSnapshot()
.result((result) => {
expect(result).toEqual(true);
})
.close()
.end();
}, 30000);
it('Multiple toMatchSnapshot when unchained', async () => {
const target = differencify.init({ chain: false });
const page = await target.newPage();
await page.goto('http://example.com/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image = await page.screenshot();
const result = await target.toMatchSnapshot(image);
await page.goto('http://example.net/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image2 = await page.screenshot();
const result2 = await target.toMatchSnapshot(image2);
await page.close();
expect(result).toEqual(true);
expect(result2).toEqual(true);
}, 30000);
it('Custom test name', async () => {
const target = differencify.init({
testName: 'test1',
chain: false,
});
const page = await target.newPage();
await page.goto('http://example.com/');
await page.setViewport({ width: 1600, height: 1200 });
await page.waitFor(1000);
const image = await page.screenshot();
const result = await target.toMatchSnapshot(image);
await page.close();
expect(result).toEqual(true);
}, 30000);
it('Custom test path', async () => {
const customDifferencify = new Differencify({
imageSnapshotPath: './src/integration.tests/__image_snapshots__/custom_test_path',
debug: true,
});
await customDifferencify.launchBrowser({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
const target = customDifferencify.init({
chain: false,
});
const page = await target.newPage();
await page.setViewport({ width: 1600, height: 1200 });
await page.goto('http://example.com/');
await page.waitFor(1000);
const image = await page.screenshot();
const result = await target.toMatchSnapshot(image);
await page.close();
await customDifferencify.cleanup();
expect(result).toEqual(true);
}, 30000);
it('Freeze image in page', async () => {
await differencify
.init()
.newPage()
.setViewport({ width: 1600, height: 1200 })
.goto('https://i.giphy.com/media/xTiTnoUnHxVaaVNWhO/giphy.webp')
.waitFor('body > img')
.freezeImage('body > img')
.screenshot()
.toMatchSnapshot()
.close()
.end();
}, 30000);
});