我正在尝试使用Spectron测试我的Electron应用程序。文档中提到,当尝试查找第n个子元素时,可以使用nth child选择器,或者使用
并获取以下输出结果:
你可以看到,输出的HTML确实有几个
顺便提一下(这不应该与主题相关):
$$
获取与选择器匹配的所有子元素,然后使用索引运算符,例如$$("foo")[0]
获取第一个foo。文档
有了这个知识,我期望以下代码输出:BAR
。但是我无法让它工作,我尝试了以下内容:const foo = ".foo";
const fooElements = app.client.$$ (foo);
console.log ("FOOELEMENTS", await TP (app.client.getHTML (foo)));
console.log ("BAR", fooElements[0].getText (".bar"));
并获取以下输出结果:
console.log components\pages\analysis\__test__\Analysis.spectron.ts:44
FOOELEMENTS [ '<div class="foo"><div class="bar">BAR</div><div class="baz">BAZ</div></div>',
'<div class="foo"><div class="bar">BAR2</div><div class="baz">BAZ2</div></div>',
'<div class="foo"><div class="bar">BAR3</div><div class="baz">BAZ3</div></div>'
'<div class="foo"><div class="bar">BAR4</div><div class="baz">BAZ4</div></div>' ]
console.log components\pages\analysis\__test__\Analysis.spectron.ts:50
EXCEPTION TypeError: Cannot read property 'getText' of undefined
at components\pages\analysis\__test__\Analysis.spectron.ts:45:44
at Generator.next (<anonymous>)
at fulfilled (components\pages\analysis\__test__\Analysis.spectron.ts:4:58)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:228:7)
你可以看到,输出的HTML确实有几个
.foo
div,但是当我尝试访问第一个时,它说fooElements[0]
为undefined
。顺便提一下(这不应该与主题相关):
TP
是我写的一个名为toPromise
的函数的别名,它让我等待webdriver的promise,因为TypeScript对它们的实现方式感到困惑。export async function toPromise<T> (client: Webdriver.Client<T>)
{
return client as any as Promise<T>;
}
// Promise
interface Client<T> {
finally(callback: (...args: any[]) => void): Client<T>;
then<P>(
onFulfilled?: (value: T) => P | Client<P>,
onRejected?: (error: any) => P | Client<P>
): Client<P>;
catch<P>(
onRejected?: (error: any) => P | Client<P>
): Client<P>;
inspect(): {
state: "fulfilled" | "rejected" | "pending";
value?: T;
reason?: any;
};
}
有没有想法我做错了什么?或者有什么建议的替代方案?如果可能的话,我更愿意避免使用nth-child选择器。
编辑:更改为类