Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spectron sometimes launches multiple instances of electron app #279

Open
nikhil-zinjurde opened this issue Apr 7, 2018 · 22 comments
Open

Comments

@nikhil-zinjurde
Copy link

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

@bksantiago
Copy link

Same issue as #60

@nikhil-zinjurde
Copy link
Author

@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.

@davidkpiano
Copy link

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.

@joywang17
Copy link

@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.

@JimmyMac6996
Copy link

Any update on this issue?

@Jmiller3711
Copy link

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

@nikhil-zinjurde
Copy link
Author

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.

@SleeplessByte
Copy link

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

@JimmyMac6996
Copy link

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?

@nikhil-zinjurde
Copy link
Author

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

@willyt150
Copy link

@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.

@JimmyMac6996
Copy link

JimmyMac6996 commented Apr 17, 2019

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
},
});

@willyt150
Copy link

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
Copy link

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.

@JimmyMac6996
Copy link

JimmyMac6996 commented Apr 18, 2019

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. :-)

@sbrow
Copy link

sbrow commented Aug 15, 2019

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.

@DWboutin
Copy link

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

@SleeplessByte
Copy link

Again. It has nothing to do with the version, but the technique used to spawn the process, as mentioned here #279 (comment).

@DWboutin
Copy link

@SleeplessByte it's not command windows, but Electron app windows who are spawned for me. #843

@SleeplessByte
Copy link

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: #60 (comment)

@sytolk
Copy link

sytolk commented May 7, 2021

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/

@majedur-rahaman
Copy link

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)
    })
})

})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests