This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
VisualRegressionLauncher.js
144 lines (129 loc) · 5.21 KB
/
VisualRegressionLauncher.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
/* global browser */
const lodashIdentity = require('lodash.identity');
const lodashPickby = require('lodash.pickby');
const fse = require('fs-extra');
const { LocalCompare } = require('./compare');
const makeElementScreenshot = require('./modules/makeElementScreenshot');
const getTerraFormFactor = require('./modules/getTerraFormFactor');
class VisualRegressionLauncher {
/**
* Service constructor.
* @param {Object} _options - The options specific to this service.
* @param {Object} _capabilities - The list of capabilities details.
* @param {Object} config - The object containing the wdio configuration and options defined in the terra-cli test runner.
*/
constructor(_options, _capabilities, config = {}) {
const { launcherOptions } = config;
this.compare = new LocalCompare(launcherOptions);
this.context = null;
this.currentSuite = null;
this.currentTest = null;
}
/**
* Gets executed before test execution begins. At this point you can access
* all global variables, such as `browser`.
* @param {Object} capabilities - desiredCapabilities
* @param {[type]} specs
* @return null
*/
async before(capabilities) {
this.context = {
desiredCapabilities: capabilities,
};
browser.addCommand('checkElement', this.wrapCommand(browser, makeElementScreenshot));
}
/**
* Hook that gets executed before the suite starts.
* @param {Object} suite - suite details
*/
beforeSuite(suite) {
this.currentSuite = suite;
}
/**
* Hook that gets executed after the suite has ended.
*/
afterSuite() {
this.currentSuite = null;
}
/**
* Function to be executed before a test in Mocha.
* @param {Object} test - test details
*/
beforeTest(test) {
this.currentTest = test;
}
/**
* Function to be executed after a test in Mocha.
* @param {Object} test - test details
* @param {String} test.parent - test parent title
* @param {String} test.title - test title
* @param {Object} results - results object
* @param {boolean} results.passed - indicates if the test passed.
*/
afterTest({ parent, title }, _, { passed }) {
if (!passed) {
const { config } = browser;
const { formFactor } = config.launcherOptions;
const screenshotContext = {
desiredCapabilities: this.context.desiredCapabilities,
suite: this.currentSuite,
test: this.currentTest,
meta: {
currentFormFactor: formFactor,
},
options: {
name: `${parent}_${title}`,
},
};
const screenshotContextCleaned = lodashPickby(screenshotContext, lodashIdentity);
const { errorPath } = this.compare.getScreenshotPaths(screenshotContextCleaned);
fse.ensureFileSync(errorPath);
browser.saveScreenshot(errorPath);
}
this.currentTest = null;
}
/**
* Command wrapper to setup the command with the correct context values defined from the global
* webdriver.IO WebDriver instance.
*
* @param {Object} browser - The global webdriver.IO WebDriver instance.
* @param {function} command - The test command that should be executed.
*/
wrapCommand(browser, command) {
/**
* The wrapped command with access to the global webdriver.IO WebDriver instance.
*
* @param {string} elementSelector - The css selector of the element that should be captured in the screenshot.
* @param {Object=} options - The screenshot capturing and comparison options.
* @param {string[]} options.hide - The list of elements to set opacity 0 on to 'hide' from the dom when capturing the screenshot.
* @param {string[]} options.remove - The list of elements to set display: none on to 'remove' from dom when capturing the screenshot.
* @param {string} options.ignoreComparison - The image comparison algorithm to use when processing the screenshot comparison.
* @param {number} options.mismatchTolerance - The acceptable mismatch tolerance the screenshot can have when processing the screenshot comparison.
* @param {string} options.name - The name of the screenshot.
* @param {boolean} options.updateScreenshots - Whether or not to automatically update all reference screenshots with the latest screenshots.
* @returns {Object} - The screenshot comparison results returned as { misMatchPercentage: Number, isSameDimensions: Boolean, getImageDataUrl: function }.
*/
return async function wrappedScreenshotCommand(elementSelector, options) {
let currentFormFactor;
if (browser.isMobile) {
currentFormFactor = await browser.getOrientation();
} else {
currentFormFactor = await getTerraFormFactor();
}
const screenshotContext = {
desiredCapabilities: this.context.desiredCapabilities,
suite: this.currentSuite,
test: this.currentTest,
meta: {
currentFormFactor,
},
options,
};
const screenshotContextCleaned = lodashPickby(screenshotContext, lodashIdentity);
const base64Screenshot = await command(browser, elementSelector, options);
const results = await this.compare.processScreenshot(screenshotContextCleaned, base64Screenshot);
return results;
}.bind(this);
}
}
module.exports = VisualRegressionLauncher;