简体   繁体   English

我们如何使用Puppeteer编写脚本/自动执行电子应用程序?

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

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, ie make a "bot" or "puppet" user that interacts inside the Electron app. 基本上,我想对Electron应用程序进行E2E测试,并将编写用户交互脚本,即创建一个在Electron应用程序内部进行交互的“机器人”或“ p”用户。

EDIT: It's been 6+ months and there are workarounds that can allow you to control the BrowserWindow with puppeteer to certain extent. 编辑:已经有6个多月了,并且有一些变通办法,可以让您在一定程度上使用puppeteer控制BrowserWindow。

If you are doing E2E testing, I still recommend spectron because that's what listed on electron website. 如果您正在进行E2E测试,我仍然建议您使用spectron,因为那是电子网站上列出的内容。

You will need puppeteer-in-electron and puppeteer-core , 您将需要puppeteer-in-electron puppeteer-corepuppeteer-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 API一起使用。


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. 与操纵up无关,但Electron具有spectron,可让您使用chrome驱动程序测试电子应用程序,并转到其主页。 and api doc . api doc

Spectron was built on top of ChromeDriver and WebDriverIO. Spectron建立在ChromeDriver和WebDriverIO之上。 So if you are already using puppeteer, the syntax and usage will feel familiar. 因此,如果您已经在使用puppeteer,则其语法和用法会很熟悉。

Quick Start Spectron 快速入门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. 因此,我们在该文件夹中包含了spectron,mocha和quickstart文件。 Now let's create some spec on test/spec.js path. 现在让我们在test/spec.js路径上创建一些规范。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM