Skip to content

Commit

Permalink
Use Spectron to execute test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuya-oc committed May 21, 2016
1 parent 3e3faf9 commit b52dfa5
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 173 deletions.
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -31,7 +31,6 @@
"babel-core": "^6.7.5",
"babel-loader": "^6.2.4",
"babel-preset-react": "^6.5.0",
"chromedriver": "^2.20.0",
"del": "^2.2.0",
"electron-builder": "^3.11.0",
"electron-connect": "^0.3.7",
Expand All @@ -48,10 +47,10 @@
"mocha": "^2.3.4",
"mocha-circleci-reporter": "0.0.1",
"should": "^8.0.1",
"spectron": "~3.0.0",
"style-loader": "^0.13.0",
"through2": "^2.0.1",
"vinyl-named": "^1.1.0",
"webdriverio": "^3.3.0",
"webpack": "^1.12.15",
"webpack-stream": "^3.1.0"
},
Expand Down
24 changes: 6 additions & 18 deletions test/modules/environment.js
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const webdriverio = require('webdriverio');
const Application = require('spectron').Application;

const source_root_dir = path.join(__dirname, '../..');
const electron_binary_path = (function() {
Expand All @@ -16,26 +16,14 @@ const electron_binary_path = (function() {
const config_file_path = path.join(source_root_dir, 'test/test_config.json');
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 = {
sourceRootDir: source_root_dir,
configFilePath: config_file_path,
mattermostURL: mattermost_url,
spawnChromeDriver: function() {
return require('child_process').spawn('node_modules/chromedriver/lib/chromedriver/chromedriver', ['--url-base=wd/hub', '--port=9515']);
},
getWebDriverIoClient: function() {
return webdriverio.remote(options);
getSpectronApp: function() {
return new Application({
path: electron_binary_path,
args: [`${path.join(source_root_dir, 'dist')}`, '--config-file=' + config_file_path]
});
}
}
76 changes: 35 additions & 41 deletions test/specs/app_test.js
Expand Up @@ -9,67 +9,61 @@ const env = require('../modules/environment');
describe('application', function() {
this.timeout(10000);

var chromedriver;
var client;
before(function(done) {
chromedriver = env.spawnChromeDriver();
client = env.getWebDriverIoClient();

fs.unlink(env.configFilePath, function(err) {
// waiting for chromedriver
setTimeout(done, 1000);
this.app = env.getSpectronApp();
fs.unlink(env.configFilePath, () => {
done()
});
});

afterEach(function() {
return client.end();
});

after(function() {
chromedriver.kill();
if (this.app && this.app.isRunning()) {
return this.app.stop()
}
});

it('should show settings.html when there is no config file', function() {
return client
.init()
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('settings.html');
})
.end();
return this.app.start().then(() => {
this.app.client
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('settings.html');
})
});
});

it('should show index.html when there is config file', function() {
fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL
}));
return client
.init()
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('index.html');
})
.end();
return this.app.start().then(() => {
this.app.client
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('index.html');
});
});
});

it('should upgrade v0 config file', function() {
const settings = require('../../src/common/settings');
fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL
}));
return client
.init()
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('index.html');
})
.end().then(function() {
var str = fs.readFileSync(env.configFilePath, 'utf8');
var config = JSON.parse(str);
config.version.should.equal(settings.version);
});
return this.app.start().then(() => {
this.app.client
.pause(1000)
.getUrl().then(function(url) {
var p = path.parse(url);
p.base.should.equal('index.html');
})
.end().then(function() {
var str = fs.readFileSync(env.configFilePath, 'utf8');
var config = JSON.parse(str);
config.version.should.equal(settings.version);
});
});
});
});
118 changes: 58 additions & 60 deletions test/specs/browser/index_test.js
Expand Up @@ -20,15 +20,10 @@ describe('browser/index.html', function() {
}]
};

var chromedriver;
var client;
before(function(done) {
chromedriver = env.spawnChromeDriver();
client = env.getWebDriverIoClient();

this.app = env.getSpectronApp();
fs.unlink(env.configFilePath, function(err) {
// waiting for chromedriver
setTimeout(done, 1000);
done();
});
});

Expand All @@ -37,70 +32,72 @@ describe('browser/index.html', function() {
});

afterEach(function() {
return client.end();
});

after(function() {
chromedriver.kill();
if (this.app && this.app.isRunning()) {
return this.app.stop()
}
});

it('should NOT show tabs when there is one team', function() {
fs.writeFileSync(env.configFilePath, JSON.stringify({
url: env.mattermostURL
}));
return client
.init()
.isExisting('#tabBar').then(function(isExisting) {
isExisting.should.be.false();
})
.end();
return this.app.start().then(() => {
this.app.client
.init()
.isExisting('#tabBar').then(function(isExisting) {
isExisting.should.be.false();
});
});
});

it('should set src of webview from config file', function() {
return client
.init()
.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);
})
.isExisting('#mattermostView2').then(function(isExisting) {
isExisting.should.be.false();
})
.end();
return this.app.start().then(() => {
this.app.client
.init()
.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);
})
.isExisting('#mattermostView2').then(function(isExisting) {
isExisting.should.be.false();
});
});
});

it('should set name of tab from config file', function() {
return client
.init()
.getText('#teamTabItem0').then(function(text) {
text.should.equal(config.teams[0].name);
})
.getText('#teamTabItem1').then(function(text) {
text.should.equal(config.teams[1].name);
})
.isExisting('#teamTabItem2').then(function(isExisting) {
isExisting.should.be.false();
})
.end();
return this.app.start().then(() => {
this.app.client
.init()
.getText('#teamTabItem0').then(function(text) {
text.should.equal(config.teams[0].name);
})
.getText('#teamTabItem1').then(function(text) {
text.should.equal(config.teams[1].name);
})
.isExisting('#teamTabItem2').then(function(isExisting) {
isExisting.should.be.false();
});
});
});

it('should show only the selected team', function() {
return client
.init()
.pause(1000)
.waitForVisible('#mattermostView0', 1000)
.isVisible('#mattermostView1').then(function(visility) {
visility.should.be.false();
})
.click('#teamTabItem1')
.pause(1000)
.waitForVisible('#mattermostView1', 1000)
.isVisible('#mattermostView0').then(function(visility) {
visility.should.be.false();
})
.end();
return this.app.start().then(() => {
this.app.client
.init()
.pause(1000)
.waitForVisible('#mattermostView0', 1000)
.isVisible('#mattermostView1').then(function(visility) {
visility.should.be.false();
})
.click('#teamTabItem1')
.pause(1000)
.waitForVisible('#mattermostView1', 1000)
.isVisible('#mattermostView0').then(function(visility) {
visility.should.be.false();
});
});
});

it('should show error when using incorrect URL', function() {
Expand All @@ -112,9 +109,10 @@ describe('browser/index.html', function() {
url: 'http://false'
}]
}));
return client
.init()
.waitForVisible('#mattermostView0-fail', 20000)
.end();
return this.app.start().then(() => {
this.app.client
.init()
.waitForVisible('#mattermostView0-fail', 20000)
});
});
});

0 comments on commit b52dfa5

Please sign in to comment.