How to switch tabs in a test in JS-Nightwatch?

How can I switch tabs in a test in JS-Nightwatch?

Hello Macy,

You can refer to the sample code below to switch tabs in a test in JS-Nightwatch:


it('Test Case: Switch Tabs', async () => {

  let parentGUID;
  let childGUID;

  // get parent GUID
  parentGUID = await this.driver.getWindowHandle();

  // click element to launch new tab
  await this.driver.elementClick('//a[@id="test"]');

  // wait until new tab loads
  await this.driver.pause(2000);

  // get all GUID's
  const allGUIDs = await this.driver.getWindowHandles();

  // check all GUID's and see which one is the child
  for (let i = 0; i < allGUIDs.length; i++) {
    if (allGUIDs[i] !== parentGUID) {
      childGUID = allGUIDs[i];
    }
  }

  // switch to child tab
  await this.driver.switchToWindow(childGUID);

  // assert content on the new page here
  // ...
  // ...
  // ...

  // close tab
  await this.driver.closeWindow();

  // switch to parent window/tab
  await this.driver.switchToWindow(parentGUID);
}