Introduction
Modern web applications frequently open new browser windows, tabs, payment gateways, login pages, and third-party authentication screens. Selenium handles these using window handles.
Similarly, dynamic web applications require synchronization before interacting with elements. Selenium provides multiple wait mechanisms to handle slow-loading pages, AJAX requests, and dynamic elements.
This tutorial combines both topics into practical exercises designed for beginners and experienced automation testers.
Windows & Tabs Practice Exercises
Level 1 – Basic Exercises (0–1 Year)
Exercise 1 – Open and Switch to a New Tab
Objective
Learn basic parent-child window switching.
Task
- Open a new browser tab
- Switch to the new tab
- Verify the page title
- Return to the parent window
- Close both windows
Focus
Basic window switching.
Exercise 2 – Close a Specific Tab
Objective
Close only one selected tab.
Task
- Open multiple tabs
- Switch using the required window handle
- Close only that tab
- Verify remaining tabs stay open
Focus
Managing individual tabs.
Exercise 3 – Handle a Child Window
Objective
Practice parent-child window handling.
Task
- Capture parent window handle
- Open child window
- Switch to child
- Print page title
- Close child
- Return to parent
Focus
Window handle management.
Exercise 4 – Navigate Multiple Tabs
Objective
Iterate through all browser tabs.
Task
- Open multiple tabs
- Switch to each tab
- Print each page title
- Close tabs one by one
- Finish on the parent window
Focus
Looping through window handles.
Level 2 – Intermediate Exercises (1–3 Years)
Exercise 5 – Handle Multiple Child Windows
Objective
Interact with multiple child windows.
Task
- Open multiple child windows
- Switch using window handles
- Perform different actions
- Close selected windows
- Return to the parent
Focus
Real project workflows.
Exercise 6 – Switch Between Parent and Child Windows
Objective
Switch multiple times during one test.
Task
- Open child window
- Perform an action
- Return to parent
- Open another child
- Repeat
- Verify active window after every switch
Focus
Reliable navigation.
Exercise 7 – Close All Child Windows
Objective
Keep only the parent window open.
Task
- Open multiple windows
- Close every child window
- Verify parent window remains active
- Continue testing
Focus
Window cleanup.
Exercise 8 – Open a New Tab Using JavaScript
Objective
Create new browser tabs using JavaScript.
Task
- Open new tab
- Switch to it
- Fill a form
- Close child tab
- Return to parent
Focus
JavaScript window handling.
Level 3 – Advanced Exercises (3–5 Years)
Exercise 9 – Parallel Window Handling
Objective
Handle browser windows during parallel execution.
Task
- Open multiple windows
- Switch correctly within each thread
- Avoid session conflicts
- Close all windows safely
Focus
Thread-safe automation.
Exercise 10 – Handle Popup Windows
Objective
Automate popup windows.
Task
- Open popup
- Switch window
- Submit a form
- Verify result
- Close popup
- Return to parent
Focus
Real-world popup automation.
Exercise 11 – Store Window Handles
Objective
Manage windows dynamically.
Task
- Open multiple windows
- Store handles in a Map
- Switch using stored handles
- Perform actions
- Close all windows
Focus
Large enterprise frameworks.
Exercise 12 – Data-Driven Window Handling
Objective
Combine multiple concepts.
Task
- Open child window
- Read test data from Excel
- Fill the form
- Submit
- Return to parent
Focus
Data-driven automation.
Wait Mechanisms Practice
Thread.sleep()
Practice Exercises
- Pause execution using
Thread.sleep(5000) - Observe unnecessary waiting
- Replace Thread.sleep with Explicit Wait
- Compare execution time
- Observe failures with dynamic page loading
- Add multiple Thread.sleep calls and measure slowdown
Practice Questions
- Why does Thread.sleep fail in CI environments?
- Why should it not be used in automation frameworks?
- When is Thread.sleep temporarily acceptable?
Implicit Wait
Practice Exercises
- Configure global implicit wait
- Locate slow-loading elements
- Remove implicit wait
- Observe
NoSuchElementException - Combine with page-load timeout
Configure Implicit Wait
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Practice Questions
- How does Implicit Wait work?
- What happens if it is too large?
- Does it affect every element?
- When should you use Implicit Wait?
Explicit Wait (WebDriverWait)
Practice Exercises
- Wait for element visibility
- Wait until element is clickable
- Wait for page title
- Wait for element text
- Wait before performing hover actions
- Create reusable wait utilities
- Wait for alert presence
Reusable Wait Method
waitForElementClickable(driver, element, 10);
Practice Questions
- How do you handle AJAX elements?
- Why is a button clickable manually but not during automation?
- When should Explicit Wait be preferred?
- How do you synchronize dynamic tables?
Fluent Wait
Practice Exercises
- Create FluentWait
- Timeout: 20 seconds
- Poll every 2 seconds
- Ignore
NoSuchElementException - Wait until text changes
- Wait for AJAX table loading
- Wait for custom conditions
Practice Questions
- When is Fluent Wait better than Explicit Wait?
- How does polling reduce execution time?
- How do you handle randomly loading elements?
Real-Time Scenario Questions
Scenario 1 – Intermittent Element Not Found
An element appears randomly depending on server response.
Question
Which wait mechanism would you choose and why?
Scenario 2 – Different Page Load Speeds
The application works locally but loads slowly in QA.
Question
How would you synchronize the test?
Scenario 3 – AJAX Delays
Buttons become clickable after background requests.
Question
How would you automate this without using Thread.sleep?
Scenario 4 – Mixed Waits
Both Implicit Wait and Explicit Wait are configured.
Question
What problems can occur?
Scenario 5 – Legacy Framework
The framework contains hundreds of Thread.sleep calls.
Question
How would you improve it?
Scenario 6 – Random Button Failure
The button is sometimes clickable and sometimes not.
Question
How would you make the test stable?
Scenario 7 – Delayed Alert
An alert appears after an unknown amount of time.
Question
How would you synchronize it?
Scenario 8 – Dynamic Table
Rows load gradually after AJAX requests.
Question
Which wait mechanism works best?
Scenario 9 – Jenkins Timeout
Tests fail only during Jenkins execution.
Question
How would you investigate and fix the synchronization issue?
Scenario 10 – Generic Wait Utility
Your framework contains repeated wait logic.
Question
How would you design reusable wait methods?
Senior Tester Tips
- Never use
Thread.sleep()in production automation frameworks. - Prefer Explicit Wait for dynamic web elements.
- Use Fluent Wait for AJAX-heavy applications.
- Use Implicit Wait only as a basic safety mechanism.
- Build reusable wait utilities for framework maintainability.
- Avoid mixing multiple wait strategies unnecessarily.
- Synchronize based on application behavior rather than fixed delays.
Site-Based Practice Exercises
Thread.sleep() Practice
Practice Steps
- Open a dynamic page
- Wait using
Thread.sleep(5000) - Click an element appearing after five seconds
- Replace with Explicit Wait
- Compare execution times
Learning
Thread.sleep introduces unnecessary delays and should be replaced with dynamic waits.
Implicit Wait Practice
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
Practice Steps
- Configure Implicit Wait
- Login to the application
- Locate Dashboard
- Remove Implicit Wait
- Observe element failures
Explicit Wait Practice
Practice waiting for:
- Element visibility
- Element clickability
- Page title
- Alert presence
- Dynamic text
- AJAX-loaded elements
Fluent Wait Practice
Configure Fluent Wait with:
- 20-second timeout
- 2-second polling interval
- Ignore
NoSuchElementException
Practice handling:
- Dynamic tables
- AJAX requests
- Loading indicators
- Delayed text updates
Frequently Asked Questions
How do you switch between browser windows?
Capture the parent window handle, retrieve all window handles, and switch using:
driver.switchTo().window(windowHandle);
How do you close only one browser tab?
Switch to the required window handle, close that window, and switch back to the parent.
Why is Thread.sleep() not recommended?
Thread.sleep pauses execution for a fixed amount of time regardless of whether the element is already available. This increases execution time and causes unstable automation.
When should Fluent Wait be used?
Fluent Wait is best for:
- AJAX applications
- Random loading elements
- Dynamic content
- Custom polling intervals
What synchronization strategy do senior Selenium testers prefer?
Most experienced automation engineers recommend:
- Explicit Wait for dynamic elements
- Fluent Wait for unpredictable loading behavior
- Reusable wait utility methods
- Avoiding unnecessary Thread.sleep() usage