Testing menu interaction #21

Open
atdrago opened this Issue May 5, 2016 · 9 comments

Projects

None yet

5 participants

@atdrago
atdrago commented May 5, 2016

Love the framework! ๐Ÿ‘

Is there any way currently to test menu item functionality through spectron? I could test the code itself but that doesn't take into account menu item states like enabled and visible.

If not, is there a suggested strategy for testing menu items?

Thanks!

@kevinsawicki
Collaborator

If not, is there a suggested strategy for testing menu items?

Not currently, the require('electron').remote.Menu.getApplicationMenu() API does not serialize to JSON so it can't currently be fetched via Spectron.

This is something that should be added though ๐Ÿ‘

@navinjoy

@atdrago wondering how you test the menu functionality like click on the menu item?

If this functionality is added then that would be nice and looking forward for this implementation.

Thanks.

@atdrago
atdrago commented May 13, 2016

@navinjoy The way I did it is not great. It relies on there being a renderer process which makes it very difficult to do anything (besides app.restart()) when all windows are closed. But here's what I did. I'm open to suggestions if anyone knows a better way!

In the main process

  1. Inject some ipcMain listeners when NODE_ENV=development https://github.com/atdrago/negative/blob/develop/lib/negative.js#L47-L49
  2. The listeners call a simulateMenuClick function https://github.com/atdrago/negative/blob/develop/lib/negative-test.js#L35-L72
  3. simulateMenuClick checks if the menuItem is enabled and visible, and then calls its click handler if it has one https://github.com/atdrago/negative/blob/develop/lib/negative-test.js#L27-L29

In the tests
4. Use ipcRenderer to send menuItem messages to ipcMain. For example, testing "Zoom In":
https://github.com/atdrago/negative/blob/develop/test/test.js#L233-L255

A couple downfalls of this approach

  • As mentioned above, this fails when there are no windows (no renderer processes), so testing things that close and open new windows is painful
  • If the menuItem uses role or performSelector, there's no way to test it
@atdrago
atdrago commented May 28, 2016

I created a small demo app to show how I implemented my tests. Much easier to digest than looking at Negative. It's actually working quite well now, except for the fact that I still can't test menus that don't have a click handler. It's written for Node v4.4.4 and Electron v0.37.8. @navinjoy I hope this helps you and your team :)

https://github.com/atdrago/electron-testing-legacy

@navinjoy

@atdrago Thanks a lot for sharing this... we will try and let you know how it goes....have a great day ๐Ÿ‘

@kedunuru
kedunuru commented May 28, 2016 edited

thanks adam ...will try this and let u know how it goes ...this totally works !!! big thank you to adam !

@atdrago
atdrago commented May 29, 2016

Not currently, the require('electron').remote.Menu.getApplicationMenu() API does not serialize to JSON so it can't currently be fetched via Spectron.

@kevinsawicki Should I file a new issue under Electron for this?

@kevinsawicki
Collaborator

Should I file a new issue under Electron for this?

I don't think so, it isn't really an issue in Electron, many APIs may return objects that don't serialize to JSON.

It should probably be fixed here instead.

@greenmanspirit

Has there been an update on this? I has a preferences window in my app that is activated by a menu item and would like to be able to test it.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment