Recall & Review
beginner
What is the purpose of switching between windows in Selenium?
Switching between windows allows the test to control and interact with multiple browser windows or tabs opened during a test session.
Click to reveal answer
beginner
How do you get all window handles in Selenium WebDriver?
Use
driver.getWindowHandles() which returns a Set of strings representing all open window handles.Click to reveal answer
intermediate
Explain the difference between
getWindowHandle() and getWindowHandles().getWindowHandle() returns the handle of the current window. getWindowHandles() returns handles of all open windows.Click to reveal answer
intermediate
Show a simple Java code snippet to switch to a new window in Selenium.
String originalWindow = driver.getWindowHandle();
for (String windowHandle : driver.getWindowHandles()) {
if (!windowHandle.equals(originalWindow)) {
driver.switchTo().window(windowHandle);
break;
}
}
Click to reveal answer
beginner
Why is it important to switch back to the original window after working with a new window?
Because Selenium commands act on the current window. To continue testing the original page, you must switch back to its window handle.
Click to reveal answer
Which Selenium method returns all open window handles?
✗ Incorrect
driver.getWindowHandles() returns a Set of all window handles currently open.
What does
driver.switchTo().window(handle) do?✗ Incorrect
This command changes Selenium's control to the specified window.
If you want to switch back to the original window after opening a new one, what should you do?
✗ Incorrect
You must switch Selenium's focus back to the original window handle.
What data type does
driver.getWindowHandles() return?✗ Incorrect
It returns a Set of strings representing window handles.
Why might you need to switch windows during a test?
✗ Incorrect
Switching windows lets you control pop-ups or new tabs opened during testing.
Describe the steps to switch from the original window to a newly opened window in Selenium.
Think about how to identify the new window among all open windows.
You got /4 concepts.
Explain why switching back to the original window is important after working with a new window.
Consider what happens if Selenium stays focused on the wrong window.
You got /3 concepts.