在量角器中使用页面对象中的函数 [英] using a function from Page Object in Protractor

查看:48
本文介绍了在量角器中使用页面对象中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 afterEach 块中有此代码,用于控制台记录失败规范的页面源.但我想把它移到另一个班级.

I have this code in my afterEach block that works to console log the page source for a failed spec. But I want to move it to another class instead.

    afterEach(function () {

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

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

but when i try to use the following in another class, i get 'Property 'currentTest' does not exist on type 'HelperClass'. How do i declare the currentTest property?

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

类文件

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

    public getSource(state:any) {

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

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

Here we are sending the State from your test file. Hope it helps you

这篇关于在量角器中使用页面对象中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆