Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
等待直到使用Typescript在Intern4中加载页面函数?_Typescript_Intern - Fatal编程技术网

等待直到使用Typescript在Intern4中加载页面函数?

等待直到使用Typescript在Intern4中加载页面函数?,typescript,intern,Typescript,Intern,我有一些关于如何实现等待页面加载TypeScript的函数。但有时它不起作用,并抛出timeoutError,即使我在函数中发现了错误。能不能请人复习一下,告诉我有什么问题 以下是我的实现: export abstract class AdvalentBasePage { remote: any; waitTime: number = -1; locator: any; constructor(remote: any, locator: any) {

我有一些关于如何实现等待页面加载
TypeScript
的函数。但有时它不起作用,并抛出
timeoutError
,即使我在函数中发现了错误。能不能请人复习一下,告诉我有什么问题

以下是我的实现:

  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  ')
                }
            }
        }

    }
}
导出抽象类AdvalentBasePage{
远程:任何;
waitTime:number=-1;
定位器:任何;
构造函数(远程:任意,定位器:任意){
this.remote=远程;
this.locator=定位器;
}
抽象getPageTitleXpath():字符串;
getPageTitleElement(){
返回this.remote.findByXpath(this.getPageTitleXpath());
}
//此函数有时甚至在参数中提供的waitTime之前抛出timeoutError
异步waitUntilPageIsFullyLoaded(waitTime:number):承诺{
this.waitTime=waitTime;
var self=这个;
试一试{
等待self.remote.sleep(1000).findByXpath(self.getPageTitleXpath());
}捕获(e){
console.log(e.name)
如果(e.name='NoSuchElement'){
如果(this.waitTime>0){
self.waitTime=self.waitTime-1000;
等待self.waitUntilPageIsFullyLoaded(self.waitTime);
}
否则{
抛出新错误('超时异常')
}
}
}
}
}

有两种不同的超时:

  • 异步测试超时——如果测试花费的时间超过此时间(默认情况下为30秒),则Intern将超时,而不管测试在做什么
  • 查找超时——如果在此时间内找不到元素,则
    findBy
    调用将超时(取决于浏览器,默认情况下通常为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())