spectron icon indicating copy to clipboard operation
spectron copied to clipboard

Spectron sometimes launches multiple instances of electron app

Open nikhil-zinjurde opened this issue 6 years ago • 22 comments

This is an issue that we are facing while testing our electron apps with Spectron. It seems to go into a recursive mode of launching multiple instances, along with multiple empty terminal windows as reported in issue #60 (Empty terminals display on Windows).

It is resulting in test failures on our test environment quite frequently. Moreover, #60 issue hinders us from choosing the app window by index. So, any help or resolution on these both issues is very much appreciated.

Here is a screen capture of the issue.

spectron_multiple_app_instances_flv

nikhil-zinjurde avatar Apr 07 '18 12:04 nikhil-zinjurde

Same issue as #60

bksantiago avatar Apr 17 '18 07:04 bksantiago

@bksantiago not really. Yes, empty terminal windows being launched is a part of the issue reported here. The other part of the issue is that the electron application itself is spawned multiple times on running tests.

nikhil-zinjurde avatar Apr 17 '18 13:04 nikhil-zinjurde

We're also experiencing this issue while trying to set up simple Spectron tests for a VS Code extension - multiple instances of VS Code open up.

davidkpiano avatar Jun 20 '18 13:06 davidkpiano

@davidkpiano hi david, could u kindly share a simple example of Spectron tests for a VS Code extension ? It would be much appreciated. I am totally new to it, and google give me this page when I searched related keywords.

joywang17 avatar Dec 26 '18 13:12 joywang17

Any update on this issue?

JimmyMac6996 avatar Apr 03 '19 20:04 JimmyMac6996

@nikhil-zinjurde Did you/your team ever figure out what was causing this issue?

Jmiller3711 avatar Apr 10 '19 20:04 Jmiller3711

We managed to get around this issue by disabling Dev mode on our Electron app before we start execution. Since then, we haven't observed this issue.

nikhil-zinjurde avatar Apr 11 '19 08:04 nikhil-zinjurde

Just back-referencing why it's opening empty windows.

  • This commit: https://github.com/electron/spectron/commit/d74b5a0ddf500747bce30629a8be239db82edcd1 changed the launcher on windows to a .bat.
  • A .bat will always launch a command window.

SleeplessByte avatar Apr 11 '19 11:04 SleeplessByte

We managed to get around this issue by disabling Dev mode on our Electron app before we start execution. Since then, we haven't observed this issue.

@nikhil-zinjurde -- Do you mean launching the electron application with Developer Tools disabled?

JimmyMac6996 avatar Apr 11 '19 14:04 JimmyMac6996

We managed to get around this issue by disabling Dev mode on our Electron app before we start execution. Since then, we haven't observed this issue.

@nikhil-zinjurde -- Do you mean launching the electron application with Developer Tools disabled?

Yes, precisely. We provide electron application .exe path to app instance with new Application () constructor. Then, we make sure to disable Developer Tools. Then, we run the Spectron tests which launches the application (still with additional blank terminal windows). But, the issue of multple electron instances has gone way. The Spectron version we are using is 3.8.0, that works well with electron version of our application

nikhil-zinjurde avatar Apr 11 '19 18:04 nikhil-zinjurde

@nikhil-zinjurde By disabling the Developer Tools, did you just set devTools: false for the webPreferences of the BrowserWindow in the electron main.js?

Or did you do something via the spectron side of things?

Cause I've tried the devTools: false approach in my electron code and still running into 2 electron instances getting started.

willyt150 avatar Apr 17 '19 19:04 willyt150

We are testing with the following and having good luck so far:

app = new Application({ path: electronPath, chromeDriverArgs: ["--disable-extensions"], env: { SPECTRON: true, ELECTRON_ENABLE_LOGGING: true, ELECTRON_ENABLE_STACK_DUMPING: true }, });

JimmyMac6996 avatar Apr 17 '19 19:04 JimmyMac6996

Hey @JimmyMac6996 I appreciate your example. Did you have to do anything else to get that to work? I'm still getting the 2 windows behavior with updating my application creation to include the chromeDriverArgs.

Looking at the Main Process Logs (client.getMainProcessLogs()), immediately after application.start() completes, it's logging DevTools listening on ws://127.0.0.1:12172/devtools/browser/90a75d82-7cff-405d-af81-cb85178f84d8 and then eventually another DevTools listening on ws://127.0.0.1:12531/devtools/browser/9197cf46-f7e1-4fac-aa36-05c7ad432027.

The app 'ready' event fires twice, thus the 2 windows being created.

The logs make it seem like it's not honoring disabling the extensions.

Our Electron version is 4, so maybe that's why we're running into this issue?

willyt150 avatar Apr 18 '19 18:04 willyt150

Well... guess my logging should've clued me in. I decided to check the console of the first window that gets open, cause the main process logs were mentioning an error, and turns out the we were attempting to import the angular service worker module. This was left over from before we switched to using electron and we don't actually use it anywhere.

So removed the import and amazingly enough.... only ONE window!

I also removed the chromeDriverArgs and it's still working fine.

willyt150 avatar Apr 18 '19 19:04 willyt150

Our issues was not every time and sounds different, we are executing our tests from ADO/VSTS on a VM. 1 time out of 10 test runs, it would get hung up in the app.start code and never get back out, and would just keep opening our electron application over and over until there where so many instances the VM crashed from running out of memory/resources. Glad you tracked down your issue. :-)

JimmyMac6996 avatar Apr 18 '19 20:04 JimmyMac6996

I experienced this problem when using the incorrect version of spectron for my version of electron. wrong:

// package.json
{
  "devDependencies": {
    "spectron": "^8.0.0",
    "electron": "5.0.6"
  }
}

right:

// package.json
{
  "devDependencies": {
    "spectron": "^7.0.0",
    "electron": "5.0.6"
  }
}

Make sure you've got the right one installed.

sbrow avatar Aug 15 '19 01:08 sbrow

It's the good versions for me and it keeps opening windows.

DWboutin avatar Jan 26 '21 22:01 DWboutin

Again. It has nothing to do with the version, but the technique used to spawn the process, as mentioned here https://github.com/electron-userland/spectron/issues/279#issuecomment-482069778.

SleeplessByte avatar Jan 27 '21 01:01 SleeplessByte

@SleeplessByte it's not command windows, but Electron app windows who are spawned for me. https://github.com/electron-userland/spectron/issues/843

DWboutin avatar Jan 27 '21 13:01 DWboutin

If it's a different issue, you shouldn't be commenting here. If it's the same issue, my comment stands.

This issue might be interesting to you: #60, and in particular this comment: https://github.com/electron-userland/spectron/issues/60#issuecomment-544201517

SleeplessByte avatar Jan 27 '21 23:01 SleeplessByte

I think that the reason for this issue is that testing suites are executed in different electron instances: https://webdriver.io/blog/2021/03/23/grouping-specs/

sytolk avatar May 07 '21 07:05 sytolk

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

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

const Application = require('spectron').Application const assert = require('assert') const electronPath = require('electron') // Require Electron from the binaries included in node_modules. const path = require('path')

describe('Application launch', function () { this.timeout(10000)

beforeEach(function () {
    this.app = new Application({
        // Your electron path can be any binary
        // i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
        // But for the sake of the example we fetch it from our node_modules.
        path: path.join(__dirname, '..', 'node_modules', '.bin', 'electron' + (process.platform === 'win32' ? '.cmd' : '')),

        // Assuming you have the following directory structure

        //  |__ my project
        //     |__ ...
        //     |__ main.js
        //     |__ package.json
        //     |__ index.html
        //     |__ ...
        //     |__ test
        //        |__ spec.js  <- You are here! ~ Well you should be.

        // The following line tells spectron to look and use the main.js file
        // and the package.json located 1 level above.
        args: [path.join(__dirname, '..')],
        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']
    })
    return this.app.start()
})

afterEach(function () {
    if (this.app && this.app.isRunning()) {
        return this.app.stop()
    }
})

it('shows an initial window', function () {
    return this.app.client.getWindowCount().then(function (count) {
        assert.equal(count, 1)
        // Please note that getWindowCount() will return 2 if `dev tools` are opened.
        // assert.equal(count, 2)
    })
})

})

majedur-rahaman avatar Jul 29 '21 09:07 majedur-rahaman