Use Spectron to execute test

This commit is contained in:
Yuya Ochiai
2016-05-21 17:23:23 +09:00
parent 3e3faf9841
commit b52dfa5348
5 changed files with 152 additions and 173 deletions

View File

@@ -31,7 +31,6 @@
"babel-core": "^6.7.5", "babel-core": "^6.7.5",
"babel-loader": "^6.2.4", "babel-loader": "^6.2.4",
"babel-preset-react": "^6.5.0", "babel-preset-react": "^6.5.0",
"chromedriver": "^2.20.0",
"del": "^2.2.0", "del": "^2.2.0",
"electron-builder": "^3.11.0", "electron-builder": "^3.11.0",
"electron-connect": "^0.3.7", "electron-connect": "^0.3.7",
@@ -48,10 +47,10 @@
"mocha": "^2.3.4", "mocha": "^2.3.4",
"mocha-circleci-reporter": "0.0.1", "mocha-circleci-reporter": "0.0.1",
"should": "^8.0.1", "should": "^8.0.1",
"spectron": "~3.0.0",
"style-loader": "^0.13.0", "style-loader": "^0.13.0",
"through2": "^2.0.1", "through2": "^2.0.1",
"vinyl-named": "^1.1.0", "vinyl-named": "^1.1.0",
"webdriverio": "^3.3.0",
"webpack": "^1.12.15", "webpack": "^1.12.15",
"webpack-stream": "^3.1.0" "webpack-stream": "^3.1.0"
}, },

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const path = require('path'); const path = require('path');
const webdriverio = require('webdriverio'); const Application = require('spectron').Application;
const source_root_dir = path.join(__dirname, '../..'); const source_root_dir = path.join(__dirname, '../..');
const electron_binary_path = (function() { const electron_binary_path = (function() {
@@ -16,26 +16,14 @@ const electron_binary_path = (function() {
const config_file_path = path.join(source_root_dir, 'test/test_config.json'); const config_file_path = path.join(source_root_dir, 'test/test_config.json');
const mattermost_url = 'http://example.com/team'; const mattermost_url = 'http://example.com/team';
var options = {
host: 'localhost', // Use localhost as chrome driver server
port: 9515, // "9515" is the port opened by chrome driver.
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
binary: electron_binary_path, // Path to your Electron binary.
args: ['app=' + path.join(source_root_dir, 'dist'), '--config-file=' + config_file_path] // Optional, perhaps 'app=' + /path/to/your/app/
}
}
};
module.exports = { module.exports = {
sourceRootDir: source_root_dir, sourceRootDir: source_root_dir,
configFilePath: config_file_path, configFilePath: config_file_path,
mattermostURL: mattermost_url, mattermostURL: mattermost_url,
spawnChromeDriver: function() { getSpectronApp: function() {
return require('child_process').spawn('node_modules/chromedriver/lib/chromedriver/chromedriver', ['--url-base=wd/hub', '--port=9515']); return new Application({
}, path: electron_binary_path,
getWebDriverIoClient: function() { args: [`${path.join(source_root_dir, 'dist')}`, '--config-file=' + config_file_path]
return webdriverio.remote(options); });
} }
} }

View File

@@ -9,49 +9,42 @@ const env = require('../modules/environment');
describe('application', function() { describe('application', function() {
this.timeout(10000); this.timeout(10000);
var chromedriver;
var client;
before(function(done) { before(function(done) {
chromedriver = env.spawnChromeDriver(); this.app = env.getSpectronApp();
client = env.getWebDriverIoClient(); fs.unlink(env.configFilePath, () => {
done()
fs.unlink(env.configFilePath, function(err) {
// waiting for chromedriver
setTimeout(done, 1000);
}); });
}); });
afterEach(function() { afterEach(function() {
return client.end(); if (this.app && this.app.isRunning()) {
}); return this.app.stop()
}
after(function() {
chromedriver.kill();
}); });
it('should show settings.html when there is no config file', function() { it('should show settings.html when there is no config file', function() {
return client return this.app.start().then(() => {
.init() this.app.client
.pause(1000) .pause(1000)
.getUrl().then(function(url) { .getUrl().then(function(url) {
var p = path.parse(url); var p = path.parse(url);
p.base.should.equal('settings.html'); p.base.should.equal('settings.html');
}) })
.end(); });
}); });
it('should show index.html when there is config file', function() { it('should show index.html when there is config file', function() {
fs.writeFileSync(env.configFilePath, JSON.stringify({ fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL url: env.mattermostURL
})); }));
return client return this.app.start().then(() => {
.init() this.app.client
.pause(1000) .pause(1000)
.getUrl().then(function(url) { .getUrl().then(function(url) {
var p = path.parse(url); var p = path.parse(url);
p.base.should.equal('index.html'); p.base.should.equal('index.html');
}) });
.end(); });
}); });
it('should upgrade v0 config file', function() { it('should upgrade v0 config file', function() {
@@ -59,17 +52,18 @@ describe('application', function() {
fs.writeFileSync(env.configFilePath, JSON.stringify({ fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL url: env.mattermostURL
})); }));
return client return this.app.start().then(() => {
.init() this.app.client
.pause(1000) .pause(1000)
.getUrl().then(function(url) { .getUrl().then(function(url) {
var p = path.parse(url); var p = path.parse(url);
p.base.should.equal('index.html'); p.base.should.equal('index.html');
}) })
.end().then(function() { .end().then(function() {
var str = fs.readFileSync(env.configFilePath, 'utf8'); var str = fs.readFileSync(env.configFilePath, 'utf8');
var config = JSON.parse(str); var config = JSON.parse(str);
config.version.should.equal(settings.version); config.version.should.equal(settings.version);
}); });
});
}); });
}); });

View File

@@ -20,15 +20,10 @@ describe('browser/index.html', function() {
}] }]
}; };
var chromedriver;
var client;
before(function(done) { before(function(done) {
chromedriver = env.spawnChromeDriver(); this.app = env.getSpectronApp();
client = env.getWebDriverIoClient();
fs.unlink(env.configFilePath, function(err) { fs.unlink(env.configFilePath, function(err) {
// waiting for chromedriver done();
setTimeout(done, 1000);
}); });
}); });
@@ -37,70 +32,72 @@ describe('browser/index.html', function() {
}); });
afterEach(function() { afterEach(function() {
return client.end(); if (this.app && this.app.isRunning()) {
}); return this.app.stop()
}
after(function() {
chromedriver.kill();
}); });
it('should NOT show tabs when there is one team', function() { it('should NOT show tabs when there is one team', function() {
fs.writeFileSync(env.configFilePath, JSON.stringify({ fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL url: env.mattermostURL
})); }));
return client return this.app.start().then(() => {
.init() this.app.client
.isExisting('#tabBar').then(function(isExisting) { .init()
isExisting.should.be.false(); .isExisting('#tabBar').then(function(isExisting) {
}) isExisting.should.be.false();
.end(); });
});
}); });
it('should set src of webview from config file', function() { it('should set src of webview from config file', function() {
return client return this.app.start().then(() => {
.init() this.app.client
.getAttribute('#mattermostView0', 'src').then(function(attribute) { .init()
attribute.should.equal(config.teams[0].url); .getAttribute('#mattermostView0', 'src').then(function(attribute) {
}) attribute.should.equal(config.teams[0].url);
.getAttribute('#mattermostView1', 'src').then(function(attribute) { })
attribute.should.equal(config.teams[1].url); .getAttribute('#mattermostView1', 'src').then(function(attribute) {
}) attribute.should.equal(config.teams[1].url);
.isExisting('#mattermostView2').then(function(isExisting) { })
isExisting.should.be.false(); .isExisting('#mattermostView2').then(function(isExisting) {
}) isExisting.should.be.false();
.end(); });
});
}); });
it('should set name of tab from config file', function() { it('should set name of tab from config file', function() {
return client return this.app.start().then(() => {
.init() this.app.client
.getText('#teamTabItem0').then(function(text) { .init()
text.should.equal(config.teams[0].name); .getText('#teamTabItem0').then(function(text) {
}) text.should.equal(config.teams[0].name);
.getText('#teamTabItem1').then(function(text) { })
text.should.equal(config.teams[1].name); .getText('#teamTabItem1').then(function(text) {
}) text.should.equal(config.teams[1].name);
.isExisting('#teamTabItem2').then(function(isExisting) { })
isExisting.should.be.false(); .isExisting('#teamTabItem2').then(function(isExisting) {
}) isExisting.should.be.false();
.end(); });
});
}); });
it('should show only the selected team', function() { it('should show only the selected team', function() {
return client return this.app.start().then(() => {
.init() this.app.client
.pause(1000) .init()
.waitForVisible('#mattermostView0', 1000) .pause(1000)
.isVisible('#mattermostView1').then(function(visility) { .waitForVisible('#mattermostView0', 1000)
visility.should.be.false(); .isVisible('#mattermostView1').then(function(visility) {
}) visility.should.be.false();
.click('#teamTabItem1') })
.pause(1000) .click('#teamTabItem1')
.waitForVisible('#mattermostView1', 1000) .pause(1000)
.isVisible('#mattermostView0').then(function(visility) { .waitForVisible('#mattermostView1', 1000)
visility.should.be.false(); .isVisible('#mattermostView0').then(function(visility) {
}) visility.should.be.false();
.end(); });
});
}); });
it('should show error when using incorrect URL', function() { it('should show error when using incorrect URL', function() {
@@ -112,9 +109,10 @@ describe('browser/index.html', function() {
url: 'http://false' url: 'http://false'
}] }]
})); }));
return client return this.app.start().then(() => {
.init() this.app.client
.waitForVisible('#mattermostView0-fail', 20000) .init()
.end(); .waitForVisible('#mattermostView0-fail', 20000)
});
}); });
}); });

View File

@@ -29,12 +29,9 @@ describe('browser/settings.html', function() {
var chromedriver; var chromedriver;
var client; var client;
before(function(done) { before(function(done) {
chromedriver = env.spawnChromeDriver(); this.app = env.getSpectronApp();
client = env.getWebDriverIoClient();
fs.unlink(env.configFilePath, function(err) { fs.unlink(env.configFilePath, function(err) {
// waiting for chromedriver done();
setTimeout(done, 1000);
}); });
}); });
@@ -43,50 +40,51 @@ describe('browser/settings.html', function() {
}); });
afterEach(function() { afterEach(function() {
return client.end(); if (this.app && this.app.isRunning()) {
}); return this.app.stop()
}
after(function() {
chromedriver.kill();
}); });
it('should show index.thml when Cancel button is clicked', function() { it('should show index.thml when Cancel button is clicked', function() {
return initClient(client) return this.app.start().then(() => {
.waitForExist('#btnCancel') initClient(this.app.client)
.click('#btnCancel') .waitForExist('#btnCancel')
.pause(1000) .click('#btnCancel')
.getUrl().then(function(url) { .pause(1000)
var url_split = url.split('/'); .getUrl().then(function(url) {
url_split[url_split.length - 1].should.equal('index.html'); var url_split = url.split('/');
}) url_split[url_split.length - 1].should.equal('index.html');
.end(); });
});
}); });
it('should show index.thml when Save button is clicked', function() { it('should show index.thml when Save button is clicked', function() {
return initClient(client) return this.app.start().then(() => {
.waitForExist('#btnSave') initClient(this.app.client)
.click('#btnSave') .waitForExist('#btnSave')
.pause(1000) .click('#btnSave')
.getUrl().then(function(url) { .pause(1000)
var url_split = url.split('/'); .getUrl().then(function(url) {
url_split[url_split.length - 1].should.equal('index.html'); var url_split = url.split('/');
}) url_split[url_split.length - 1].should.equal('index.html');
.end(); });
});
}); });
describe('Options', function() { describe('Options', function() {
describe('Hide Menu Bar', function() { describe('Hide Menu Bar', function() {
it('should appear on win32 or linux', function() { it('should appear on win32 or linux', function() {
return initClient(client) return this.app.start().then(() => {
.isExisting('#inputHideMenuBar').then(function(isExisting) { initClient(this.app.client)
if (process.platform === 'win32' || process.platform === 'linux') { .isExisting('#inputHideMenuBar').then(function(isExisting) {
isExisting.should.be.true(); if (process.platform === 'win32' || process.platform === 'linux') {
} isExisting.should.be.true();
else { }
isExisting.should.be.false(); else {
} isExisting.should.be.false();
}) }
.end(); });
});
}); });
if (process.platform === 'win32' || process.platform === 'linux') { if (process.platform === 'win32' || process.platform === 'linux') {
@@ -96,24 +94,26 @@ describe('browser/settings.html', function() {
Object.assign(new_config, config); Object.assign(new_config, config);
new_config.hideMenuBar = v; new_config.hideMenuBar = v;
fs.writeFileSync(env.configFilePath, JSON.stringify(new_config)); fs.writeFileSync(env.configFilePath, JSON.stringify(new_config));
return initClient(client) return this.app.start().then(() => {
.isSelected('#inputHideMenuBar input').then(function(value) { initClient(this.app.client)
value.should.equal(v); .isSelected('#inputHideMenuBar input').then(function(value) {
}) value.should.equal(v);
.end(); });
});
}); });
}); });
it('should be saved as config.json', function() { it('should be saved as config.json', function() {
return initClient(client) return this.app.start().then(() => {
.click('#inputHideMenuBar input') initClient(this.app.client)
.click('#btnSave') .click('#inputHideMenuBar input')
.pause(1000) .click('#btnSave')
.then(function() { .pause(1000)
const saved_config = JSON.parse(fs.readFileSync(env.configFilePath, 'utf8')); .then(function() {
saved_config.hideMenuBar.should.be.true(); const saved_config = JSON.parse(fs.readFileSync(env.configFilePath, 'utf8'));
}) saved_config.hideMenuBar.should.be.true();
.end(); });
});
}); });
} }
}); });