Skip to content

Commit fb4d484

Browse files
authored
Merge pull request #52 from oslabs-beta/staging
Electron test fixes
2 parents 9ec0710 + c2d1d0c commit fb4d484

File tree

10 files changed

+224
-89
lines changed

10 files changed

+224
-89
lines changed

main.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,9 @@ function createWindow() {
194194
// allowRunningInsecureContent: true,
195195
webPreferences: {
196196
nodeIntegration: false,
197-
contextIsolation: true,
198-
enableRemoteModule: false,
199-
sandbox: true,
197+
contextIsolation: (process.env.NODE_ENV !== 'test'),
198+
// enableRemoteModule: false,
199+
sandbox: (process.env.NODE_ENV !== 'test'),
200200
webSecurity: true,
201201
preload: path.resolve(__dirname, "preload.js"),
202202
},
@@ -245,7 +245,7 @@ function createWindow() {
245245
mainWindow.once("ready-to-show", () => {
246246
mainWindow.show();
247247
// Open the DevTools automatically if developing
248-
if (isDev) {
248+
if (isDev && process.env.NODE_ENV !== 'test') {
249249
mainWindow.webContents.openDevTools();
250250
}
251251
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"repository": "https://github.com/open-source-labs/Swell",
77
"scripts": {
88
"test-jest": "jest",
9-
"test-mocha": "webpack --mode=production --config ./webpack.production.js && SET NODE_ENV=test & mocha",
9+
"test-mocha": "webpack --mode=production --config ./webpack.production.js && cross-env process.env.NODE_ENV=test mocha",
1010
"build": "webpack --mode=production --config ./webpack.production.js",
1111
"dev": "webpack-dev-server --mode=development --config ./webpack.development.js",
1212
"prod": "webpack --mode production --config ./webpack.production.js && electron --noDevServer .",

preload.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { ipcRenderer, contextBridge } = require("electron");
22

3-
contextBridge.exposeInMainWorld("api", {
3+
const apiObj = {
44
send: (channel, ...data) => {
55
// allowlist channels SENDING to Main
66
const allowedChannels = [
@@ -22,8 +22,7 @@ contextBridge.exposeInMainWorld("api", {
2222
}
2323
},
2424
receive: (channel, cb) => {
25-
console.log("listening on channel : ", channel);
26-
// allowlist channels LISTENING
25+
// allowlist channels
2726
const allowedChannels = [
2827
"add-collection",
2928
"clear-history-response",
@@ -37,4 +36,19 @@ contextBridge.exposeInMainWorld("api", {
3736
ipcRenderer.on(channel, (event, ...args) => cb(...args));
3837
}
3938
},
40-
});
39+
}
40+
41+
// this is because we need to have context isolation to be false for spectron tests to run, but context bridge only runs if context isolation is true
42+
// basically we are assigning certain node functionality (require, ipcRenderer) to the window object in an UN-isolated context only for testing
43+
// security is reduced for testing, but remains sturdy otherwise
44+
if (process.env.NODE_ENV === 'test') {
45+
console.log('made it into here')
46+
window.electronRequire = require;
47+
window.api = apiObj;
48+
49+
} else {
50+
contextBridge.exposeInMainWorld("api", apiObj);
51+
}
52+
53+
54+

test/electronTests.js

Lines changed: 0 additions & 80 deletions
This file was deleted.

test/pageObjects/Sidebar.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const app = require('../testApp.js');
2+
3+
class Sidebar {
4+
5+
get url() {
6+
return app.client.$('input.composer_url_input');
7+
};
8+
9+
get requestMethod() {
10+
return app.client.$('select.composer_method_select.http');
11+
};
12+
13+
get choosePost() {
14+
return app.client.$('option=POST');
15+
};
16+
17+
get chooseGet(){
18+
return app.client.$('option=GET');
19+
};
20+
21+
get activateHeaders(){
22+
return app.client.$('.composer_subtitle=Headers');
23+
};
24+
25+
get headerCheckbox(){
26+
return app.client.$$('header_checkbox');
27+
};
28+
29+
get headerKey(){
30+
return app.client.$('.header_key');
31+
};
32+
33+
get headerValue(){
34+
return app.client.$('.header_value');
35+
}
36+
};
37+
38+
module.exports = new Sidebar();

test/subSuites/appOpens.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const assert = require("assert");
2+
const path = require("path");
3+
const fs = require("fs");
4+
const app = require('../testApp');
5+
6+
7+
module.exports = () => {
8+
9+
describe('App opens and renders a page', () => {
10+
describe("Browser Window Tests", () => {
11+
it("window is visible", async () => {
12+
const isVisible = await app.browserWindow.isVisible();
13+
return assert.equal(isVisible, true);
14+
});
15+
it("browser window title is 'Swell'", async () => {
16+
const titleWithBrowser = await app.browserWindow.getTitle();
17+
return assert.equal(titleWithBrowser, "Swell");
18+
});
19+
20+
it("Confirm browser window count is 1", async () => {
21+
const windowCount = await app.client.getWindowCount();
22+
return assert.equal(1, windowCount);
23+
});
24+
it("take a snapshot of app", async () => {
25+
const imageBuffer = await app.browserWindow.capturePage();
26+
fs.writeFileSync(path.resolve(__dirname, "snapshot.png"), imageBuffer);
27+
});
28+
});
29+
describe("DOM Tests", () => {
30+
it("html file title is 'Swell'", async () => {
31+
const titleWithClient = await app.client
32+
.waitUntilWindowLoaded()
33+
.getTitle(); // the dom set inside dist/index.html which is set inside webpack htmlPlugin
34+
return assert.equal(titleWithClient, "Swell");
35+
});
36+
37+
it("devTool should NOT open since we are in production mode", async () => {
38+
const isOpen = await app.browserWindow.isDevToolsOpened();
39+
return assert.equal(isOpen, false);
40+
});
41+
it("Sidebar exists", async () => {
42+
await app.client.waitUntilWindowLoaded();
43+
// $ is basically querySelector
44+
const sidebar = await app.client.$(".sidebar_composer-console");
45+
return assert.notEqual(sidebar.value, null);
46+
});
47+
it("Main Component exists", async () => {
48+
await app.client.waitUntilWindowLoaded();
49+
const content = await app.client.$(".contents");
50+
return assert.notEqual(content.value, null);
51+
});
52+
});
53+
});
54+
}

test/subSuites/historyTests.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
const assert = require('assert');
2+
const sideBar = require('../pageObjects/Sidebar.js');
3+
4+
5+
module.exports = () => {
6+
describe('CRUD/History functionality', function(){
7+
8+
describe('URL/request method inputs', () => {
9+
it('can type url into url input', async () => {
10+
await sideBar.url.addValue('pokeapi.co/api/v2/pokemon?limit=5');
11+
const input = await sideBar.url.getValue();
12+
13+
return assert.strictEqual(input, 'http://pokeapi.co/api/v2/pokemon?limit=5');
14+
});
15+
16+
it('can select a request method', async () => {
17+
await sideBar.requestMethod.click();
18+
await sideBar.choosePost.click();
19+
let isSelected = await sideBar.choosePost.isSelected();
20+
assert.strictEqual(isSelected, true);
21+
22+
await sideBar.requestMethod.click();
23+
await sideBar.chooseGet.click();
24+
isSelected = await sideBar.chooseGet.isSelected();
25+
assert.strictEqual(isSelected, true);
26+
});
27+
})
28+
29+
// describe('headers inputs', async () => {
30+
// let inputs;
31+
// let headerChecked;
32+
33+
// it('should open headers input, rendering single input at first', async () => {
34+
// await sideBar.activateHeaders.click();
35+
// inputs = await sideBar.headerCheckbox[0];
36+
// assert.strictEqual(inputs.length, 1);
37+
// });
38+
39+
// it('can type new headers in request', async () => {
40+
// await sideBar.headerKey.addValue('testing');
41+
// const headerKey = await sideBar.headerKey.getValue();
42+
// assert.strictEqual(headerKey, 'testing');
43+
44+
// await sideBar.headerValue.addValue('true');
45+
// const headerValue = await sideBar.headerValue.getValue();
46+
// assert.strictEqual(headerValue, 'true');
47+
48+
// });
49+
50+
// it('new headers initialize as checked', async () => {
51+
// headerChecked = await sideBar.headerCheckbox.isSelected();
52+
// assert.strictEqual(headerChecked, true);
53+
// });
54+
55+
// // NOTE : THIS WILL FAIL FOR NOW, THIS IS UI DETAIL THAT NEEDS TO BE IMPLEMENTED IN FUTURE
56+
// it('deleting text in input eliminates checkmark', async () => {
57+
// await sideBar.headerKey.clearValue();
58+
// await sideBar.headerValue.clearVAlue();
59+
// headerChecked = await sideBar.headerCheckbox.isSelected();
60+
// assert.strictEqual(headerChecked, false);
61+
// })
62+
// })
63+
64+
});
65+
}

test/subSuites/snapshot.png

47.7 KB
Loading

test/testApp.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { Application } = require("spectron");
2+
const electronPath = require("electron");
3+
const path = require("path");
4+
5+
6+
const TEST_MODE = "TEST_MODE";
7+
8+
const app = new Application({
9+
// Your electron path can be any binary
10+
// i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
11+
// But for the sake of the example we fetch it from our node_modules.
12+
requireName: 'electronRequire',
13+
path: electronPath,
14+
// The following line tells spectron to look and use the main.js file
15+
// and the package.json located 1 level above along with an arg, 'TEST_MODE'
16+
args: [path.join(__dirname, ".."), TEST_MODE],
17+
});
18+
19+
module.exports = app;

test/testSuite.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Integration test for electron for spectron
2+
// ****** use "npm run test-mocha" to run these tests ******
3+
4+
// import other tests
5+
const historyTests = require('./subSuites/historyTests');
6+
const appOpensTests = require('./subSuites/appOpens');
7+
8+
const app = require('./testApp');
9+
10+
describe("Electron Tests", function () {
11+
this.timeout(10000);
12+
before(function () {
13+
return app.start();
14+
});
15+
16+
after(function () {
17+
if (app && app.isRunning()) {
18+
return app.stop();
19+
}
20+
});
21+
22+
// these are are test suites within this broader suite
23+
appOpensTests();
24+
historyTests();
25+
});

0 commit comments

Comments
 (0)