0
0
Selenium Javatesting~20 mins

Creating new windows/tabs in Selenium Java - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tab Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that opens a new tab and switches to it. What will be the output printed?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();

// Open new tab
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");

Set<String> handles = driver.getWindowHandles();
handles.remove(originalHandle);
String newTabHandle = handles.iterator().next();

driver.switchTo().window(newTabHandle);
System.out.println(driver.getCurrentUrl());
Ahttps://example.com
BThrows NoSuchWindowException
Cabout:blank
Dhttps://google.com
Attempts:
2 left
💡 Hint
Remember that window.open with '_blank' opens a new tab with the given URL.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the number of open windows after opening a new tab?
After opening a new tab using Selenium WebDriver, you want to assert that exactly two windows/tabs are open. Which assertion is correct?
Selenium Java
driver.get("https://example.com");
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
AassertEquals(2, handles.size());
BassertTrue(handles.size() == 1);
CassertFalse(handles.size() > 2);
DassertEquals(1, handles.size());
Attempts:
2 left
💡 Hint
Count of window handles increases by one after opening a new tab.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java code fail to switch to the new tab?
Identify the reason why the following code fails to switch to the newly opened tab.
Selenium Java
driver.get("https://example.com");
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
String newTabHandle = driver.getWindowHandle();
driver.switchTo().window(newTabHandle);
System.out.println(driver.getCurrentUrl());
Adriver.switchTo().window() requires an integer index, not a string
Bdriver.getWindowHandle() returns the original window handle, not the new tab handle
CJavaScriptExecutor cannot open new tabs
Dwindow.open does not open a new tab in Selenium
Attempts:
2 left
💡 Hint
Check what getWindowHandle() returns after opening a new tab.
🧠 Conceptual
advanced
1:30remaining
What is the best practice to close a newly opened tab and return to the original window?
After opening a new tab and switching to it, you want to close the new tab and switch back to the original window. Which sequence is correct?
Adriver.quit(); driver.switchTo().window(originalHandle);
Bdriver.switchTo().window(originalHandle); driver.close();
Cdriver.close(); driver.switchTo().window(originalHandle);
Ddriver.close(); driver.quit();
Attempts:
2 left
💡 Hint
Close the current tab first, then switch back to the original window.
framework
expert
2:30remaining
In a Selenium Java test framework, how to reliably wait for a new tab to open before switching?
You want to wait until a new tab opens after clicking a link that opens a new tab. Which approach is best to wait for the new tab handle before switching?
AUse WebDriverWait with ExpectedCondition that waits until window handles size is greater than original count
BUse Thread.sleep(5000) to wait 5 seconds before switching
CImmediately call driver.switchTo().window(newTabHandle) without waiting
DUse driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) to wait for new tab
Attempts:
2 left
💡 Hint
Waiting for a condition is better than fixed sleep or implicit wait for new tabs.