TrevorSundberg / puppeteer-in-electron

Use puppeteer to test and control your electron application.
MIT License
342 stars 51 forks source link

Headless browserwindow #23

Closed Majid1512 closed 3 years ago

Majid1512 commented 3 years ago

Thank you for your very useful module. I'm using it to make use of puppeteer without additionally needing to download chromium. However, I'm having some trouble when doing the webautomation headlessly.

The following code works as expected and produces a .png:

const main = async () => {
  await pie.initialize(app);
  const browser = await pie.connect(app, puppeteer_C);
  const window = new BrowserWindow({show: true});
  const url = "https://google.com/";
  await window.loadURL(url);
  const page = await pie.getPage(browser, window);
  await page.screenshot({path: 'example0.png'});
  window.destroy();
};
main();

I tried making this task headless by setting the show parameter of BrowserWindow to false:

const main = async () => {
  await pie.initialize(app);
  const browser = await pie.connect(app, puppeteer_C);
  const window = new BrowserWindow({show: false});
  const url = "https://google.com/";
  await window.loadURL(url);
  const page = await pie.getPage(browser, window);
  await page.screenshot({path: 'example1.png'});
  window.destroy();
};
main();

Now no browserwindow pops up, but no screenshot is being produced.

I wonder how to make the webautomation to work headlessly with this module.

ghost commented 3 years ago

Change this: await window.loadURL(url); To this: await page.goto(url);

joaovitordinizmistrinel commented 3 years ago

What worked for me was to do exactly like the previous code

True

const pie = require("puppeteer-in-electron")
const puppeteer = require("puppeteer-core");

const main = async () => {
  await pie.initialize(app);
  const browser = await pie.connect(app, puppeteer);
  const window = new BrowserWindow({show: true});
  const url = "https://instagram.com";
  await window.loadURL(url);
  const page = await pie.getPage(browser, window);
  await page.screenshot({path: 'example1.png'});
};
main();

False

const pie = require("puppeteer-in-electron")
const puppeteer = require("puppeteer-core");

const main = async () => {
  await pie.initialize(app);
  const browser = await pie.connect(app, puppeteer);
  const window = new BrowserWindow({show: false});
  const url = "https://instagram.com";
  await window.loadURL(url);
  const page = await pie.getPage(browser, window);
  await page.screenshot({path: 'example1.png'});
};
main();
TrevorSundberg commented 3 years ago

It would appear electron does not have a proper headless mode, and as you discovered if you use new BrowserWindow({show: false}) then the page.screenshot will hang. @Slayde1r solution works and await page.goto(url) works in this case however I have not tested it for more complex scenarios.

The advised way for doing "headless" operations on electron is to use Xvfb to wrap your application: https://www.electronjs.org/docs/tutorial/testing-on-headless-ci