问题描述
我想在我的存储库 (https://github.com/DWboutin/jest-spectron-ts) 中创建测试似乎一切正常,但我无法在我的测试中访问 Application.client API。
我尝试了 Promises 风格、async/await 和 Mocha,但我无法让它工作。
这是我唯一的测试文件(非常基础)
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 中的最后一条评论。
// 请注意,如果打开 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 为我指明了此解决方案的正确方向。
干杯。