我有一些实现的函数,直到用TypeScript
在Intern 4中加载页面。但是有时候,即使我在函数中捕获错误,它也无效并抛出TimeOutEror
。有人可以查看并告诉我这怎么了。
我的实施:
export abstract class AdvalentBasePage {
remote: any;
waitTime: number = -1;
locator: any;
constructor(remote: any, locator: any) {
this.remote = remote;
this.locator =locator ;
}
abstract getPageTitleXpath(): string;
getPageTitleElement() {
return this.remote.findByXpath(this.getPageTitleXpath());
}
//this function sometimes throws timeoutError even before waitTime provided in argument
async waitUntilPageIsFullyLoaded(waitTime: number): Promise<any> {
this.waitTime = waitTime;
var self = this;
try {
await self.remote.sleep(1000).findByXpath(self.getPageTitleXpath());
} catch (e) {
console.log(e.name)
if (e.name == 'NoSuchElement') {
if (this.waitTime > 0) {
self.waitTime = self.waitTime - 1000;
await self.waitUntilPageIsFullyLoaded(self.waitTime);
}
else {
throw new Error('TimeOut Exception ')
}
}
}
}
}
播放中有几个不同的超时:
- 异步测试超时 - 如果测试需要更长的时间(默认情况下为30秒),则实习生将超时。
- 查找超时 - 如果在这段时间内找不到元素,则
findBy
调用将超时(浏览器依赖于浏览器,通常为0,默认情况下为0)
从您的描述中不清楚什么是特定错误,尽管听起来您可能正在遇到一般测试超时错误。您可以通过增加测试超时(在测试中的this.timeout = x
或this.async(x)
,其中X是多个毫秒)。
看起来您还可以通过简单地设置一个非常大的找到超时而不是创建waitUntilPageIsFullLoaded
方法来简化您的方法。例如(假设self
指向测试对象):
// Make sure the test timeout is large enough to handle the time
// required to run all test actions.
self.timeout = 130000;
await self.remote
// Set the find timeout to something huge; this is still efficient
// because the test will continue as soon as an element is found
.setFindTimeout(120000)
.sleep(1000)
.findByXpath(self.getPageTitleXpath())