using a function from Page Object in Protractor

…衆ロ難τιáo~ 提交于 2019-12-11 17:57:53

问题


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

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

回答1:


Try like below

test file:

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

Class File

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

    public getSource(state:any) {

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

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



来源:https://stackoverflow.com/questions/55008539/using-a-function-from-page-object-in-protractor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!