forked from Crunch-io/nightwatch-vrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshotIdenticalToBaseline.js
65 lines (57 loc) · 2.25 KB
/
screenshotIdenticalToBaseline.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
'use strict'
const compareWithBaseline = require('../lib/compare-with-baseline')
/**
* Asserts if a screenshot that captures the visual representation of
* an element in the application is identical to a previously captured
* screenshot used as a baseline.
*
* When this assertion is executed and the baseline screenshot doesn't exist,
* it will save the captured screenshot as the baseline and the assertion will succeed.
*
* The baseline screenshot will be saved in the baseline directory passed in
* the settings here, or the directory specified in the nightwatch configuration
* (under test_settings/visual_regression), or in a default generated path; in
* that order. Further assertions will compare against the screenshot that was
* saved in the first execution of the assertion.
*
* @param {String} selector Identifies the element that will be captured in the screenshot.
* @param {String} fileName Optional file name for this screenshot; defaults to the selector
* @param {NightwatchVRTOptions} settings Optional settings to override the defaults and `visual_regression_settings`
* @param {String} message Optional message for `nightwatch` to log upon completion
*/
exports.assertion = function screenshotIdenticalToBaseline(
elementId,
fileName = elementId,
settings,
message
) {
this.message = message || `Visual regression test results for element <${elementId}>.`
this.expected = true
this.pass = function pass(value) {
return value === this.expected
}
this.value = function value(result) {
return result
}
this.command = function command(callback) {
let screenshot,
comparisonResult
this
.api
.waitForElementVisible(elementId, 5000)
.captureElementScreenshot(elementId, (elementScreenshot) => {
screenshot = elementScreenshot
})
.perform((done) => {
compareWithBaseline(this.api, screenshot, fileName, settings).then((result) => {
comparisonResult = result
done()
})
})
.perform((done) => {
callback(comparisonResult)
done()
})
return this
}
}