我想在我的存储库(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'sbrowser
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/