Skip to content
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

Headless version for testing #228

Open
mcolyer opened this issue Apr 9, 2014 · 82 comments
Open

Headless version for testing #228

mcolyer opened this issue Apr 9, 2014 · 82 comments

Comments

@mcolyer
Copy link
Contributor

mcolyer commented Apr 9, 2014

@zcbenz how much work do you think it would be to create a headless version of atom-shell that could be used as a replacement for phantomjs?

phantomjs is lagging behind more and more from what actual web browsers do today and it would be great to have something more up to date to use for headless testing.

@zcbenz
Copy link
Member

zcbenz commented Apr 10, 2014

By using hidden browser window atom-shell can actually do what phantomjs does, the example in phantomjs's homepage could be translated to atom-shell:

BrowserWindow = require('browser-window');

console.log('Loading a web page');
var page = new BrowserWindow({show: false});
var url = 'http://www.phantomjs.org/';
page.on('loading-state-changed', (event, isLoading) {
  if (!isLoading)
    //Page is loaded!
    require('app').exit();
});
page.loadUrl(url);

Of course we may need to add some more APIs for automation testing purpose.

The only problem is instead of drawing into a virtual buffer, atom-shell actually draws the page into a real window, this would require a graphic environment, on Window and OS X it doesn't matter, however on Linux we have to use xvfb to provide a X server for atom-shell. This is by design in Chromium's content API, so we can not do anything to remove the dependency of graphic environment.

@zcbenz zcbenz added discussion and removed question labels May 6, 2014
@cpsubrian
Copy link

I've had success with this lately (using Xvfb on an Ubuntu server). My use-case is capturing screenshots of templated pages. In fact, I've found that atom-shell through Xvfb on the server (an m3-large) has better performance than on my local Macbook pro. This led me to want to get atom-shell working through Xvfb in osx as well.

Since osx ships with Xvfb, that part is easy. Can I get atom-shell to use an Xvfb display in osx? Using the standard DISPLAY env variable like I do in linux is not working. Perhaps libchromiumcontent doesn't know how to use X11 when running in Darwin?

@zcbenz
Copy link
Member

zcbenz commented Jun 26, 2014

Xvfb only works for applications that written for X11 environment, on OS X atom-shell uses Cocoa for display, which I believe can not work with Xvfb.

@cpsubrian
Copy link

Yeah kinda put that together. Probably not really a way to compile from source for X11 instead?

@FWeinb
Copy link
Contributor

FWeinb commented Jun 26, 2014

@zcbenz it is currently not possible to create a BrowserWindow that is bigger than the current resolution on OS X, even if you use new BrowserWindow({show: false});

@sindresorhus
Copy link
Contributor

@FWeinb can you open a separate specific ticket for the above?

@FWeinb
Copy link
Contributor

FWeinb commented Jul 10, 2014

Created #475

@zcbenz
Copy link
Member

zcbenz commented Mar 16, 2015

I'm closing this because there is no way to draw a page without actually creating a native widget in Chromium, and I don't think they will ever allow it.

For automatic tests, we do support Selenium.

@etiktin
Copy link
Contributor

etiktin commented Aug 29, 2015

The CEF project has support for off-screen rendering, so you can draw the screen into a buffer instead of a window. Regarding X server for Linux, it seems there's a way to work without it by adding a target called Ozone (see discussion here).

@zcbenz
Copy link
Member

zcbenz commented Aug 31, 2015

@etiktin Thanks for the information! I'm reopening this since there is an existing implementation on how to do it.

@matthewmueller
Copy link

We could really use support for this. We recently replaced Phantom with Electron in Nightmare and love it so far, but it doesn't work out of the box on Linux.

Here's what we need to do right now to get it working: segment-boneyard/nightmare#224 (comment)

@RickIniguez
Copy link

I've been given the task to automate user behavior for one of our web applications that will be converted to a stand alone desktop Electron application. Before our company decided to make this move, we created page objects using the chrome web driver and interacted with the web application by invoking buttons/dropdowns/textboxes using the css selectors. Is there a way to do this with a web application that is wrapped with the Electron shell? The company plans on using the menu bar options to invoke certain functionality and I tried to get to the default menu bar options like File/Edit/Help using the JavaScript driver with no success. Are there any examples out there on how to do this?

@fritx
Copy link
Contributor

fritx commented Sep 23, 2015

segment-boneyard/nightmare#224 (comment) it seems @matthewmueller's snippet works on Linux 👍

@avindra
Copy link

avindra commented Nov 17, 2015

Has anyone gotten headless testing working on SuSE? Specifically SLES?

@puzrin
Copy link

puzrin commented Nov 17, 2015

@fritx The same used for SlimerJS, but that's NOT headless mode.

@Vanuan
Copy link

Vanuan commented Dec 8, 2015

@fritx that's what @zcbenz was saying, you have to have Xvfb running. Both CEF3 and Chromium Content Shell currently depend on Xlib. But with completion of Ozone: https://www.chromium.org/developers/design-documents/ozone
you'd be able to provide any low level I/O.

@Vanuan
Copy link

Vanuan commented Dec 8, 2015

Apparently, there is a master bug in Chromium itself: https://code.google.com/p/chromium/issues/detail?id=546953

@rinogo
Copy link

rinogo commented Aug 15, 2017

Totally agreed with @keithkml - the solutions, although well-intentioned (thank you!), have been more along the lines of "alternatives" until an actual solution is provided. Along those lines, does someone here have sufficient expertise to answer the questions I posed in my previous comment? (And again, please excuse my ignorance! :))

@hitliubo
Copy link

I still don't get the point in responses. Could someone please give me a clarify that do we have NATIVE headless mode for electron app to run in the CI or not?

@dead-claudia
Copy link

dead-claudia commented Jan 22, 2018

@hitliubo Chrome has a --headless flag, but it only works if you're not using plugins like Flash or the PDF reader. If you are, then the answer is an affirmative no at the moment. If you are not, you could pass that flag (along with --disable-gpu if necessary - they fixed this missing implication in newer Chrome versions IIRC) when creating the browser context and see if that works. (Note that if you're using something like Nightmare and you fall into the second category, you really should file an issue in that project's respective repo if there isn't one already.)

Even if you can't use --headless (or if it doesn't work), there are options:

  • Windows: Unless you're using something obscure, you'll always have a window context to render into. Windows Server 2016 dropped default GUI support, but it still supports creating GUI windows, and AFAICT should support the bare minimum for Selenium testing.
  • macOS: You'll always have a GUI here (and thus have a window context to render into). Apple doesn't provide a version that doesn't.
  • Linux: xvfb is a headless X server that's available for just about every common Linux distro (and I mean "common" pretty loosely here). If you're using Travis, they have instructions on how to add it to your project.

@hitliubo
Copy link

@isiahmeadows Thanks for your information. Currently I have a web app running in browser and with chrome/firefox I could always add --headless for the headless testing. Recently we'd like to convert it to Electron app and I tried with --headless which does not work in my macOS. Now I know thee reason. Thanks!

Actually I don't like the solution by xvfb as it's not native. However, given that we don't support native headless so I guess I need to give it a try.

FYI - Now I'm using capybara for the testing.

@faressoft
Copy link

That would be great (y)

@googleson78

This comment has been minimized.

@quinn
Copy link

quinn commented Mar 10, 2020

I got redirected here from a post about rendering directly to a linux frame buffer, but this seems to be focused on headless testing. Has any progress been made rendering directly to a real frame buffer?

@thepont
Copy link

thepont commented Mar 11, 2020

@quinn I'm pretty certain you're going to need an X server, although you can run X11 (Xorg) on a frame-buffer if you like (see: https://www.x.org/releases/current/doc/man/man4/fbdev.4.xhtml).

Edit:

Actually after looking into this a little bit more, this can also be achieved by using ozone. ( https://github.com/jakwu/ozone-fb )

Adding ozone would also allow wayland support, another feature that electron is missing seeing majority of Linux distributions have since migrated to this.

@trusktr
Copy link

trusktr commented Jun 19, 2020

Based on the description of ozone-fb and @isiahmeadows comment, it seems GPU acceleration does not work if rendering to a frame buffer.

@dead-claudia
Copy link

@trusktr That's a 2 year old comment. I'd recommend not taking my comment as authoritative, as that could have changed (I haven't checked since then).

@bigben0123
Copy link
Contributor

bigben0123 commented Oct 28, 2020

I add headless lib to electron and call the HeadlessShellMain。
run:

e run  --headless --enable-logging --v=2 --disable-gpu --screenshot  http://192.168.50.206

then it shows:

Running "/home/a/dev0/e9.2.1/src/out/ReleaseSym0/electron --headless --enable-logging --v=2 --disable-gpu --screenshot http://192.168.50.206:8889"
[1028/172650.483932:INFO:cpu_info.cc(53)] Available number of cores: 4
[1028/172650.484061:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates
[1028/172650.484400:INFO:cpu_info.cc(53)] Available number of cores: 4
[1028/172650.484465:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates
[1028/172650.493514:VERBOSE1:webrtc_internals.cc(119)] Could not get the download directory.
[1028/172650.494623:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings.
[1028/172650.494764:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689
[1028/172650.494873:VERBOSE1:proxy_config_service_linux.cc(500)] All gsettings tests OK. Will get proxy config from gsettings.
[1028/172650.494919:VERBOSE1:proxy_config_service_linux.cc(1261)] Obtained proxy settings from annotation hash code 11258689
[1028/172650.504033:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: renderer.
[1028/172650.505596:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.511468:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.524408:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.524916:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.525173:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.525963:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: gpu-process.
[1028/172650.526373:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.528735:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.531839:VERBOSE2:thread_state.cc(470)] [state:0x556bd75583a0] ScheduleGCIfNeeded
[1028/172650.535051:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed
[1028/172650.550076:VERBOSE1:configured_proxy_resolution_service.cc(873)] PAC support disabled because there is no system implementation
[1028/172650.550312:VERBOSE1:configured_proxy_resolution_service.cc(873)] PAC support disabled because there is no system implementation
[1028/172650.550923:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: http://192.168.50.206:8889/
[1028/172650.575829:VERBOSE1:url_loader.cc(418)] To buffer: http://192.168.50.206:8889/
[1028/172650.580122:VERBOSE1:v8_context_snapshot.cc(153)] A context is created from snapshot for main world
[1028/172650.587399:VERBOSE1:v8_context_snapshot.cc(153)] A context is created from snapshot for main world
[1028/172650.595294:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed
[1028/172650.612295:ERROR:paint_controller.cc(646)] PaintController::FinishCycle() completed
[1028/172650.676553:INFO:headless_shell.cc(620)] Written to file screenshot.png.

Does that mean headless has been implemented?

@samwillis
Copy link

@bigben0123 thats interesting and very exciting! So you have compiled your own version of electron incorporating the headless shell from chromium?

Have you tested in an environment without X on Linux to see if it works?

When chromium is run in headless mode the render sub-processes are started with the passed through —headless flag (you can see this is you use ‘ps args’ from memory). Is this happening for you?

(Strangely with electron currently if you try to start it with —headless the flag isn’t passed though to the render processes but is to the GPU process.)

@bigben0123
Copy link
Contributor

bigben0123 commented Oct 29, 2020

@bigben0123 thats interesting and very exciting! So you have compiled your own version of electron incorporating the headless shell from chromium?

Have you tested in an environment without X on Linux to see if it works?

When chromium is run in headless mode the render sub-processes are started with the passed through —headless flag (you can see this is you use ‘ps args’ from memory). Is this happening for you?

(Strangely with electron currently if you try to start it with —headless the flag isn’t passed though to the render processes but is to the GPU process.)

Yes, I run it on ubuntu which just start in user command mode.
Headless has been passed:

electron --headless --enable-logging --v=2 --disable-gpu -print-to-pdf http://www.google.com
electron --type=zygote --no-zygote-sandbox --enable-logging --headless --v=2 --headless
electron --type=zygote --enable-logging --headless --v=2 --headless
electron --type=zygote --enable-logging --headless --v=2 --headless
electron --type=gpu-process --field-trial-handle=15536072708541054845,15522400966085077738,131072 --enable-logging --headless --v=2 --headless --gpu-preferences=MAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAQAAAAAAAAAAAAAAAAAAAACAAAAAAAAAA= --use-gl=swiftshader-webgl --override-use-software-gl-for-tests --enable-logging --v=2 --shared-files
electron --type=utility --field-trial-handle=15536072708541054845,15522400966085077738,131072 --lang=en-US --service-sandbox-type=network --enable-logging --use-gl=swiftshader-webgl --v=2 --headless --enable-logging --v=2 --shared-files=v8_snapshot_data:100
electron --type=renderer --allow-pre-commit-input --enable-logging --v=2 --field-trial-handle=15536072708541054845,15522400966085077738,131072 --disable-databases --disable-gpu-compositing --lang=en-US --headless --lang=en-US --num-raster-threads=2 --enable-main-frame-before-activation --renderer-client-id=4 --shared-files=v8_snapshot_data:100
electron --type=broker

@samwillis
Copy link

@bigben0123 do you have your changes somewhere? Even if this doesn’t make it into electron core for some reason I would love to be able to use it.

bigben0123 added a commit to bigben0123/electron that referenced this issue Oct 30, 2020
bigben0123 added a commit to bigben0123/electron that referenced this issue Oct 30, 2020
@bigben0123
Copy link
Contributor

bigben0123 commented Nov 4, 2020

This commit only merge chrome headless lib to electron and you can use it the same way as chrome does.
https://github.com/bigben0123/electron/commit/b6cad8993d68d39f1732aa6ed5ece0135b9ae0c8

As far as I'm concerned, chrome and headless has different content layer implementation. Like two browser shell, if you start headless it's nothing to do with chrome, only except you start by "chrome --headless".

The one of headless goal is that ”Minimize the number of invasive or headless-specific changes (e.g., #ifdefs) to Chromium’s code base“.

Therefore, it's difficult to implement that electron is headless to remove xvfb. We can just let electron support headless, but you cannot execute electron apps.

We may use headless' implementation to replace electron's to get a new headless branch.

remove the dependence from electron/BUILD.gn:

      "//ui/events/devices/x11",
      "//ui/events/platform/x11",
      "//ui/gtk"  #all of gkt related

    if (use_x11) {
      deps += [
        "//ui/gfx/x",
        "//ui/gtk:x",
      ]
    }
    configs += [ ":gio_unix" ]
    configs += [ "//build/config/linux:x11" ]

replace with:
"//ui/display",
"//ui/events/devices",

@foxxy432
Copy link

foxxy432 commented Sep 5, 2023

It is ignored by Microsoft because of incompatibilities BUT you can use my fork: https://github.com/lnxx-kernel/electron_touch

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

No branches or pull requests