在量角器中使用页面对象的函数

using a function from Page Object in Protractor

我的 afterEach 块中有这段代码,用于控制台记录失败规范的页面源。但我想将其移至另一个 class。

    afterEach(function () {

    const state = this.currentTest.state;
    if (state === 'failed') {
        browser.driver.getPageSource().then(function (res) {
            console.log(res);     
        });
    }
});

但是当我尝试在另一个 class 中使用以下内容时,我得到“属性 'currentTest' 在类型 'HelperClass' 上不存在”。我如何声明 currentTest 属性?

import { browser, } from 'protractor';
export class HelperClass {

    public getSource() {

        const state = this.currentTest.state;
        if (state === 'failed') {
            browser.driver.getPageSource().then(function (res) {
                console.log(res);
            });
        }
    }
}

像下面这样尝试

测试文件:

const helper = new HelperClass(); // create object for class
  afterEach(async ()=> {
    const state = this.currentTest.state;
    await helper.getSource(state);
});

Class 文件

import { browser, } from 'protractor';
export class HelperClass {

    public getSource(state:any) {

        if (state === 'failed') {
            browser.driver.getPageSource().then(function (res) {
                console.log(res);
            });
        }
    }
}

这里我们从您的测试文件发送 State。 希望对你有帮助