using a function from Page Object in Protractor

2019-08-20 11:52发布

问题:

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