在Protractor中使用Page Object中的函数

问题描述 投票:0回答:1

我在我的afterEach块中有这个代码,用于控制登录失败规范的页面源。但我想把它转移到另一个班级。

    afterEach(function () {

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

但是当我尝试在另一个类中使用以下内容时,我得到'属性'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);
            });
        }
    }
}
protractor pageobjects
1个回答
0
投票

尝试如下

测试文件:

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

类文件

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

    public getSource(state:any) {

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

这里我们从您的测试文件发送State。希望它能帮到你

© www.soinside.com 2019 - 2024. All rights reserved.