davidanthoff / Electron.jl

Julia wrapper for Electron
Other
86 stars 19 forks source link

Triggering events #59

Closed SimonDanisch closed 4 years ago

SimonDanisch commented 4 years ago

I found the following snippet window.webContents.sendInputEvent({type: 'mouseMove', x: 10, y: 10}), but I'm not sure how I can run this code in an electron window.. It seems like the eval context for the JS that gets sent to Electron doesn't have the references to e.g. the window...

davidanthoff commented 4 years ago

I think that needs to be run in the Main process, not in the browser process, right?

So you would send the javascript to the app, not the window. And then use something like this to get access to the window and webcontents.

SimonDanisch commented 4 years ago

I see... Yeah I'm still not sure how things are layered... Trying to figure out how to best do web ui testing! Maybe integrating https://github.com/electron-userland/spectron would be a good idea!

SimonDanisch commented 4 years ago

Ok, nevermind... I can just trigger events with pure JS, without any deeper browser integration:

function trigger_keyboard_press(key){
    var down = new KeyboardEvent('keydown', {key: key, code: key});
    var up = new KeyboardEvent('keyup', {key: key, code: key});
    document.dispatchEvent(down);
    document.dispatchEvent(up)
}

I thought something like this doesn't work, since nothing happened when I first tried it, but that was just due to all sorts of subtleties in how to trigger the events ;)

SimonDanisch commented 4 years ago

Btw, I incorperated this function in ElectronTests.jl and plan to add more functionality like this ;)