Task Progress0 of 4 Tasks
0%

Windows & Tabs

Switch between tabs and popup windows: read data from a new tab, approve in a popup that closes itself, wait for a slow popup and pick the right window by its title. Each task ticks itself when its result box turns green.

1.New tab

Read from a new tabTo do
Open the secret in a new tab
Read the secret from the new tab
Goal

Open the link in a new tab, read the secret word, close the tab, then type the word here and press Check.

2.Popup that reports back

Approve in a popupTo do

Approval code:

Waiting for approval
Goal

Open the popup and press Approve. The popup sends a code back to this page and closes itself.

3.Slow popup

Wait inside a popupTo do
Waiting for confirmation
Goal

Open the slow popup. Its Confirm button appears after 1 to 3 seconds: wait for it and click it.

4.Pick the right window

Find a window by its titleTo do
Pick Window B
Goal

Open all three windows, switch to the one titled “Window B” and press its button.

5. Solutions

Try the challenges yourself first. Reference solutions are hidden until you ask for them.

About this Windows & Tabs page

Window handling is switching your test's focus between browser tabs and popup windows. Selenium gives each window a handle, and you switch with getWindowHandles() and switchTo().window(handle), often choosing the target by title or URL. Playwright exposes new pages through the context page event or page.waitForEvent('popup'). Cypress cannot switch tabs, so tests remove target=_blank or visit the URL directly.

Screenshot of the Windows & Tabs practice page on QA Playground. New tabs, popups that close themselves and windows found by title.
Windows & Tabs: New tabs, popups that close themselves and windows found by title.

Frequently asked questions

How do you switch to a new tab in Selenium?

Store the current handle with getWindowHandle(), click the link, then loop through getWindowHandles() and switch to the one that differs. In Selenium 4 you can also call driver.switchTo().newWindow(WindowType.TAB) to open and focus a tab. Switch back with the stored handle.

How do you switch to a window by title in Selenium?

Iterate over driver.getWindowHandles(), call switchTo().window(handle) for each, and compare driver.getTitle() with the expected title. Stop when it matches. If none match, switch back to the original handle so later steps do not run in the wrong window.

How do you handle a new tab in Playwright?

Start waiting before the click: const [newPage] = await Promise.all([context.waitForEvent('page'), page.click('a')]). For window.open popups use page.waitForEvent('popup'). Then call newPage.waitForLoadState() and work with newPage directly. No switching is required.

How do you handle a popup that closes itself?

Grab the handle list before the popup closes, switch to it, read what you need, and switch back to the parent. After it closes, using the old handle throws NoSuchWindowException in Selenium. In Playwright, listen for the page close event.

Can Cypress switch between tabs?

No, Cypress runs inside one tab. Remove the target attribute with invoke('removeAttr', 'target') so the link opens in the same tab, or read the href and cy.visit it. For popups, stub window.open with cy.stub and assert the call.

Free developer tools on Randomly.online

QA Playground is part of Randomly.online. These tools help while you write and debug tests: