1

This is my Page Object class

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");
  }
}

this is my "it" block. This is the starting point of the program.

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


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

My output is like this

**TopMenu constructor called
aaaaaaaaaa
bbbbbbbbbb
ddddddddddddddd
ccccccccc

as you can see from my above output "console.log("ddddddddddddddd")" is called BEFORE this line resolved "this.menuItems = await this.topbarMenu.all(by.xpath("./li")); within TopMenu class"

is it possible to omit that ?

I want to execute any line I right within "it block" AFTER my "TopMenu" gets fully created.

How to control the flow like that ?

AMendis
  • 1,346
  • 4
  • 18
  • 34

1 Answers1

2

awaiting a constructor, unless you're constructing a Promise (which you're not) makes no sense, since a constructor can not return a Promise (except when creating a Promise)

You'll probably want to do something like

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");
}

Now your it block will await the promise returned by setMenus

note: based on the output you are getting, I can safely say that element(... is NOT asynchronous (well, it doesn't return a Promise, it may well have asynchrony, but that isn't "exposed" at all) - therefore you don't need to await element(....

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87