Skip to main content

closeWindow

Close the current top-level browsing context, e.g. the current tab or window. If it was the last one, the driver ends the session and the command rejects.

The command calls the WebDriver closeWindow endpoint, then switches to the first remaining window. It resolves after that switch, so the next command runs in the remaining window. To go to another window in a WebDriver Classic session, call switchWindow.

In a WebDriver BiDi session, newWindow does not switch to the new tab, so browser.closeWindow() closes the current tab and not the new tab. To close the new tab, call closeWindow() on the browsing context that newWindow returns.

Usage​

await browser.closeWindow()

Example​

closeWindow.js
it('should close the new tab in a WebDriver Classic session', async () => {
await browser.url('https://webdriver.io')
await browser.newWindow('https://webdriver.io/docs/api', { type: 'tab' })

await browser.closeWindow()
console.log(await browser.getTitle()) // outputs the title of https://webdriver.io
});

it('should close the new tab in a WebDriver BiDi session', async () => {
await browser.url('https://webdriver.io')
const tab = await browser.newWindow('https://webdriver.io/docs/api', { type: 'tab' })

await tab.closeWindow()
console.log(await browser.getTitle()) // outputs the title of https://webdriver.io
});

Welcome! How can I help?

WebdriverIO AI Copilot