【发布时间】:2018-06-16 22:46:50
【问题描述】:
我正在尝试构建一个填充了商家名称的数组,每个商家名称都有一个元素 ID。我首先检查该行是否有值,因为我不知道列表中有多少商家,除了少于 20 个。我想返回数组,以便对其进行排序并执行其他操作验证。承诺不会在返回之前等待解决。
我可以看到数组构建,但我想输出最终列表Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
正如您在下面看到的那样,即使我已经建立了一个承诺并正在调用 .then() 以在我的 @ 987654323@ 声明。
merchants.po.ts
export class MerchantsPage {
buildMerchantListArray(): Promise < string[] > {
return new Promise < string[] > (resolve => {
const list: string[] = [];
for (let i = 20; i >= 0; i--) {
this.getMerchantName(i)
.isPresent()
.then((present) => {
if (present) {
this.getMerchantName(i)
.getText()
.then((text: string) => {
list.unshift(text);
console.log('building list: ' + list)
})
}
});
}
return resolve(list);
});
}
doBuild() {
this.buildMerchantListArray()
.then((list: string[]) => {
return console.log('doBuild: ' + list);
});
}
/**
* Get table element for Merchant's Name of row i
* @param i
* @returns {ElementFinder}
*/
getMerchantName(i: number): ElementFinder {
return element(by.id('Merchant-' + i + '-Name'));
}
}
merchants.e2e-spec.ts
import {FooterParams} from '../../../../components/footer/footer.params';
import {Footer} from '../../../../components/footer/footer.po';
import {Logo} from '../../../utility/logo/logo.po';
import {LoginParams} from '../../../authentication/login/login.params';
import {LoginPage} from '../../../authentication/login/login.po';
import {AppPage} from '../../../../app.po';
import {Header} from '../../../dashboard/header/header.po';
import {ElementFinder} from 'protractor';
import {MerchantsPage} from './merchants.po';
import {MerchantsParams} from './merchants.params';
describe('Merchants Pages of Dashboard App', () => {
let app: AppPage;
let login: LoginPage;
let navigation: Header;
let page: MerchantsPage;
beforeAll(() => {
navigation = new Header();
app = new AppPage();
login = new LoginPage();
page = new MerchantsPage();
app.navigateTo('dashboard').then(() => {
login.checkAuthentication(LoginParams.user, LoginParams.user_pwd);
});
});
beforeEach(() => {
Logo.getLogo().click().then(() => {
navigation.clickAdminDropdown().then(() => {
navigation.clickMerchants();
expect(page.getMerchantsHeader().getText()).toContain(MerchantsParams.merchantsHeaderText);
Footer.getFooter().then((footer: ElementFinder) => {
(expect(footer.getText()).toContain(FooterParams.footer));
});
});
});
});
it('should verify the Merchants list view is alphabetical by Name', () => {
Promise.resolve(page.buildMerchantListArray()).then((arr) => {
console.log('my list: ' + arr);
});
page.doBuild();
page.buildMerchantListArray().then((response) => {
console.log('response: ' + response);
});
});
});
控制台输出
Jasmine started
my list:
doBuild:
response:
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: TestMerchant
building list: Paytrace Merchant,TestMerchant
building list: Michelle's Business,Paytrace Merchant,TestMerchant
building list: Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
building list: Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant
Merchants Pages of Dashboard App
√ should verify the Merchants list view is alphabetical by Name
【问题讨论】:
-
为什么要使用 setTimeout 在 1500 年强制退出承诺?这可能就是为什么
-
我在其他地方看到了这个建议。在我也合并 setTimeout 之前,我得到了相同的结果。我已经用我尝试不使用 setTimeout 调用的代码更新了我的问题。
标签: arrays typescript promise protractor resolve