twolfson / karma-electron

Karma launcher and preprocessor for Electron
The Unlicense
59 stars 21 forks source link

Inject global variables into main process #12

Closed dylangmiles closed 8 years ago

dylangmiles commented 8 years ago

Hello,

In the main process of my app I have a global variable:

const navigator = require('./navigator.js');
.
.
app.on('ready', function() {
    //Setup global references
    global.navigator = navigator;

In my render process in index.html I use:

const navigator = remote.getGlobal('navigator');

I am trying to bootstrap the global variable so that I can test my page in karma-electron, but since it runs in the render process I am struggling. Can you suggest a strategy for injecting a global variable into the launcher from my test?

Thank you.

twolfson commented 8 years ago

Can you elaborate on why you need support for this?

dylangmiles commented 8 years ago

The navigator contains shared state across multiple pages. Each page builds a navigation menu from data in the navigator and also asks the navigator to change load a different page when a menu linked is clicked.

So it makes sense to me at least that the navigator should be created in the main process so that each page has access to the shared state via remote.getGlobal('navigator')

Thinking out loud: If I create the navigator on the render process then my page can't get access to it through remote.getGlobal. In other words I would need different way of accessing the variable for testing vs when running the app. I have yet to find a way to do this.

I would also be creating the navigator on the render process which does not reflect how it will be created in the main process when running the app.

twolfson commented 8 years ago

I don't think karma-electron is best suited for your needs. It's more built for testing single page applications but not how they interact with browser window menus and OS level interactions (e.g. saving application state). The reason is anything application level will wind up being shared by the karma-electron harness which might not properly clear state between test runs.

For browser window menus and OS level interactions, electron has tools like spectron and electron-chromedriver:

https://github.com/electron/spectron

https://github.com/electron/chromedriver

https://github.com/electron/electron/blob/v1.3.6/docs/tutorial/using-selenium-and-webdriver.md

The low level difference between these is: