我正在尝试在基类中包装一个预期的条件,以尽可能多地创建干代码但是我遇到了返回promise的问题。我已经看了很多解决方案,但还没有找到满足我需求的解决方案。
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
}
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
}
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&lt; {}&gt;。我尝试了不同的方法但更喜欢让我等待规定时间的方法,如果元素符合EC,则返回true,如果不返回false,或者在这种情况下是布尔承诺。
任何建议都将不胜感激。
答案 0 :(得分:0)
试一试:
expect(await myPage.waitUntilElementVisible ...
自Protractor 5.2.1以后发生了变化:
在
await expect(getPromise()).toEqual(42);
在
expect(await getPromise()).toEqual(42);
答案 1 :(得分:0)
我看到你使用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;
}