-
Notifications
You must be signed in to change notification settings - Fork 228
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
API to test global shortcuts #96
Comments
@rayrutjes This brings up something I mentioned in another issue somewhere.
// This is what you need to test
const doThing = () => {
// Do the thing
};
// This is not
globalShortcut.register('Ctrl+A', doThing); You should assume that the framework you are using (Electron) is tested correctly and test your code does what it should, not that globalShortcut does what it should. This is all just IMO but it makes sense to me 😆 So you shouldn't be mocking or simulating the shortcut. Simply call the function the shortcut is associated with. 👍 |
Thank you @MarshallOfSound for your answer.
I feel like this is what spectron is aimed at solving, right? If it isn't, would you be so kind as to suggest a case where spectron makes sense? I've seen tests where we test the size of the windows, but that brings us back to testing electron. Some insights about where spectron makes sense would be very much appreciated. |
I agree this would be useful, I'm dynamically creating global shortcuts. Testing the action that happens is done, but knowing those shortcuts work in combination and through the stack would be amazing. |
I couldn't get this integrated into spectron, but I did manage to get robotjs integrated into my test suite. Using |
I am facing the same problem. RobotJS is nice and does the job well, but I really don't want to add another dependency just to test a single feature of my app. Any chance for this to be implemented into Spectorn? |
@blainesch I really need this functionality - do you have an example of robotjs integrated with spectron so I can copy the setup? many thanks in advance ... |
thanks @blainesch - I have something working :-) |
what I notice though is that robotjs is hitting the computer generally rather than the electron app specifically, so if I start trying to do something else during the acceptance tests, then the change of window focus screws things up. Would really love to be able to send specific key presses to electron via chromedriver ... |
robotjs is not working for zazu, robotjs don't support node 12 at this time. I use this to press keys: hitKey (key, modifier) {
if (os.type() === 'Darwin' && !isTravis) {
const keyForAppleScript = key.length === 1 ? `\\"${key}\\"` : key
if (modifier) {
const modifierForAppleScript = modifier.replace('alt', 'option')
return exec(`Script="tell app \\"System Events\\" to keystroke ${keyForAppleScript} using ${modifierForAppleScript} down"
osascript -e "$Script"`)
} else {
return exec(`Script="tell app \\"System Events\\" to keystroke ${keyForAppleScript}"
osascript -e "$Script"`)
}
}
const robot = require('robotjs')
if (modifier) {
return Promise.resolve(robot.keyTap(key, modifier))
} else {
// * will becomes 8, see https://github.com/octalmage/robotjs/issues/285
const robotjsBadChars = /[~!@#$%^&*()_+{}|:"<>?]/
let match = key.match(robotjsBadChars)
if (match) {
return Promise.resolve(robot.keyTap(key, 'shift'))
}
return Promise.resolve(robot.keyTap(key))
}
} |
Closing in light of above workarounds. |
I recommend this issue be re-opened. None of the above work-arounds work reliably (they've all worked for me at some point, but updates to Node, Electron, or new OS cause them to break, especially on macOS). Given that Electron apps can and do use global shortcuts, it'd be great to be able to test them. As it stands, I've had to disable testing on macOS, because I can't get spectron to handle global shortcuts like "Cmd+S". |
I have reopened this issue in light of the comment by AdrianoFerrari |
I have window that only shows when a key combination is pressed (
Ctrl+Space
by default).The behaviour uses electron's
globalShortcut
.I have found no way of simulating the shortcut, is this possible somehow?
Keep it up!
The text was updated successfully, but these errors were encountered: