-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathscreenshots_spec.js
More file actions
126 lines (114 loc) · 5.01 KB
/
screenshots_spec.js
File metadata and controls
126 lines (114 loc) · 5.01 KB
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
const _ = require('lodash')
const path = require('path')
const Promise = require('bluebird')
const Fixtures = require('../lib/fixtures')
const systemTests = require('../lib/system-tests').default
let sizeOf = require('image-size')
const { fs } = require('@packages/server/lib/util/fs')
sizeOf = Promise.promisify(sizeOf)
const screenshotsPath = Fixtures.projectPath('screenshots')
const onServer = function (app) {
app.get('/color/:color', (req, res) => {
return systemTests.sendHtml(`\
<style>body { margin: 0; }</style>
<div style="height: 2000px; width: 2000px; background-color: ${req.params.color};"></div>`)(req, res)
})
app.get('/fullPage', systemTests.sendHtml(`\
<style>body { margin: 0; }</style>
<div style="background: white; height: 200px;"></div>
<div style="background: black; height: 200px;"></div>
<div style="background: white; height: 100px;"></div>\
`))
app.get('/fullPage-same', systemTests.sendHtml(`\
<style>body { margin: 0; }</style>
<div style="height: 500px;"></div>\
`))
app.get('/element', systemTests.sendHtml(`\
<div class="element" style="background: red; width: 400px; height: 300px; margin: 20px;"></div>\
`))
app.get('/pathological', systemTests.sendHtml(`\
<style>div { width: 1px; height: 1px; position: fixed; }</style>
<div style="left: 0; top: 0; background-color: grey;"></div>
<div style="left: 1px; top: 0; background-color: white;"></div>
<div style="left: 0; top: 1px; background-color: white;"></div>
<div style="right: 0; top: 0; background-color: white;"></div>
<div style="left: 0; bottom: 0; background-color: white;"></div>
<div style="right: 0; bottom: 0; background-color: black;"></div>\
`))
return app.get('/identical', systemTests.sendHtml(`\
<style>div { height: 1300px; width: 200px; background-color: #ddd; }</style>
<div></div>\
`))
}
describe('e2e screenshots', () => {
systemTests.setup({
servers: {
port: 3322,
onServer,
},
})
// this tests that screenshots can be manually generated
// and are also generated automatically on failure with
// the test title as the file name
systemTests.it('passes', {
browser: '!webkit', // TODO(webkit): fix+unskip (failing partially due to broken stack trace)
project: 'screenshots',
spec: 'screenshots.cy.js',
expectedExitCode: 5,
snapshot: true,
timeout: 180000,
onStdout: systemTests.normalizeWebpackErrors,
onRun (exec, browser) {
return exec()
.then(() => {
const screenshot = (...paths) => {
return path.join(screenshotsPath, 'cypress', 'screenshots', 'screenshots.cy.js', ...paths)
}
const screenshot1 = screenshot('black.png')
const screenshot2 = screenshot('red.png')
const screenshot3 = screenshot('foo', 'bar', 'baz.png')
const screenshot4 = screenshot('taking screenshots -- generates pngs on failure (failed).png')
const screenshot5 = screenshot('taking screenshots -- before hooks -- empty test 1 -- before all hook (failed).png')
const screenshot6 = screenshot('taking screenshots -- each hooks -- empty test 2 -- before each hook (failed).png')
const screenshot7 = screenshot('taking screenshots -- each hooks -- empty test 2 -- after each hook (failed).png')
const screenshot8 = screenshot('taking screenshots -- ensures unique paths when there\'s a non-named screenshot and a failure.png')
const screenshot9 = screenshot('taking screenshots -- ensures unique paths when there\'s a non-named screenshot and a failure (failed).png')
return Promise.all([
fs.statAsync(screenshot1).get('size'),
fs.statAsync(screenshot2).get('size'),
fs.statAsync(screenshot3).get('size'),
fs.statAsync(screenshot4).get('size'),
fs.statAsync(screenshot5).get('size'),
fs.statAsync(screenshot6).get('size'),
// Ignore comparing 6 and 7 since they can sometimes be the same since we take the screenshot as close to the failure as possible and
// the test run error may not have displayed yet. Leaving this commented in case we want to change this behavior in the future
// fs.statAsync(screenshot7).get('size'),
fs.statAsync(screenshot8).get('size'),
fs.statAsync(screenshot9).get('size'),
])
.then((sizes) => {
// make sure all of the values are unique
expect(sizes).to.deep.eq(_.uniq(sizes))
}).then(() => {
return Promise.all([
sizeOf(screenshot1),
sizeOf(screenshot2),
sizeOf(screenshot3),
sizeOf(screenshot4),
sizeOf(screenshot5),
sizeOf(screenshot6),
sizeOf(screenshot7),
])
}).then((dimensions = []) => {
if (browser === 'electron') {
// all of the images should be 1280x720
// since thats what we run headlessly
return _.each(dimensions, (dimension) => {
expect(dimension).to.deep.eq({ width: 1280, height: 720, type: 'png' })
})
}
})
})
},
})
})