mattermost-desktop/test/modules/environment.js

103 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-05-30 08:23:57 -07:00
// Copyright (c) 2015-2016 Yuya Ochiai
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2016-05-02 06:34:00 -07:00
'use strict';
2016-12-27 05:10:53 -08:00
const fs = require('fs');
2016-05-02 06:34:00 -07:00
const path = require('path');
2018-02-20 04:44:23 -08:00
2016-05-21 01:23:23 -07:00
const Application = require('spectron').Application;
2018-02-20 04:44:23 -08:00
const chai = require('chai');
chai.should();
2016-05-02 06:34:00 -07:00
2016-09-25 07:14:01 -07:00
const sourceRootDir = path.join(__dirname, '../..');
const electronBinaryPath = (() => {
2016-05-02 06:34:00 -07:00
if (process.platform === 'darwin') {
return path.join(sourceRootDir, 'node_modules/electron/dist/Electron.app/Contents/MacOS/Electron');
2016-05-02 06:34:00 -07:00
}
2016-09-25 07:14:01 -07:00
const exeExtension = (process.platform === 'win32') ? '.exe' : '';
return path.join(sourceRootDir, 'node_modules/electron/dist/electron' + exeExtension);
2016-05-02 06:34:00 -07:00
})();
const userDataDir = path.join(sourceRootDir, 'test/testUserData/');
const configFilePath = path.join(userDataDir, 'config.json');
2016-12-27 05:10:53 -08:00
const boundsInfoPath = path.join(userDataDir, 'bounds-info.json');
2018-01-19 03:50:00 -08:00
const mattermostURL = 'http://example.com/';
2016-05-02 06:34:00 -07:00
module.exports = {
2016-09-25 07:14:01 -07:00
sourceRootDir,
configFilePath,
2017-10-04 08:02:54 -07:00
userDataDir,
2016-12-27 05:10:53 -08:00
boundsInfoPath,
2016-09-25 07:14:01 -07:00
mattermostURL,
2016-12-27 05:10:53 -08:00
cleanTestConfig() {
[configFilePath, boundsInfoPath].forEach((file) => {
try {
fs.unlinkSync(file);
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(err);
}
}
});
},
createTestUserDataDir() {
if (!fs.existsSync(userDataDir)) {
fs.mkdirSync(userDataDir);
}
},
2016-09-25 07:14:01 -07:00
getSpectronApp() {
const options = {
2016-09-25 07:14:01 -07:00
path: electronBinaryPath,
args: [`${path.join(sourceRootDir, 'src')}`, `--data-dir=${userDataDir}`, '--disable-dev-mode'],
chromeDriverArgs: [],
// enable this if chromedriver hangs to see logs
// chromeDriverLogPath: '../chromedriverlog.txt',
};
if (process.platform === 'darwin') {
// on a mac, debbuging port might conflict with other apps
// this changes the default debugging port so chromedriver can run without issues.
options.chromeDriverArgs.push('remote-debugging-port=9222');
}
return new Application(options);
2016-05-21 04:57:59 -07:00
},
2016-09-25 07:14:01 -07:00
addClientCommands(client) {
client.addCommand('loadSettingsPage', function async() {
2017-03-06 04:31:17 -08:00
return this.url('file://' + path.join(sourceRootDir, 'src/browser/settings.html')).waitUntilWindowLoaded();
});
client.addCommand('isNodeEnabled', function async() {
2016-09-25 07:14:01 -07:00
return this.execute(() => {
try {
2016-09-25 07:14:01 -07:00
if (require('child_process')) {
return true;
}
return false;
} catch (e) {
return false;
}
2016-09-25 07:14:01 -07:00
}).then((requireResult) => {
return requireResult.value;
});
});
client.addCommand('waitForAppOptionsAutoSaved', function async() {
const ID_APP_OPTIONS_SAVE_INDICATOR = '#appOptionsSaveIndicator';
const TIMEOUT = 5000;
return this.
waitForVisible(ID_APP_OPTIONS_SAVE_INDICATOR, TIMEOUT).
waitForVisible(ID_APP_OPTIONS_SAVE_INDICATOR, TIMEOUT, true);
});
},
// execute the test only when `condition` is true
2016-09-25 07:14:01 -07:00
shouldTest(it, condition) {
return condition ? it : it.skip;
},
isOneOf(platforms) {
return (platforms.indexOf(process.platform) !== -1);
},
2016-09-25 07:14:01 -07:00
};