Bird
Raised Fist0
Selenium Javatesting~20 mins

Creating new windows/tabs in Selenium Java - Practice Exercises

Choose your learning style10 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.

Practice

(1/5)
1. Which Selenium Java command is used to open a new browser tab?
easy
A. driver.switchTo().newWindow(WindowType.TAB)
B. driver.openNewTab()
C. driver.createTab()
D. driver.newWindow(Tab)

Solution

  1. Step 1: Understand Selenium's new window/tab creation

    Selenium provides a method driver.switchTo().newWindow() to open new tabs or windows.
  2. Step 2: Identify the correct WindowType for a tab

    Using WindowType.TAB opens a new tab, which matches driver.switchTo().newWindow(WindowType.TAB).
  3. Final Answer:

    driver.switchTo().newWindow(WindowType.TAB) -> Option A
  4. Quick Check:

    Open new tab = driver.switchTo().newWindow(WindowType.TAB) [OK]
Hint: Use driver.switchTo().newWindow(WindowType.TAB) for new tabs [OK]
Common Mistakes:
  • Using non-existent methods like driver.openNewTab()
  • Confusing WindowType.WINDOW with TAB
  • Trying to create tabs without switchTo()
2. Which of the following is the correct syntax to open a new browser window in Selenium Java?
easy
A. driver.newWindow(WindowType.WINDOW);
B. driver.switchTo().newWindow(WindowType.WINDOW);
C. driver.switchTo().openWindow(WindowType.WINDOW);
D. driver.openNewWindow(WindowType.WINDOW);

Solution

  1. Step 1: Recall the correct method to open new windows

    The correct method is driver.switchTo().newWindow() with a WindowType argument.
  2. Step 2: Match the exact syntax

    driver.switchTo().newWindow(WindowType.WINDOW); matches the correct syntax exactly with WindowType.WINDOW.
  3. Final Answer:

    driver.switchTo().newWindow(WindowType.WINDOW); -> Option B
  4. Quick Check:

    Open new window = driver.switchTo().newWindow(WindowType.WINDOW) [OK]
Hint: Use switchTo().newWindow(WindowType.WINDOW) to open new windows [OK]
Common Mistakes:
  • Omitting switchTo() before newWindow()
  • Using incorrect method names like openWindow()
  • Missing semicolon in Java syntax
3. What will be the output of the following Selenium Java code snippet?
driver.get("https://example.com");
String originalHandle = driver.getWindowHandle();
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://selenium.dev");
System.out.println(driver.getCurrentUrl());
driver.switchTo().window(originalHandle);
System.out.println(driver.getCurrentUrl());
medium
A. https://selenium.dev https://selenium.dev
B. https://example.com https://selenium.dev
C. https://selenium.dev https://example.com
D. https://example.com https://example.com

Solution

  1. Step 1: Understand window handles and tab switching

    The code opens example.com, saves the original window handle, then opens a new tab and navigates to selenium.dev.
  2. Step 2: Analyze the printed URLs

    First print shows the URL of the new tab (selenium.dev). Then it switches back to the original window and prints its URL (example.com).
  3. Final Answer:

    https://selenium.dev https://example.com -> Option C
  4. Quick Check:

    Switch tabs = different URLs printed [OK]
Hint: Switching tabs changes current URL output [OK]
Common Mistakes:
  • Assuming URLs remain the same after switching tabs
  • Not saving original window handle before opening new tab
  • Confusing getWindowHandle() with getWindowHandles()
4. Identify the error in the following Selenium Java code snippet that tries to open a new tab:
driver.get("https://example.com");
driver.newWindow(WindowType.TAB);
driver.get("https://selenium.dev");
medium
A. No error, code is correct
B. WindowType.TAB is invalid
C. driver.get() cannot be used after newWindow()
D. Missing driver.switchTo() before newWindow()

Solution

  1. Step 1: Check method usage for opening new tabs

    The method newWindow() must be called after driver.switchTo().
  2. Step 2: Identify the missing part in the code

    The code calls driver.newWindow() directly, which is incorrect syntax.
  3. Final Answer:

    Missing driver.switchTo() before newWindow() -> Option D
  4. Quick Check:

    Always use switchTo().newWindow() [OK]
Hint: Always use driver.switchTo() before newWindow() [OK]
Common Mistakes:
  • Calling newWindow() directly on driver
  • Confusing WindowType.TAB as invalid
  • Thinking driver.get() is restricted after newWindow()
5. You want to open a new tab, navigate to a URL, then switch back to the original tab and verify its title. Which code snippet correctly does this in Selenium Java?
hard
A. String original = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
B. driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); String original = driver.getWindowHandle(); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
C. String original = driver.getWindowHandle(); driver.newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example");
D. driver.get("https://selenium.dev"); driver.switchTo().newWindow(WindowType.TAB); driver.switchTo().window(original); assert driver.getTitle().contains("Example");

Solution

  1. Step 1: Save original window handle before opening new tab

    We must save the original window handle before opening a new tab to switch back later.
  2. Step 2: Open new tab, navigate, then switch back and assert title

    String original = driver.getWindowHandle(); driver.switchTo().newWindow(WindowType.TAB); driver.get("https://selenium.dev"); driver.switchTo().window(original); assert driver.getTitle().contains("Example"); correctly calls switchTo().newWindow(WindowType.TAB), navigates, switches back using saved handle, and asserts title.
  3. Final Answer:

    Option A code snippet correctly performs all steps -> Option A
  4. Quick Check:

    Save handle, open tab, switch back, assert [OK]
Hint: Save handle before new tab, switch back to assert [OK]
Common Mistakes:
  • Saving window handle after opening new tab
  • Calling newWindow() without switchTo()
  • Not switching back before assertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard