Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

241
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda