Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we script/automate an Electron app with Puppeteer?

Tags:

Is it possible? Is there a guide somewhere? Basically I'd like to do E2E testing of an Electron app, and will script user interactions, i.e. make a "bot" or "puppet" user that interacts inside the Electron app.

like image 535
trusktr Avatar asked Feb 27 '19 00:02

trusktr


People also ask

Does puppeteer work with electron?

Use puppeteer to test and control your electron application. See the API documentation.

What is electron automation?

Test automation is an efficient way of validating that your application code works as intended. While Electron doesn't actively maintain its own testing solution, this guide will go over a couple ways you can run end-to-end automated tests on your Electron app.

How do you test an electron application?

Start recording a test by going to Test > Record > Record Keyword Test. Expand the Recording toolbar, click Run App, and then select the Electron application. Create Property Checkpoints by clicking Add Check. Checkpoints verify objects and values in the application that's being tested.


1 Answers

EDIT: It's been 6+ months and there are workarounds that can allow you to control the BrowserWindow with puppeteer to certain extent.

If you are doing E2E testing, I still recommend spectron because that's what listed on electron website.

You will need puppeteer-in-electron and puppeteer-core,

const { BrowserWindow, app } = require("electron")
const pie = require("puppeteer-in-electron")
const puppeteer = require("puppeteer-core");

const main = async () => {
  const browser = await pie.connect(app, puppeteer);

  const window = new BrowserWindow();
  const url = "about:blank";
  await window.loadURL(url);

  const page = await pie.getPage(browser, window);

  // here is your page to control
  await page.goto('https://example.net');
  console.log(await page.title()); // should print Example Domain

  // use the following instead of browser.close or disconnect
  window.destroy();
};

main();

There are also other experimental solutions available right now, but not guaranteed to work with all puppeteer API's.

  • puppeteer-electron

Previous answer below,

Not related to puppeteer, but Electron has spectron, which allows you to test electron apps using chrome driver, head to their home page. and api doc.

Spectron was built on top of ChromeDriver and WebDriverIO. So if you are already using puppeteer, the syntax and usage will feel familiar.

Quick Start Spectron

Commands to get you started fast,

mkdir electron-test && cd electron-test    
git clone https://github.com/electron/electron-quick-start
yarn init -y
yarn add -D spectron mocha

So, we have spectron, mocha and the quickstart file inside that folder. Now let's create some spec on test/spec.js path.

const Application = require("spectron").Application;
const assert = require("assert");

describe("Verify a visible window is opened with a title", function() {
  before(async function() {
    this.app = new Application({
      // your app or electron executable path
      path: "node_modules/electron/dist/electron",
      // path to main.js file location
      args: ["electron-quick-start/"]
    });
    await this.app.start();
  });
  after(async function() {
    this.app.stop();
  });

  it("is visible", async function() {
    const isVisible = await this.app.browserWindow.isVisible();
    assert.equal(isVisible, true);
  });

  it("gets the title", async function() {
    const title = await this.app.client.getTitle();
    assert.equal(title, "Hello World!");
  });
});

Let's run it,

➜  electron-test ./node_modules/.bin/mocha


  Verify a visible window is opened with a title
    ✓ is visible
    ✓ gets the title


  2 passing (665ms)
like image 150
Md. Abu Taher Avatar answered Oct 12 '22 14:10

Md. Abu Taher