webdriverio: [🐛 Bug]: WebDriverError: no such window: target window already closed

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.11.0

Node.js Version

18.19.1

Mode

WDIO Testrunner

Which capabilities are you using?

{
    platformName: 'android',
    'appium:webviewConnectTimeout': 90000,
    // For W3C the appium capabilities need to have an extension prefix
    // http://appium.io/docs/en/writing-running-appium/caps/
    // This is `appium:` for all Appium Capabilities which can be found here
    'appium:deviceName': 'Testing Device',
    // 'appium:platformVersion': '13',
    'appium:automationName': 'UiAutomator2',
 
    // The path to the app
    'appium:app': join(process.cwd(), '/platforms/android/app/build/outputs/apk/debug/app-debug.apk'),
    'appium:appPackage': '<our-app-package-name>',
    // @ts-ignore
    'appium:appWaitActivity': 'MainActivity', // Include all relevant activities
    'appium:appWaitDuration': 20000, // 30 seconds

    'appium:newCommandTimeout': 360,
    'appium:sessionOverride': false,
    'appium:autoGrantPermissions': true,
    'appium:fullReset': false,
    'appium:autoWebview': false ,// This helps in automating the webview context switching

  }

What happened?

On my team we’re building an E2E tests infrastructure with AWS DeviceFarm. Until a couple months we managed to stabilise tests and they were running properly, but now we’re experiencing issues when trying to switch to a webView to authenticate with Auth0. We clearly need to automate this process in order to be able to test all app flows.

The funny thing is that when the application starts, it automatically redirects and opens a webView with Auth0 login page and this first time things work as they should. It logs in, and runs all respective tests. The problem occurs when we need to logout and login again (Testing our logout flow). For starters, the webView context is there, but as soon as it switches to it, I get the error message listed below.

Initially, we were using @wdio/cli@9.5.7. Thought an upgrade could solve it, but it didn’t. Tried also switching context by context title, and the same thing happens.

Any thought on what might

What is your expected behavior?

I would expect to be able to switch between context when needed without experiencing this issue.

How to reproduce the bug.

Here’re all WDIO packages we’re using:

    "@wdio/appium-service": "9.5.7",
    "@wdio/cli": "9.11.0",
    "@wdio/jasmine-framework": "9.11.0",
    "@wdio/junit-reporter": "9.11.0",
    "@wdio/local-runner": "9.11.0",
    "@wdio/spec-reporter": "9.11.0",
    "@wdio/types": "9.10.1",

And an excerpt of the relevant code:

Jasmine test file:

// Note that it's just the relevant code
  it('should open login page', async () => {
    await helper.checkAndSwitchContext(constants.BROWSER_ANDROID, constants.timeouts.SECONDS60); //Try to check if logout routed to the login page
    await page.checkOriginMatches(constants.LOCATION_ORIGIN_LOGIN, constants.timeouts.SECONDS60);
  }, constants.timeouts.SECONDS120, 3);

Helper functions:

async function switchContext(targetContext: string) {
  try{
    const result = await driver.switchContext(targetContext)
    console.log(`Switched to context: ${targetContext}`, result);
  }
  catch(e){
    console.log(`Failed to switch to context: ${e}`);
  }
}

async function checkAndSwitchContext(targetContext: string, timeout: number = 30000) {
  await driver.waitUntil(
    async () => {
      return (await browser.getContexts()).includes(targetContext);
    },
    {
      timeout: timeout,
      timeoutMsg: 'expected text to be different after 10s',
    },
  );
  try{
    await switchContext(targetContext);
  }
  catch(e){
    console.log(e)
  }
}

Relevant log output

2025-03-11T14:24:06.530Z INFO webdriver: [GET] http://127.0.0.1:4723/session/5b35bbf1-dcbb-43ea-abf9-71d262d83204/contexts
[0-0] 2025-03-11T14:24:06.920Z INFO webdriver: RESULT [ 'NATIVE_APP', 'WEBVIEW_chrome', 'WEBVIEW_com.powunity.app' ]
[0-0] 2025-03-11T14:24:06.920Z INFO webdriver: The standard Appium `context`-method is used. If you want to switch to a webview with a specific title or url, please provide an object with the `title` or `url` property. See https://webdriver.io/docs/api/mobile/switchContext for more information.
[0-0] 2025-03-11T14:24:06.921Z INFO webdriver: COMMAND switchAppiumContext("WEBVIEW_chrome")
[0-0] 2025-03-11T14:24:06.921Z INFO webdriver: [POST] http://127.0.0.1:4723/session/5b35bbf1-dcbb-43ea-abf9-71d262d83204/context
[0-0] 2025-03-11T14:24:06.921Z INFO webdriver: DATA { name: 'WEBVIEW_chrome' }
[0-0] 2025-03-11T14:24:07.286Z INFO webdriver: RESULT null
[0-0] Switched to context: WEBVIEW_chrome
[0-0] 2025-03-11T14:24:07.287Z INFO webdriver: COMMAND getUrl()
[0-0] 2025-03-11T14:24:07.287Z INFO webdriver: [GET] http://127.0.0.1:4723/session/5b35bbf1-dcbb-43ea-abf9-71d262d83204/url
[0-0] 2025-03-11T14:24:07.292Z WARN webdriver: WebDriverError: no such window: target window already closed
[0-0] from unknown error: web view not found
[0-0]   (Session info: chrome=134.0.6998.39) when running "url" with method "GET"
[0-0] 2025-03-11T14:24:07.292Z INFO webdriver: Retrying 1/3
[0-0] 2025-03-11T14:24:07.292Z INFO webdriver: [GET] http://127.0.0.1:4723/session/5b35bbf1-dcbb-43ea-abf9-71d262d83204/url
[0-0] 2025-03-11T14:24:07.797Z WARN webdriver: WebDriverError: no such window: target window already closed
[0-0] from unknown error: web view not found
[0-0]   (Session info: chrome=134.0.6998.39) when running "url" with method "GET"

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 43 (24 by maintainers)

Most upvoted comments

@thamp59

From what I can see it’s not related. This is more for mobile with Appium. Yours is releated to desktop web