electron-userland / spectron

DEPRECATED: 🔎 Test Electron apps using ChromeDriver
http://electronjs.org/spectron
MIT License
1.68k stars 228 forks source link

app.electron, app.webContents, etc.. undefined even if nodeIntegration is set to true #254

Open remipassmoilesel opened 6 years ago

remipassmoilesel commented 6 years ago

Hello,

Thanks for your great work on spectron. I'm trying to setup spectron in my project but even if nodeIntegration is set to true, app.electron and other variables are undefined.

You can reproduce this issue in few minutes:

$ git clone https://github.com/remipassmoilesel/abc-map-2
$ cd abc-map-2
$ npm i
$ npm run test

Here is my test code:

https://github.com/remipassmoilesel/abc-map-2/blob/master/src/tests/api/handlers/MapHandlersTest.ts

And here is the electron init script:

https://github.com/remipassmoilesel/abc-map-2/blob/master/src/electron-main.ts

And if I log the application object we can see that nodeIntegration value is set to false:

app {                                                                
  "host": "127.0.0.1",                                                                                                                                                                                             
  "port": 9515,                                     
  "quitTimeout": 1000,                              
  "startTimeout": 5000,                             
  "waitTimeout": 5000,                                                                                   
  "connectionRetryCount": 10,                                                                            
  "connectionRetryTimeout": 30000,                  
  "nodePath": "/usr/local/bin/node",                                                                     
  "path": "/home/remipassmoilesel/projects/abc-map-2/node_modules/electron/dist/electron",               
  "args": [                                                                                              
    "/home/remipassmoilesel/projects/abc-map-2/dist/electron-main.js"                                    
  ],                                                                                                     
  "chromeDriverArgs": [],                                                                                
  "env": {},                                                                                             
  "workingDirectory": "/home/remipassmoilesel/projects/abc-map-2",                                       
  "requireName": "require",                                                                                                                                                                                        
  "api": {                                                                                               
    "app": "~",                                                                                          
    "requireName": "require",                                                                                                                                                                                      
    "nodeIntegration": false                                                                                                                                                                                       
  },                                        
  ...

What can I do to fix this ? Thanks for your help.

warpdesign commented 5 years ago

I am having the exact same problem: electron isn't set for me too.

I also get weird stuff like this.app.client.getWindowCount() returning 3 although I should only have two windows (1 app window, 1 devtools).

So I wanted to use electron to compare but noticed it was undefined.

warpdesign commented 5 years ago

It seems to be related to the devtools. Commenting this code fixes the problem:

    mainWindow.webContents.setDevToolsWebContents(devtools.webContents);
    mainWindow.webContents.openDevTools({ mode: 'detach' });

Especially, the call to openDevTools seems to be the problem, if I comment only this line, app.electron gets defined as expected. If I put it back, app.electron is undefined.

Versions used:

remipassmoilesel commented 5 years ago

Hi @warpdesign, great news ! Unfortunately, my code is not available (it's an old issue) but I think your solution is related to my problem as I was openning devtools too.

agirorn commented 5 years ago

I have got this issue and no devTools when running

But when I used an older version of electron (v4.1.4) it worked.

I think it is related to the app.clien also not being set.

package.json

 {
   "dependencies": {
    "electron": "^5.0.0",
    "electron-debug": "^2.2.0",
    "electron-default-menu": "^1.0.1"
  },
  "devDependencies": {
    "chai": "^4.2.0",
    "electron-packager": "^12.1.0",
    "mocha": "^6.1.4",
    "require-dir": "^1.2.0",
    "spectron": "^5.0.0"
  }
}

test

const electron = require('electron');
const { Application } = require('spectron');

describe('Launch', function () {
  let app;

  beforeEach(() => {
    this.timeout(10000);
    app = new Application({
      path: electron,
      args: ['src/index.js'],
      startTimeout: 10000,
      waitTimeout: 10000,
    });

    return app.start()
      .then(() => {
        console.log('==> app.electron', app.electron);
        return app.client.waitUntilWindowLoaded();
      });
  });

  afterEach(() => {
    this.timeout(50000);

    if (app && app.isRunning()) {
      return app.stop();
    }
    return Promise.resolve();
  });

  it('should run this test', function () {
    this.timeout(50000);
    console.log('Now we can start testing the app');
    return new Promise((resolve) => {
      setTimeout(resolve, 1000);
    });
  });
});

Shell Output

./node_modules/.bin/mocha --timeout=100000 test/e2e
  Launch
==> app.electron undefined
    1) "before each" hook for "should run this test"

  0 passing (3s)
  1 failing

  1) Launch
       "before each" hook for "should run this test":
     waitUntilWindowLoaded Cannot read property 'isLoading' of undefined
  TypeError: Cannot read property 'isLoading' of undefined
      at Object.<anonymous> (node_modules/spectron/lib/application.js:230:31)
      at Timer.tick (node_modules/webdriverio/build/lib/utils/Timer.js:111:31)
      at Timer.start (node_modules/webdriverio/build/lib/utils/Timer.js:75:22)
      at new Timer (node_modules/webdriverio/build/lib/utils/Timer.js:62:14)
      at Object.exports.default (node_modules/webdriverio/build/lib/commands/waitUntil.js:25:17)
      at Object.exec (node_modules/webdriverio/build/lib/helpers/safeExecute.js:28:24)
      at Object.resolve (node_modules/webdriverio/build/lib/webdriverio.js:190:29)
      at /Users/agir/code/spectron-test-menu/example/node_modules/webdriverio/build/lib/webdriverio.js:485:32
      at _fulfilled (node_modules/q/q.js:854:54)
      at /Users/agir/code/spectron-test-menu/example/node_modules/q/q.js:883:30
      at Promise.promise.promiseDispatch (node_modules/q/q.js:816:13)
      at /Users/agir/code/spectron-test-menu/example/node_modules/q/q.js:570:49
      at runSingle (node_modules/q/q.js:137:13)
      at flush (node_modules/q/q.js:125:13)
      at processTicksAndRejections (internal/process/task_queues.js:79:9)
ajantiss commented 5 years ago

I get the same issue as @agirorn , it seems to work with Electron 5.0.1, at least on my local machine, I can't get it work on a Jenkins build so far....

abettadapur commented 5 years ago

Any updates on this problem? I can reproduce with electron 4.1.4 and spectron 5. I removed all instances of devtools and made sure nodeintegration was set

sindresorhus commented 5 years ago

I'm seeing this too just using the examples in the readme.

florin05 commented 5 years ago

Working sample with Electron 6 https://github.com/florin05/electron-spectron-example

Momotoculteur commented 4 years ago

Hi, i'm using electron 7.1.9 with spectron 9.0.0 and have some undefinied and weird issues about app.electron, etc, with a multiple app instance opening.

By testing some stuff, i realize that all is working great when i comment in the main.js file the devtool :

/* win.webContents.openDevTools({ mode: 'detach' }); */

wk989898 commented 4 years ago

i solved it by add env

//main.js
if (process.env.NODE_ENV === 'test') win.webContents.closeDevTools()
//test.js
process.env.NODE_ENV='test'