量角器预期条件包装器返回布尔承诺

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了量角器预期条件包装器返回布尔承诺相关的知识,希望对你有一定的参考价值。

我试图在基类中包装一个预期的条件,以尽可能多的干代码,但我有一个问题是返回的承诺。我已经看了很多解决方案,但还没有找到满足我需求的解决方案。

Base Class (POM)

import { browser, by, element, ElementArrayFinder, ElementFinder, Locator, ExpectedConditions, protractor } from 'protractor';

export class BasePage {
    //#region Page Properties
    ...
    //#endregion Page Properties

    //#region Contructor
    ...
    //#endregion Contructor

    //#region Getters
    ...
    //#endregion Getters

    //#region Page Methods
    ...

public async waitUntilElementPresent(item: ElementFinder, timeToWait: number = 5000): Promise<Boolean> {
    const until = ExpectedConditions;
    return browser.wait(until.presenceOf(item), timeToWait, `Element ${item.locator} is taking too long to become present in the DOM. Waiting Time : ${timeToWait}`).then(() => {
        return true;
    }, () => {
        return false;
    });
}

public async waitUntilElementClickable(item: ElementFinder, timeToWait: number = 5000): Promise<Boolean> {
    const until = ExpectedConditions;
    return browser.wait(until.elementToBeClickable(item), timeToWait, `Element ${item.locator} is taking too long to become clickable. Waiting Time : ${timeToWait}`).then(() => {
        return true;
    }, () => {
        return false;
    });
}

public async waitUntilElementVisible(item: ElementFinder, timeToWait: number = 5000): Promise<Boolean> {
    const until = ExpectedConditions;
    return browser.wait(until.visibilityOf(item), timeToWait, `Element ${item.locator} is taking too long to become visible. Waiting Time : ${timeToWait}`).then(() => {
        return true;
    }, () => {
        return false;
    });
}
//#endregion Page Methods
}

MyPage Class (POM)

import { by, element, ElementFinder, Locator } from 'protractor';
import { BasePage } from '../../common/basePageObjects/basePage';

export class MyPage extends BasePage {

    //#region Page Properties
    private closeButton: Locator;
    //#endregion Page Properties

    //#region Contructor
    constructor() {
        super();
        this.closeButton = by.css('closeBtn');
    }
    //#endregion Contructor

    //#region Getters
    public get CloseButton(): ElementFinder {
        return element(this.closeButton);
    }
    //#endregion Getters

    //#region Page Methods
    public async clickOnCloseButton() {
        await this.CloseButton.click();
    }
    //#endregion Page Methods
}

Step

const myPage: MyPage = new MyPage(); // Extends base class

Then(/^I close my Page popup$/, async () => {
    await expect(myPage.waitUntilElementVisible(myPage.CloseButton, 5000)).to.be.true;
});

当我调试代码时,我遇到了一个断言错误:“expect {}为true”。现在这确实有意义,因为browser.wait返回promise.Promise <{}>。我尝试了不同的方法,但更喜欢让我等待规定时间的方法,如果元素确实符合EC,则返回true,如果不返回false,或者在这种情况下是布尔承诺。

任何建议将不胜感激。

答案

试试吧:

expect(await myPage.waitUntilElementVisible ...

自Protractor 5.2.1以来发生了变化:

之前

await expect(getPromise()).toEqual(42);

expect(await getPromise()).toEqual(42);
另一答案

我看到你使用async。你可以尝试相同,但与await

public async waitUntilElementClickable(item: ElementFinder, timeToWait: number = 5000): Promise<Boolean> {
    const until = ExpectedConditions;
    await browser.wait(until.elementToBeClickable(item), timeToWait, `Element ${item.locator} is taking too long to become clickable. Waiting Time : ${timeToWait}`);
    return true;
}

以上是关于量角器预期条件包装器返回布尔承诺的主要内容,如果未能解决你的问题,请参考以下文章

在量角器中使用本机承诺

总是返回承诺有用吗

SQLServer 空间查询返回错误在预期条件的上下文中指定的非布尔类型的表达式,靠近 ')'

SQL Server - 在“返回”附近的预期条件的上下文中指定的非布尔类型的表达式

如何解决量角器中的承诺

量角器异步/等待 UnhandledPromiseRejectionWarning:未处理的承诺拒绝