nightmare icon indicating copy to clipboard operation
nightmare copied to clipboard

Support for running inside a normal electron app

Open bjrmatos opened this issue 9 years ago • 18 comments

Currently nightmare assumes that it will be running inside a normal node.js app so it ends using child_process.spawn(/path/to/electron) to do its job, what i'm proposing is support a way to execute nightmare inside a native electron app, without spawning anything just using it like a normal module.

this feature will let nightmare to be running with a workers manager module (something like electron-workers) and do tasks in scale.

if you are ok i can implement this on my own and send a PR

what i would like is to discuss with you guys is how would you like to support this feature.

some options:

  • a new option to the nigthmare constructor? (Nightmare({ nativeElectronApp: true }))
  • detecting if nightmare is running inside electron via a run-time condition

bjrmatos avatar Dec 18 '15 16:12 bjrmatos

interesting, can you explain to me a little more about what the advantage would be? it seems like the module you linked is also a node process that manages electron processes, it can just manage more than one.

i guess what i'm trying to understand is what the advantage of this approach is over throwing nightmare behind a node cluster (https://nodejs.org/api/cluster.html)

matthewmueller avatar Dec 18 '15 18:12 matthewmueller

the important difference is that currently if i put nigthmare behind a node cluster it will mean that 1 task = 1 electron process (created by nigthmare), so if my application need to do 100 tasks in parallel it will mean creating 100 electron process.. there is no electron process re-use and there is the initial overhead of creating and electron process each time per task.

the advantage of electron-workers is that the logic for worker assignment lives in the electron process by itself (using node cluster or another algorithm is just an implementation detail in electron-workers, so electron-workers is just an abstraction), if nigthmare can support running in a normal electron app, from a node app i can invoke a module with a worker abstraction (can be electron-workers or a custom node cluster script), inside the worker module i can spawn an electron process with a electron script just one time and re-use it for many tasks, in the electron script i can require('nigthmare') and using it normally, so if my node app need to do 100 tasks in parallel it will mean 1 task spawning the electron process, 99 tasks re-using the electron process created before.. just one electron process created per worker :)

bjrmatos avatar Dec 18 '15 20:12 bjrmatos

in other words.. with my use case in mind, there is no need for nigthmare to spawn an electron process per instance created, it could just create a browser window each time per instance (assuming that nigthmare will be running inside an electron app/script).

another advantage gained with nigthmare supporting this feature is that the developer can have control of the electron process by itself, so anyone can implement custom funcionality (#391, #373)

bjrmatos avatar Dec 18 '15 20:12 bjrmatos

yah okay thanks, I understand what you mean. I guess the two issues in my mind that i see are:

  1. major refactor of the existing codebase (not a huge deal)
  2. more importantly, anytime you want to use nightmare, you'd need to run it using the electron executable. this is fine if you're running it standalone, but if you need to integrate it with a node server or something, you're basically rewriting the existing node layer yourself.

i'd be interested in figuring out the proper separation for the electron process to run and be useful standalone if you want to run it on it's own, but for the sake of ease of use, I don't see the node layer going away.

matthewmueller avatar Dec 22 '15 04:12 matthewmueller

I don't see the node layer going away.

yes, that was not my intention :smile: just figure out how to make it run stand-alone.. let me experiment with a run-time condition (detecting if nigthmare is running inside a electron app) and see how it looks!

bjrmatos avatar Dec 22 '15 19:12 bjrmatos

okay sweet :+1:

matthewmueller avatar Dec 22 '15 20:12 matthewmueller

+1 for this, although I'm not sure I''m totally following everything mentioned above. It does seem like it's related to my use case which is that I'd like to build an Electron app that's a GUI for taking screenshots. Being able to take many screenshots all at once (or in quick succession) which varying options.

FranciscoG avatar Jan 25 '16 18:01 FranciscoG

Ideally you would detect and/or have options to specify that nightmare should spawn Electron process itself or that it is already running in an Electron process and use it natively.

I want this functionality now where I have an app that runs as a service (node) as well as a UI (electron). When running the UI, I will now have additional ram usage due to additional electron process, though I can't avoid the doubling up of electron-prebuilt directory.

TimNZ avatar Jun 21 '16 01:06 TimNZ

An issue with Nightmare not running inside Electron just became real.

I just realised that electron-packager builds an executor that won't accept an arbitrary script file on the command line e.g. 'electron index.js'

This means it complicates create a package for an Electron app that utilises Nightmare.

I can work around this, but right now without custom packaging requires the packaged electron plus electron-prebuilt to be installed doubling up on disk usage. Memory use is less of a concern.

TimNZ avatar Jun 22 '16 21:06 TimNZ

how's it going?

starInEcust avatar Aug 29 '16 06:08 starInEcust

@bjrmatos hi there! I just ran across a situation where I am required to have nightmare run inside an already executing electron application, and I noticed you said you might have experimented with this.

I was wondering if you had anything working/WIP that I could use as a base -- otherwise I am going to implement this shortly! Thanks!

Timer avatar Jan 22 '17 05:01 Timer

Nevermind! I managed to get nightmare to use the already running electron instance. Sorry for bugging you @bjrmatos.

Timer avatar Jan 22 '17 07:01 Timer

@Timer Could you share a link to how you managed to do that? I have an idea for a project that will need to do just that. (nightmare using electron instance)

yourfavorite avatar Jan 22 '17 12:01 yourfavorite

@yourfavorite I need to do some tweaking and I'll push it to my fork of nightmare. I broke the existing functionality of running within node and always assumed to not launch electron. Once I have the changes working for both use cases I'll drop a line on this thread. :)

Timer avatar Jan 22 '17 16:01 Timer

@yourfavorite see https://github.com/segmentio/nightmare/pull/967 👍

Timer avatar Jan 22 '17 17:01 Timer

@matthewmueller Will the problem be solved in which version?

quan-zai avatar Jul 04 '18 10:07 quan-zai

Any update with this? I have tried #967 and it works well except for in the case of running multiple instances of nightmare and using functions such as .type() that fill out input fields. It gets the windows confused and doesn't type the correct information or sometimes, any information at all. Im assuming this is a problem with focusing on the window, as it runs perfectly when I open multiple instances of my actual electron app and do it like that.

CameronStuartSmith avatar Nov 01 '18 21:11 CameronStuartSmith

Has anybody found a way to make this work? I am getting spawn ENOTDIR error when trying to spawn a nightmare instance from within the packaged electron app.

mattlm0831 avatar Jul 01 '20 20:07 mattlm0831