Electron-308 - Added electron-chromedriver as dev dependency as it is

required to run Spectron tests

Electron-308 - Updated the code as per PR review

Electron-308

1. Changed args name
2. Fixed copy config method

Electron-308 - Optimized code and added error logs
This commit is contained in:
Kiran Niranjan 2018-02-19 13:07:30 +05:30 committed by kiranniranjan
parent bd48118b89
commit 01ecf8c065
12 changed files with 163 additions and 80 deletions

View File

@ -87,6 +87,7 @@
"electron": "^1.8.2",
"electron-builder": "^13.9.0",
"electron-builder-squirrel-windows": "^12.3.0",
"electron-chromedriver": "^1.8.0",
"electron-packager": "^8.5.2",
"electron-rebuild": "^1.5.7",
"eslint": "^3.16.1",

View File

@ -1,6 +1,7 @@
const Application = require('./spectronSetup');
const {isMac} = require('../../js/utils/misc.js');
const childProcess = require('child_process');
const constants = require('./spectronConstants');
let app = new Application({});
let robot;
@ -20,9 +21,15 @@ describe('Tests for Always on top', () => {
getConfigPath().then((config) => {
configPath = config;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_GET_USER_CONFIG_PATH, err);
expect(err).toBeNull();
done();
});
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});
});
@ -34,8 +41,8 @@ describe('Tests for Always on top', () => {
return require('electron').remote.app.getPath('userData');
})
});
app.client.getUserDataPath().then((path) => {
resolve(path.value + '/Symphony.config')
app.client.getUserDataPath().then((userConfigPath) => {
resolve(userConfigPath.value)
}).catch((err) => {
reject(err);
});
@ -136,6 +143,8 @@ describe('Tests for Always on top', () => {
robot.setMouseDelay(200);
robot.moveMouse(190, 0);
robot.mouseClick();
// Key tap 10 times as "Always on Top" is in the
// 10th position under view menu item
for (let i = 0; i < 10; i++) {
robot.keyTap('down');
}
@ -151,6 +160,8 @@ describe('Tests for Always on top', () => {
robot.moveMouseSmooth(x, y);
robot.mouseClick();
// Key tap 4 times as "Always on Top" is in the
// 4th position under window menu item
for (let i = 0; i < 4; i++) {
robot.keyTap('down');
}

View File

@ -1,4 +1,6 @@
const Application = require('./spectronSetup');
const constants = require('./spectronConstants');
let app = new Application({});
describe('Tests for Bring to front', () => {
@ -11,7 +13,9 @@ describe('Tests for Bring to front', () => {
app = startedApp;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});

View File

@ -1,5 +1,7 @@
const Application = require('./spectronSetup');
const path = require('path');
const constants = require('./spectronConstants');
let app = new Application({});
describe('Tests for clipboard', () => {
@ -11,6 +13,10 @@ describe('Tests for clipboard', () => {
return app.startApplication().then((startedApp) => {
app = startedApp;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});

View File

@ -1,18 +1,21 @@
const Application = require('./spectronSetup');
const constants = require('./spectronConstants');
let app = new Application({});
describe('Tests for Close', () => {
let originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 90000;
let app;
beforeAll((done) => {
const Application = require('./spectronSetup');
app = new Application({});
return app.startApplication().then((startedApp) => {
app = startedApp;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});

View File

@ -1,11 +1,10 @@
const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const Application = require('./spectronSetup');
const {isMac} = require('../../js/utils/misc');
const { isMac } = require('../../js/utils/misc');
const constants = require('./spectronConstants');
let robot;
let configPath;
let app = new Application({});
describe('Tests for Full screen', () => {
@ -22,10 +21,14 @@ describe('Tests for Full screen', () => {
configPath = config;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_GET_USER_CONFIG_PATH, err);
expect(err).toBeNull();
done();
});
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});
});
@ -37,8 +40,8 @@ describe('Tests for Full screen', () => {
return require('electron').remote.app.getPath('userData');
})
});
app.client.getUserDataPath().then((path) => {
resolve(path.value + '/Symphony.config')
app.client.getUserDataPath().then((userConfigPath) => {
resolve(userConfigPath.value)
}).catch((err) => {
reject(err);
});
@ -108,6 +111,9 @@ describe('Tests for Full screen', () => {
robot.moveMouseSmooth(205, 10);
robot.mouseClick();
robot.setKeyboardDelay(100);
// Key tap 8 times as "Enter Full Screen" is in the
// 8th position under view menu item
for (let i = 0; i < 8; i++) {
robot.keyTap('down');
}

View File

@ -1,11 +1,10 @@
const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const Application = require('./spectronSetup');
const {isMac} = require('../../js/utils/misc');
const { isMac } = require('../../js/utils/misc');
const constants = require('./spectronConstants');
let robot;
let configPath;
let app = new Application({});
describe('Tests for Minimize on Close', () => {
@ -22,10 +21,14 @@ describe('Tests for Minimize on Close', () => {
configPath = config;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_GET_USER_CONFIG_PATH, err);
expect(err).toBeNull();
done();
});
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});
});
@ -37,8 +40,8 @@ describe('Tests for Minimize on Close', () => {
return require('electron').remote.app.getPath('userData');
})
});
app.client.getUserDataPath().then((path) => {
resolve(path.value + '/Symphony.config')
app.client.getUserDataPath().then((userConfigPath) => {
resolve(userConfigPath.value)
}).catch((err) => {
reject(err);
});
@ -124,6 +127,8 @@ describe('Tests for Minimize on Close', () => {
robot.mouseClick();
robot.setKeyboardDelay(100);
// Key tap 9 times as "Minimize on Close" is in the
// 9th position under view menu item
for (let i = 0; i < 9; i++) {
robot.keyTap('down');
}
@ -148,6 +153,8 @@ describe('Tests for Minimize on Close', () => {
let y = bounds.y + 35;
robot.moveMouse(x, y);
robot.mouseClick();
// Key tap 5 times as "Minimize on Close" is in the
// 5th position under Window menu item
for (let i = 0; i < 5; i++) {
robot.keyTap('down');
}

View File

@ -1,6 +1,8 @@
const Application = require('./spectronSetup');
const path = require('path');
const {isMac} = require('../../js/utils/misc');
const { isMac } = require('../../js/utils/misc');
const constants = require('./spectronConstants');
let app = new Application({});
describe('Tests for Notification position', () => {
@ -13,7 +15,9 @@ describe('Tests for Notification position', () => {
app = startedApp;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});

View File

@ -0,0 +1,14 @@
module.exports = {
SYMPHONY_CONFIG_FILE_NAME: "/Symphony.config",
ELECTRON_GLOBAL_CONFIG_PATH_MAC: "node_modules/electron/dist/Electron.app/Contents/config",
ELECTRON_GLOBAL_CONFIG_PATH_WIN: "node_modules/electron/dist/config",
SEARCH_LIBRARY_PATH_MAC: "node_modules/electron/dist/Electron.app/Contents/library",
SEARCH_LIBRARY_PATH_WIN: "node_modules/electron/dist/library",
UNABLE_TO_START_APPLICATION: "Unable to start application error: ",
UNABLE_TO_GET_USER_CONFIG_PATH: "Unable to get user config path error: "
};

View File

@ -1,8 +1,9 @@
const Application = require('spectron').Application;
const path = require('path');
const fs = require('fs');
const {isMac} = require('../../js/utils/misc');
const { isMac, isWindowsOS } = require('../../js/utils/misc');
const ncp = require('ncp').ncp;
const constants = require('./spectronConstants.js');
class App {
@ -15,8 +16,16 @@ class App {
this.options.args = [path.join(__dirname, '..', '..', 'js/main.js')];
}
App.copyConfigPath();
App.copyLibraryDir();
if (isMac) {
App.copyConfigPath(constants.ELECTRON_GLOBAL_CONFIG_PATH_MAC);
App.copyLibraries(constants.SEARCH_LIBRARY_PATH_MAC);
}
if (isWindowsOS) {
App.copyConfigPath(constants.ELECTRON_GLOBAL_CONFIG_PATH_WIN);
App.copyLibraries(constants.SEARCH_LIBRARY_PATH_WIN);
}
this.app = new Application(this.options);
}
@ -25,6 +34,7 @@ class App {
return this.app.start().then((app) => {
return app;
}).catch((err) => {
throw new Error("Unable to start application " + err);
});
}
@ -42,54 +52,63 @@ class App {
static readConfig(configPath) {
if (!fs.existsSync(configPath)) {
return this.copyConfigPath();
} else {
return new Promise(function (resolve) {
fs.readFile(configPath, 'utf-8', function (err, data) {
if (err) {
throw new Error("Unable to read user config file " + err);
}
resolve(JSON.parse(data));
const configFilePath = configPath + constants.SYMPHONY_CONFIG_FILE_NAME;
if (!fs.existsSync(configFilePath)) {
return new Promise(function (resolve, reject) {
App.copyConfigPath(configPath).then(() => {
fs.readFile(configFilePath, 'utf-8', function (err, data) {
if (err) {
throw new Error(`Unable to read user config file at ${configFilePath} ${err}`);
}
let parsedData;
try {
parsedData = JSON.parse(data);
} catch (err) {
return reject(err);
}
return resolve(parsedData);
});
});
});
}
return new Promise(function (resolve, reject) {
fs.readFile(configFilePath, 'utf-8', function (err, data) {
if (err) {
throw new Error(`Unable to read user config file at ${configFilePath} ${err}`);
}
let parsedData;
try {
parsedData = JSON.parse(data);
} catch (err) {
reject(err);
}
resolve(parsedData);
});
});
}
static copyConfigPath() {
static copyConfigPath(configPath) {
return new Promise((resolve) => {
if (isMac) {
ncp('config', 'node_modules/electron/dist/Electron.app/Contents/config', function (err) {
if (err) {
throw new Error("Unable to copy config file to Electron dir " + err);
}
resolve();
});
} else {
ncp('config', 'node_modules/electron/dist/config', function (err) {
if (err) {
throw new Error("Unable to copy config file to Electron dir " + err);
}
resolve();
});
}
ncp('config', configPath, function (err) {
if (err) {
throw new Error("Unable to copy config file to Electron dir " + err);
}
return resolve();
});
})
}
static copyLibraryDir() {
if (isMac) {
ncp('library', 'node_modules/electron/dist/Electron.app/Contents/library', function (err) {
static copyLibraries(libraryPath) {
return new Promise((resolve) => {
return ncp('library', libraryPath, function (err) {
if (err) {
throw new Error("Unable to copy Swift search library dir " + err);
throw new Error("Unable to copy Swift search Libraries " + err);
}
return resolve();
});
} else {
ncp('library', 'node_modules/electron/dist/library', function (err) {
if (err) {
throw new Error("Unable to copy Swift search library dir " + err);
}
});
}
});
}
}

View File

@ -2,6 +2,8 @@ const Application = require('./spectronSetup');
const path = require('path');
const {isMac} = require('../../js/utils/misc.js');
const childProcess = require('child_process');
const constants = require('./spectronConstants');
let app = new Application({});
let robot;
@ -11,11 +13,15 @@ describe('Tests for spellChecker', () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = Application.getTimeOut();
beforeAll((done) => {
childProcess.exec(`npm rebuild robotjs --target=${process.version} --build-from-source`, function () {
childProcess.exec(`npm rebuild robotjs --target=${process.version} --build-from-source`, () => {
robot = require('robotjs');
app.startApplication().then((startedApp) => {
app = startedApp;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});
});
@ -78,35 +84,33 @@ describe('Tests for spellChecker', () => {
it('should invoke context menu ', (done) => {
if (isMac) {
app.browserWindow.getBounds().then((bounds) => {
let x = bounds.x + 55;
let y = bounds.y + 430;
const tag = app.client.$('#tag');
tag.waitForExist('#tag', 2000);
tag.moveToObject('#tag', 10, 10);
tag.rightClick('#tag', 10, 10);
robot.moveMouseSmooth(x, y);
robot.setMouseDelay(200);
robot.mouseClick('left', true);
robot.mouseClick('right');
// Timeout is required for context menu to appear
setTimeout(() => {
robot.setKeyboardDelay(500);
robot.keyTap('down');
robot.keyTap('down');
robot.keyTap('enter');
done();
});
}, 2000);
} else {
app.browserWindow.getBounds().then((bounds) => {
let x = bounds.x + 55;
let y = bounds.y + 430;
const tag = app.client.$('#tag');
tag.waitForExist('#tag', 2000);
tag.moveToObject('#tag', 10, 10);
tag.rightClick('#tag', 10, 10);
robot.moveMouseSmooth(x, y);
robot.setMouseDelay(200);
robot.mouseClick('left', true);
robot.mouseClick('right');
// Timeout is required for context menu to appear
setTimeout(() => {
robot.setKeyboardDelay(500);
robot.keyTap('down');
robot.keyTap('down');
robot.keyTap('enter');
done();
});
}, 2000);
}
});

View File

@ -1,8 +1,8 @@
const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const Application = require('./spectronSetup');
const {isMac} = require('../../js/utils/misc');
const { isMac } = require('../../js/utils/misc');
const constants = require('./spectronConstants');
let robot;
let configPath;
@ -22,10 +22,14 @@ describe('Tests for Zoom in and Zoom out', () => {
configPath = config;
done();
}).catch((err) => {
console.error(constants.UNABLE_TO_GET_USER_CONFIG_PATH, err);
expect(err).toBeNull();
done();
});
}).catch((err) => {
console.error(constants.UNABLE_TO_START_APPLICATION, err);
expect(err).toBeNull();
done();
});
});
});
@ -37,8 +41,8 @@ describe('Tests for Zoom in and Zoom out', () => {
return require('electron').remote.app.getPath('userData');
})
});
app.client.getUserDataPath().then((path) => {
resolve(path.value + '/Symphony.config')
app.client.getUserDataPath().then((userConfigPath) => {
resolve(userConfigPath.value)
}).catch((err) => {
reject(err);
});