0
0
Selenium Javatesting~20 mins

Window handles (getWindowHandles) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Handles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of getWindowHandles() after opening a new tab?

Consider the following Selenium Java code snippet:

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());

What will be printed?

Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
System.out.println(handles.size());
A1
B0
C2
DThrows NoSuchWindowException
Attempts:
2 left
💡 Hint

Remember that getWindowHandles() returns all open windows and tabs.

assertion
intermediate
1:30remaining
Which assertion correctly verifies the number of open windows?

You want to assert that exactly three browser windows are open after some actions. Which assertion is correct?

Selenium Java
Set<String> handles = driver.getWindowHandles();
AassertNull(handles.size() - 3);
BassertEquals(3, handles.size());
CassertFalse(handles.size() != 3);
DassertTrue(handles.size() == 3);
Attempts:
2 left
💡 Hint

Use the assertion that clearly compares expected and actual values.

🔧 Debug
advanced
2:30remaining
Why does switching to a window handle cause NoSuchWindowException?

Given this code snippet:

Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
    driver.switchTo().window(handle);
    driver.close();
}
driver.switchTo().window(handles.iterator().next());

Why does the last line throw NoSuchWindowException?

ABecause all windows were closed in the loop, so the handle is invalid.
BBecause <code>handles.iterator().next()</code> returns null after closing windows.
CBecause <code>switchTo()</code> requires a frame, not a window handle.
DBecause <code>driver.close()</code> does not close windows, only tabs.
Attempts:
2 left
💡 Hint

Think about what happens to window handles after closing windows.

🧠 Conceptual
advanced
1:30remaining
What is the main difference between getWindowHandle() and getWindowHandles()?

Choose the best explanation:

A<code>getWindowHandle()</code> returns the handle of the current window; <code>getWindowHandles()</code> returns handles of all open windows/tabs.
B<code>getWindowHandle()</code> returns all window handles; <code>getWindowHandles()</code> returns the current window handle.
CBoth methods return the same set of window handles.
D<code>getWindowHandle()</code> returns handles of all frames; <code>getWindowHandles()</code> returns handles of all windows.
Attempts:
2 left
💡 Hint

Think about singular vs plural in the method names.

framework
expert
3:00remaining
How to safely switch to a newly opened window in Selenium Java?

You have a test that opens a new window. Which code snippet correctly switches to the new window without errors?

Selenium Java
String originalHandle = driver.getWindowHandle();
// code that opens new window
Set<String> allHandles = driver.getWindowHandles();
Adriver.switchTo().frame(allHandles.iterator().next());
Bdriver.switchTo().window(allHandles.iterator().next());
Cdriver.switchTo().window(originalHandle);
D
for (String handle : allHandles) {
  if (!handle.equals(originalHandle)) {
    driver.switchTo().window(handle);
    break;
  }
}
Attempts:
2 left
💡 Hint

Remember to avoid switching back to the original window.