Skip to content

Commit 9c9a76a

Browse files
committed
(#246) Added dedicated test to verify builds against Electron
1 parent 2430f6e commit 9c9a76a

File tree

18 files changed

+20263
-24835
lines changed

18 files changed

+20263
-24835
lines changed

Diff for: .build/build.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ echo "Entering working directory $targetDir"
2121
cd $targetDir
2222
echo "Installing node version $nodeVersion"
2323
nvm install $nodeVersion
24-
echo "npm run coverage"
24+
25+
npm ci
2526
E2E_TEST=1 npm run coverage -- --coverageDirectory=coverage/e2e

Diff for: e2e/tests/lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"packages": [
33
"packages/*"
44
],
5-
"version": "1.4.2"
5+
"version": "1.6.0"
66
}

Diff for: e2e/tests/package-lock.json

+9,207-22,166
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: e2e/tests/package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@
2222
},
2323
"homepage": "https://github.com/nut-tree/nut.js#readme",
2424
"devDependencies": {
25-
"electron": "13.1.6",
26-
"jest": "26.6.3",
27-
"lerna": "3.22.1",
28-
"spectron": "15.0.0",
29-
"@nut-tree/nut-js": "file:../../"
25+
"lerna": "3.22.1"
3026
},
3127
"dependencies": {
32-
"window-integration-tests": "file:packages/window-integration-tests"
28+
"window-integration-tests": "file:packages/window-integration-tests",
29+
"electron-tests": "file:packages/electron"
3330
}
3431
}

Diff for: e2e/tests/packages/electron/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# nut.js Electron sample
2+
3+
nut.js is compatible with both node and Electron runtimes.
4+
5+
- [Setup](#setup)
6+
7+
## Setup
8+
9+
Using nut.js with Electron requires compatible OpenCV bindings.
10+
11+
[electron-rebuild](https://www.npmjs.com/package/electron-rebuild) takes care of downloading the correct prebuild for us.
12+
13+
```shell script
14+
npm i -D electron-rebuild
15+
```
16+
17+
In our sample we call `electron-rebuild` in the `pretest` phase, so everything will be set up before we execute our demo.
18+
19+
Running `npm test` will spawn a new Electron application.
20+
21+
nut.js will search for the button displayed in the center of the window and click it, which will exit our application.
22+
23+
In case we're unable to locate the button, our application will run into a timeout and exit with non-zero exitcode.

Diff for: e2e/tests/packages/electron/assets/darwin/quit.png

3.27 KB
Loading

Diff for: e2e/tests/packages/electron/assets/linux/quit.png

9.75 KB
Loading

Diff for: e2e/tests/packages/electron/assets/win32/quit.png

3.27 KB
Loading

Diff for: e2e/tests/packages/electron/index.css

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
body {
2+
width: 100vw;
3+
height: 100vh;
4+
}
5+
6+
#content {
7+
display: flex;
8+
flex-direction: row;
9+
align-items: center;
10+
justify-content: center;
11+
height: 100vh;
12+
width: 100vw;
13+
}
14+
15+
#exit {
16+
color: white;
17+
font-size: 1.5rem;
18+
background: darkblue;
19+
}

Diff for: e2e/tests/packages/electron/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
6+
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
7+
<link href="index.css" rel="stylesheet"/>
8+
<title>Hello from nut.js!</title>
9+
</head>
10+
<body style="width: 100%; height: 100%">
11+
<div id="content">
12+
<button id="exit">Click me!</button>
13+
</div>
14+
<script src="renderer.js"></script>
15+
</body>
16+
</html>

Diff for: e2e/tests/packages/electron/main.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const {app, ipcMain, BrowserWindow} = require('electron')
2+
const {mouse, screen, straightTo, centerOf} = require("@nut-tree/nut-js");
3+
const path = require('path')
4+
5+
function createWindow() {
6+
const mainWindow = new BrowserWindow({
7+
width: 800,
8+
height: 600,
9+
webPreferences: {
10+
nodeIntegration: true,
11+
contextIsolation: false,
12+
preload: path.join(__dirname, 'preload.js')
13+
}
14+
})
15+
mainWindow.loadFile(path.join(__dirname, "index.html"))
16+
mainWindow.maximize();
17+
18+
(async () => {
19+
screen.config.resourceDirectory = `${__dirname}/assets/${process.platform}`;
20+
await mouse.move(straightTo(centerOf(screen.waitFor("quit.png", 10000))));
21+
await mouse.leftClick();
22+
})();
23+
}
24+
25+
ipcMain.on("main", (event, args) => {
26+
if (args === "quit") {
27+
app.quit();
28+
}
29+
});
30+
31+
app.whenReady().then(() => {
32+
setTimeout(() => process.exit(1), 15000);
33+
createWindow()
34+
35+
app.on('activate', function () {
36+
if (BrowserWindow.getAllWindows().length === 0) createWindow()
37+
})
38+
})
39+
40+
app.on('window-all-closed', function () {
41+
console.log("Bye!");
42+
app.quit();
43+
})

0 commit comments

Comments
 (0)