DEV Community

karthik22061993
karthik22061993

Posted on

Electron app opening multiple windows or processes in spectron test

My electron app works as expected but it keeps on opening new windows when I run spectron test that tests for number of windows opened.

Versions Installed
npm ls --depth=0 demoelectronapp@1.0.0 C:\demoCLS\demoElectronApp +-- assert@2.0.0 +-- electron@8.2.2 +-- electron-packager@14.2.1 +-- mocha@7.1.1 +-- path@0.12.7 `-- spectron@10.0.1

I am just running a small demo, below I will give code snippet

Note: I am running the spectron test pointing to .exe file generated by electron-packager.

Does Anyone have an idea what is the issue if possible how to solve it?

main.js
`
const { app, BrowserWindow } = require('electron')

function createWindow () {

let win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true
}
})

win.loadFile('index.html')
}

app.whenReady().then(createWindow)

`
**index.js**
`

<!DOCTYPE html>



Hello World!



Hello World!




`
**test.js**
`

const assert = require('assert');
const path = require('path');
const Application = require('spectron').Application;
const electronPath = require('electron');

const app = new Application({
path: 'C:/demoElectronApp/winx64/demoelectronapp-win32-x64/demoelectronapp.exe',
});

describe('client_settings_app', function () {
this.timeout(10000);

beforeEach(() => {
  return app.start();
});

it('shows only one initial window', async () => {
  const count = await app.client.getWindowCount();
  return assert.equal(count, 1);
});
Enter fullscreen mode Exit fullscreen mode

})

`
**package.json**
`

{
"name": "demoelectronapp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"electronpackage": "electron-packager . --out winx64"
},
"author": "",
"license": "ISC",
"dependencies": {
"path": "^0.12.7"
},
"devDependencies": {
"electron": "^8.2.2",
"electron-packager": "^14.2.1",
"assert": "^2.0.0",
"mocha": "^7.1.1",
"spectron": "^10.0.1"
}
}
`

Top comments (5)

Collapse
 
majed profile image
Majedur Rahaman

Okay, I was having the same issue. And solved by setting remote debugging port.

chromeDriverArgs: ['remote-debugging-port=9222']:

Necessary codes:

env: {
ELECTRON_ENABLE_LOGGING: true,
ELECTRON_ENABLE_STACK_DUMPING: true,
NODE_ENV: 'test'
},
waitTimeout: 10e3,
requireName: 'electronRequire',
chromeDriverLogPath: '../chromedriverlog.txt',
chromeDriverArgs: ['remote-debugging-port=9222']

For more info.
gitmemory.com/issue/electron-userl...

Collapse
 
karthik22061993 profile image
karthik22061993

Has anyone solved this issue?

Collapse
 
ladvace profile image
Gianmarco

I have the same problem but I didn't find any solution

Collapse
 
sarathyt profile image
Tarangini Sarathy

where you able to figure out a solution?

Collapse
 
elrond25 profile image
Carlos Araya

can you format the code better? I can't read the code the way it is