javascript - Spectron-TypeError : app. client.getText不是函数

标签 javascript jestjs electron electron-forge spectron

我想在我的存储库(https://github.com/DWboutin/jest-spectron-ts)中创建测试,但似乎都可以,但是我无法在测试中访问Application.client API。
我在Mocha中尝试了Promises风格(异步/等待),但无法正常工作。
这是我唯一的测试文件(非常基本)


const path = require("path");
const Application = require("spectron").Application;
const electron = require("electron");

jest.setTimeout(10000); // increase to 50000 on low spec laptop

let app: any = new Application({
  path: electron,
  args: [path.join(__dirname, "..", ".webpack", "main", "index.js")]
});

describe("test", () => {
  beforeAll(async () => {
    await app.start();
  });

  afterAll(async () => {
    if (app && app.isRunning()) {
      await app.stop();
    }
  });

  it("shows an initial window", async () => {
    const windowCount = await app.client.getWindowCount();

    expect(windowCount).toBe(1); // this gives 2, I don't know why
  });

  it("should have correct text", async () => {
    const h1Text = await app.client.getText("h1"); // TypeError: app.client.getText is not a function

    expect(h1Text).toEqual("Hello World!");
  });
});
有谁可以帮助我吗?

最佳答案

关于您对windowCount的评论是2,请参阅this section of the docs的最后评论。

// Please note that getWindowCount() will return 2 if dev tools are opened.


我不知道devtools是否打开,但这似乎是目前的官方理由。
关于app.client.getText不是一个函数,是因为它不是一个函数。实际上,似乎文档不正确-可能未从v11.0.0 to v11.1.0更新,并被遗忘了。

Spectron uses WebdriverIO and exposes the managed client property on the created Application instances. The client API is WebdriverIO's browser object.


资料来源:https://github.com/electron-userland/spectron#client
WebdriverIO的browser对象不包含getText函数(https://webdriver.io/docs/api.html),但是,检索到的元素确实具有所涉及的 getText 函数。
这应该可以解决您的问题:
it("should have correct text", async () => {
  // because there are "two" windows...
  await app.client.windowByIndex(1);
  // use query selector to get h1
  const header = await app.client.$("h1");
  // grab the text from the element
  const h1Text = await header.getText();

  expect(h1Text).toEqual("💖 Hello World!");
});
Spectron存储库上的This unit test为我指出了解决该问题的正确方向。
干杯。

关于javascript - Spectron-TypeError : app. client.getText不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65753657/

相关文章:

javascript - 关于requirejs异步

javascript - 模拟移动连接

javascript - 如何监视对于Jest方式导入的函数

javascript - 未找到 testResultsProcessor 选项中的模块 'jest-junit'

css - Electron 不使用更新的 css 文件

javascript - 使用 Ghost.py 在 python 中屏幕抓取动态网页

javascript - price_from.toFixed() 不是函数

javascript - 在 Jest 环境被拆除后导入文件

javascript - Electron - 创建后更改窗口设置

node.js - Electron.JS占用大量CPU,我无法确定我的代码出了什么问题