【问题标题】:Protractor and async/await量角器和异步/等待
【发布时间】:2019-03-10 05:55:25
【问题描述】:

这是我的页面对象类

export class TopMenu {
  topbarMenu: ElementFinder;
  menuItems;
  numberofMenuItems: number;

  constructor() {
    console.log("TopMenu constructor called");
    this.setMenus();
  }

  async setMenus() {
    console.log("aaaaaaaaaa");
    this.topbarMenu = await element(by.css("ul[class*='topbar-menu']"));
    console.log("bbbbbbbbbb");
    this.menuItems = await this.topbarMenu.all(by.xpath("./li"));
    console.log("ccccccccc");
  }
}

这是我的“它”块。这是程序的起点。

it("Click all Submenues 3", async () => {
    browser.waitForAngularEnabled(true);


    let topMenu: TopMenu = await new TopMenu();
    console.log("ddddddddddddddd");
}

我的输出是这样的

**TopMenu constructor called
aaaaaaaaaa
bbbbbbbbbb
ddddddddddddddd
ccccccccc

从我上面的输出中可以看出,“console.log("ddddddddddddddd")”在此行解析为“this.menuItems = await this.topbarMenu.all(by.xpath("./li")); in TopMenu 类”之前被调用

可以省略吗?

在我的“TopMenu”完全创建后,我想在“it block”内执行任何行。

如何控制这样的流量?

【问题讨论】:

    标签: javascript async-await jasmine protractor angular-e2e


    【解决方案1】:

    await构造一个构造函数,除非你正在构造一个 Promise(你不是)是没有意义的,因为构造函数不能返回一个 Promise(除非创建一个 Promise)

    你可能想做类似的事情

    export class TopMenu {
      topbarMenu: ElementFinder;
      menuItems;
      numberofMenuItems: number;
      somePromise;
    
      constructor() {
        console.log("TopMenu constructor called");
        this.somePromise = this.setMenus();
      }
    
      async setMenus() {
        console.log("aaaaaaaaaa");
        this.topbarMenu = await element(by.css("ul[class*='topbar-menu']"));
        console.log("bbbbbbbbbb");
        this.menuItems = await this.topbarMenu.all(by.xpath("./li"));
        console.log("ccccccccc");
      }
    }
    
    it("Click all Submenues 3", async () => {
        browser.waitForAngularEnabled(true);
    
    
        let topMenu: TopMenu = new TopMenu();
        await topMenu.somePromise;
        console.log("ddddddddddddddd");
    }
    

    现在您的it 块将等待setMenus 返回的承诺

    注意:根据您得到的输出,我可以肯定地说 element(... 不是异步的(好吧,它不返回 Promise,它可能具有异步性,但这不是“暴露”在all) - 因此你不需要await element(....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      相关资源
      最近更新 更多