等到使用 Typescript 在实习生 4 中加载页面功能?

wait until page is loaded function in intern 4 using Typescript?

我知道如何实现等待页面在 intern 4TypeScript 中加载的功能。但有时它不起作用并抛出 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  ')
                }
            }
        }

    }
}

有几个不同的超时在起作用:

  1. 异步测试超时 -- 如果测试花费的时间超过此时间(默认情况下为 30 秒),无论测试在做什么,实习生都会超时。
  2. 查找超时 -- 如果在此时间内未找到元素,findBy 调用将超时(取决于浏览器,默认情况下通常为 0)

从您的描述中不清楚具体的错误是什么,尽管听起来您可能 运行 陷入了一般测试超时错误。您可以通过增加测试超时来解决这个问题(测试中的 this.timeout = xthis.async(x),其中 x 是毫秒数)。

看起来您还可以通过简单地设置一个非常大的查找超时而不是创建 waitUntilPageIsFullLoaded 方法来简化您的方法。例如(假设 self 指向 Test 对象):

// 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())