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?
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());
Remember that getWindowHandles() returns all open windows and tabs.
Initially, there is one window. After opening a new tab, there are two windows/tabs. So getWindowHandles() returns a set of size 2.
You want to assert that exactly three browser windows are open after some actions. Which assertion is correct?
Set<String> handles = driver.getWindowHandles();
Use the assertion that clearly compares expected and actual values.
assertEquals(3, handles.size()); directly checks that the size is 3. Option B also works logically but is less clear. Option B is invalid because handles.size() - 3 is an int, never null.
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?
Think about what happens to window handles after closing windows.
The loop closes all windows, so no window handles remain valid. Trying to switch to any handle after closing all windows causes NoSuchWindowException.
Choose the best explanation:
Think about singular vs plural in the method names.
getWindowHandle() returns a single String for the current window. getWindowHandles() returns a Set of Strings for all open windows/tabs.
You have a test that opens a new window. Which code snippet correctly switches to the new window without errors?
String originalHandle = driver.getWindowHandle(); // code that opens new window Set<String> allHandles = driver.getWindowHandles();
Remember to avoid switching back to the original window.
Option D loops through all handles and switches to the one that is not the original. Option D may switch back to the original window. Option D switches to the original window, not the new one. Option D tries to switch to a frame using a window handle, causing an error.