Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

246
Vistas
Webdriver.io: Problems when switching to new window/tab

I'm using Webdriver.io/Nodejs/Mocha and when I try to switch from parent tab to child tab, majority of the time it works; however, there are times that the tab will take a long time to load due to issues/bad adverts on the page and during those times, even though I get the window/tab GUID, it still doesn't switch and remains on the parent tab. It doesn't happen all the time but occassionally it fails to switch.

Does the page fully have to load to be able to switch to the chid tab? Am I missing anything? Any help would be appreciated. Thanks!

  • Node version: v16.13.1
  • WebdriverIO version: 7.16.12
  • Mocha version: 9.0.0
  • Chromedriver version: 95.0.0

Execution:

npx mocha --config ./mocharc.json ./test/test.js

Test File: test.js

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);
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This seems like a simple coding mistake. While you are initiating your parentGUID, you are not assigning any value to it. You are then using it to compare in your for loop. Sometimes, the childGUID is assigned with parent GUID. In this case, the switch seems to be not happening. I have corrected it below.

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

  let childGUID;

  // get parent GUID
  const 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);
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda