【问题标题】:Spectron - TypeError: app.client.getText is not a functionSpectron - TypeError:app.client.getText 不是函数
【发布时间】:2021-04-21 12:27:39
【问题描述】:

我想在我的 repo (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!");
  });
});

有人可以帮帮我吗?

【问题讨论】:

    标签: javascript jestjs electron electron-forge spectron


    【解决方案1】:

    关于您对windowCount2 的评论,请参阅this section of the docs 中的最后一条评论。

    //请注意,如果dev tools被打开,getWindowCount()将返回2。

    我不知道 devtools 是否开放,但这似乎是目前官方的理由。

    关于app.client.getText 不是函数,那是因为它不是函数。实际上,文档似乎不正确 - 可能没有从 v11.0.0 to v11.1.0 更新,并且被遗忘了。

    Spectron 使用 WebdriverIO 并在创建的应用程序实例上公开托管的 client 属性。客户端API是WebdriverIO的browser对象。

    来源: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 为我指明了该解决方案的正确方向。

    干杯。

    【讨论】:

      【解决方案2】:

      这里还有一些可能对其他人有用的东西。

      您使用了错误的断言。你想使用:

      expect(headerText).to.equal("Hello World!");
      

      试试这样的:

          it("Check - Header Text", async function () {
              const headerElement = await app.client.$("h1"); 
              let headerText = await headerElement.getText();
              expect(headerText).to.equal("Hello World!");
          });
      

      如果您在使用 expect() 时遇到错误,也不要介意添加它:

      const expect = require('chai').expect
      

      【讨论】:

        猜你喜欢
        • 2017-06-22
        • 2018-05-19
        • 1970-01-01
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        相关资源
        最近更新 更多