Welcome to mirror list, hosted at ThFree Co, Russian Federation.

app.js « support « screenshot-testing « lib « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0aff28d6dcbb42fe675fb2454dbc68f6c8154cf7 (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*!
 * Piwik - free/libre analytics platform
 *
 * UI screenshot test runner Application class
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

var fs = require('fs'),
    fsExtra = require('fs-extra'),
    path = require('./path');

var walk = function (dir, pattern, result) {
    result = result || [];

    if (!fs.isDirectory(dir)) {
        return result;
    }

    fs.readdirSync(dir).forEach(function (item) {
        if (item === '.'
            || item === '..'
        ) {
            return;
        }

        var wholePath = path.join(dir, item);

        if (fs.isDirectory(wholePath)) {
            walk(wholePath, pattern, result);
        } else if (wholePath.match(pattern)) {
            result.push(wholePath);
        }
    });

    return result;
};

var isCorePlugin = function (pathToPlugin) {
    // if the plugin is a .git checkout, it's not part of core
    var gitDir = path.join(pathToPlugin, '.git');
    return !fs.existsSync(gitDir);
};

var Application = function () {
    this.runner = null;

    this.diffviewerDir = path.join(PIWIK_INCLUDE_PATH, 'tests/UI', config.screenshotDiffDir);
};

Application.prototype.printHelpAndExit = function () {
    console.log("Usage: phantomjs run-tests.js [options] [test-files]");
    console.log();
    console.log("Available options:");
    console.log("  --help:                   Prints this message.");
    console.log("  --persist-fixture-data:   Persists test data in a database and does not execute tear down.");
    console.log("                            After the first run, the database setup will not be called, which");
    console.log("                            Makes running tests faster.");
    console.log("  --plugin=name:            Runs all tests for a plugin.");
    console.log("  --keep-symlinks:          If supplied, the recursive symlinks created in tests/PHPUnit/proxy");
    console.log("                            aren't deleted after tests are run. Specify this option if you'd like");
    console.log("                            to view pages phantomjs captures in a browser.");
    console.log("  --print-logs:             Prints webpage logs even if tests succeed.");
    console.log("  --store-in-ui-tests-repo: Stores processed screenshots within the UI tests repository even if");
    console.log("                            the tests are in another plugin. For use with travis build.");
    console.log("  --assume-artifacts:       Assume the diffviewer and processed screenshots will be stored on the.");
    console.log("                            builds artifacts server. For use with travis build.");
    console.log("  --screenshot-repo:        Specifies the GitHub repository that contains the expected screenshots");
    console.log("                            to link to in the diffviewer. For use with travis build.");
    console.log("  --core:                   Only execute UI tests that are for Piwik core or Piwik core plugins.");
    console.log("  --first-half:             Only execute first half of all the test suites. Will be only applied if no")
    console.log("                            specific plugin or test-files requested");
    console.log("  --second-half:            Only execute second half of all the test suites. Will be only applied if no")
    console.log("                            specific plugin or test-files requested");

    process.exit(0);
};

Application.prototype.init = function () {
    var app = this;

    // overwrite describe function so we can inject the base directory of a suite
    var oldDescribe = describe;
    describe = function () {
        var suite = oldDescribe.apply(null, arguments);
        suite.baseDirectory = app.currentModulePath.match(/\/plugins\//) ? path.dirname(app.currentModulePath) : uiTestsDir;
        if (options['assume-artifacts']) {
            suite.diffDir = path.join(PIWIK_INCLUDE_PATH, 'tests/UI', config.screenshotDiffDir);
        } else {
            suite.diffDir = path.join(suite.baseDirectory, config.screenshotDiffDir);
        }

        // remove existing diffs
        if (!fs.existsSync(suite.diffDir)) {
            fs.mkdirSync(suite.diffDir);
        }

        fs.readdirSync(suite.diffDir).forEach(function (item) {
            var file = path.join(suite.diffDir, item);
            if (fs.existsSync(file)
                && item.slice(-4) === '.png'
            ) {
                fs.unlinkSync(file);
            }
        });

        return suite;
    };
};

Application.prototype.loadTestModules = function () {
    var self = this,
        pluginDir = path.join(PIWIK_INCLUDE_PATH, 'plugins');

    // find all installed plugins
    var plugins = fs.readdirSync(pluginDir).map(function (item) {
        return path.join(pluginDir, item);
    }).filter(function (path) {
        return fs.isDirectory(path) && !path.match(/\/\.*$/);
    });

    // load all UI tests we can find
    var modulePaths = walk(uiTestsDir, /_spec\.js$/);

    if (options.core) {
        plugins = plugins.filter(function (path) {
            return isCorePlugin(path);
        });
    }

    plugins.forEach(function (pluginPath) {
        walk(path.join(pluginPath, 'Test'), /_spec\.js$/, modulePaths);
        walk(path.join(pluginPath, 'tests'), /_spec\.js$/, modulePaths);
    });

    modulePaths.forEach(function (path) {
        self.currentModulePath = path;

        require(path);
    });

    // filter suites to run
    if (options.tests.length) {
        mocha.suite.suites = mocha.suite.suites.filter(function (suite) {
            return options.tests.indexOf(suite.title) != -1;
        });
    }

    if (options.plugin) {
        mocha.suite.suites = mocha.suite.suites.filter(function (suite) {
            return suite.baseDirectory.match(new RegExp("\/plugins\/" + options.plugin + "\/"));
        });
    }

    var specificTestsRequested = options.plugin || options.tests.length;

    if ((options['run-first-half-only'] || options['run-second-half-only']) && !specificTestsRequested) {
        // run only first 50% of the test suites or only run last 50% of the test suites.
        // we apply this option only if not a specific plugin or test suite was requested. Only there for travis to
        // split tests into multiple jobs.
        var numTestsFirstHalf = Math.round(mocha.suite.suites.length / 2);
        numTestsFirstHalf -= 3;
        mocha.suite.suites = mocha.suite.suites.filter(function (suite, index) {
            if (options['run-first-half-only'] && index < numTestsFirstHalf) {
                return true;
            } else if (options['run-second-half-only'] && index >= numTestsFirstHalf) {
                return true;
            }
            return false;
        });
    }

    if (!mocha.suite.suites.length) {
        console.log("No tests are executing... are you running tests for a plugin? Make sure to use the"
                  + " --plugin=MyPlugin option.");
    }

    // configure suites (auto-add fixture setup/teardown)
    mocha.suite.suites.forEach(function (suite) {
        var fixture = typeof suite.fixture === 'undefined' ? "Piwik\\Tests\\Fixtures\\UITestFixture" : suite.fixture;

        suite.beforeAll(function (done) {
            this.timeout(0); // no timeout for fixture setup (this requires normal anonymous function, not fat arrow function)

            var oldOptions = JSON.parse(JSON.stringify(options));
            if (suite.optionsOverride) {
                for (var key in suite.optionsOverride) {
                    options[key] = suite.optionsOverride[key];
                }
            }

            testEnvironment.setupFixture(fixture, (error, result) => {
                options = oldOptions;

                done(error, result);
            });
        });

        // move to before other hooks
        suite._beforeAll.unshift(suite._beforeAll.pop());

        suite.afterAll(function (done) {
            this.timeout(0); // no timeout for fixture teardown (this requires normal anonymous function, not fat arrow function)

            var oldOptions = JSON.parse(JSON.stringify(options));
            if (suite.optionsOverride) {
                for (var key in suite.optionsOverride) {
                    options[key] = suite.optionsOverride[key];
                }
            }

            testEnvironment.teardownFixture(fixture, (error, result) => {
                options = oldOptions;

                done(error, result);
            });
        });

        // if a test fails, print failure info and for non-comparison fails, save failure screenshot
        suite.afterEach(async function() {
            const test = this.currentTest;
            const err = this.currentTest.err;
            if (!err) {
                return;
            }

            var indent = "     ";

            var message = err && err.message ? err.message : err;
            if (message.indexOf(indent) !== 0) {
                message = indent + message.replace(/\n/g, "\n" + indent);
            }

            const url = await page.getWholeCurrentUrl();
            message += "\n" + indent + indent + "Url to reproduce: " + url + "\n";

            if (message.indexOf('Generated screenshot') === -1) {
                const failurePath = path.join(PIWIK_INCLUDE_PATH, 'tests/UI/processed-ui-screenshots', test.title.replace(/\s+/g, '_') + '_failure.png');

                message += indent + indent + "Screenshot of failure: " + failurePath + "\n";

                const screenshot = await page.screenshot({ fullPage: true });
                fs.writeFileSync(failurePath, screenshot);
            } else {
                delete this.currentTest.err.stack;
            }

            var renderingLogs = page.getPageLogsString(indent);
            if (renderingLogs) {
                message += renderingLogs + "\n";
            } else {
                message += indent + indent + "No captured console logs.\n";
            }

            console.log(message); // so it prints out as the test fails (for builds that run too long)
            this.currentTest.err.message = message.replace(/\n/g, "\n  ");
        });
    });
};

Application.prototype.runTests = function (mocha) {
    // make sure all necessary directories exist (symlinks handled by PHP since phantomjs can't create any)
    var dirsToCreate = [
        path.join(PIWIK_INCLUDE_PATH, 'tmp/sessions')
    ];

    dirsToCreate.forEach(function (path) {
        if (!fs.isDirectory(path)) {
            fsExtra.mkdirsSync(path);
        }
    });

    this.doRunTests(mocha);
};

Application.prototype.doRunTests = function (mocha) {
    testEnvironment.reload();

    // run tests
    this.runner = mocha.run(function (failures) {
        // remove symlinks
        if (!options['keep-symlinks']) {
            var symlinks = ['libs', 'plugins', 'tests', 'misc', 'piwik.js', 'matomo.js'];

            symlinks.forEach(function (item) {
                var file = path.join(uiTestsDir, '..', 'PHPUnit', 'proxy', item);
                if (fs.existsSync(file)) {
                    fs.unlinkSync(file);
                }
            });
        }

        process.exit(failures);
    });

    this.runner.on('suite', function() {
        page.webpage.mouse.move(-10, -10);
    });

    this.runner.on('test', function () {
        page._reset();
    });
};

Application.prototype.finish = function () {
    process.exit(this.runner ? this.runner.failures : -1);
};

Application.prototype.appendMissingExpected = function (screenName) {
    var missingExpectedFilePath = path.join(this.diffviewerDir, 'missing-expected.list');
    fs.appendFileSync(missingExpectedFilePath, screenName + "\n");
};

exports.Application = new Application();