为什么我的方法在返回之前不等待承诺解决?

Why is my method not waiting for the promise to resolve before returning?

我正在尝试构建一个填充有商家名称的数组,每个商家名称都有一个元素 ID。我首先检查该行是否有值,因为我不知道列表中会有多少商家,除非少于 20 个。我想 return 数组,以便我可以对其进行排序和执行其他操作以进行验证。承诺不会在 returning 之前等待解决。

我可以看到数组构建,但我想输出最终列表Banamex Merchant,Dashboard Demo,eServices Merchant,Germany Merchant,Global Canada Merchant,Michelle's Business,Paytrace Merchant,TestMerchant

正如您在下面看到的,我试图输出最终列表的各种方式都是 return 在构建列表之前就开始了,即使我已经建立了承诺并正在调用 .then() 到 return 我的 it() 声明中的响应。

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

你的问题是你的诺言落空了。在返回承诺解决之前,您没有等待它们完成。

试试这个:

function buildMerchantListArray() {
  const list: string[] = []
  const promises: Promise<void>[] = []
  for (let i = 20; i >= 0; i--) {
    promises.push(
      this.getMerchantName(i)
        .isPresent()
        .then((present) => {
          if (present) {
            this.getMerchantName(i)
              .getText()
              .then((text: string) => {
                list.unshift(text);
                console.log('building list: ' + list)
              })
          }
        })
    )
  }
  return Promise.all(promises).then(() => list)
}