Bird
Raised Fist0
Selenium Javatesting~20 mins

Closing browser (close vs quit) in Selenium Java - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Browser Closing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Difference between close() and quit() in Selenium
Which statement correctly describes the difference between driver.close() and driver.quit() in Selenium WebDriver?
A<code>close()</code> closes all browser windows and ends the WebDriver session, while <code>quit()</code> closes only the current window.
B<code>close()</code> refreshes the current page, while <code>quit()</code> closes the browser window.
C<code>close()</code> closes the current browser window, while <code>quit()</code> closes all browser windows and ends the WebDriver session.
D<code>close()</code> logs out the user from the application, while <code>quit()</code> closes the browser window.
Attempts:
2 left
💡 Hint
Think about what happens when multiple tabs or windows are open.
❓ Predict Output
intermediate
2:00remaining
Output after calling close() on single window
What will happen if the following Selenium Java code is executed when only one browser window is open?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.close();
driver.getTitle();
AReturns the page title of https://example.com successfully.
BDoes nothing; the browser remains open and getTitle() returns null.
COpens a new browser window automatically and returns its title.
DThrows an exception because the browser window is closed, so getTitle() fails.
Attempts:
2 left
💡 Hint
Consider what happens to the WebDriver session after closing the only window.
❓ assertion
advanced
2:00remaining
Assertion to verify browser is closed after quit()
Which assertion correctly verifies that the browser session is closed after calling driver.quit() in Selenium Java?
Selenium Java
driver.quit();
// Which assertion below is valid?
AassertThrows(NoSuchSessionException.class, () -> driver.getTitle());
BassertEquals("", driver.getTitle());
CassertTrue(driver.getWindowHandles().isEmpty());
DassertNotNull(driver.getTitle());
Attempts:
2 left
💡 Hint
After quitting, the session no longer exists.
🔧 Debug
advanced
2:30remaining
Debugging error after close() on multiple windows
Given the code below, what is the cause of the error when trying to switch to the second window after calling driver.close()?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
// Open new tab
((JavascriptExecutor)driver).executeScript("window.open('https://google.com','_blank');");
Set<String> handles = driver.getWindowHandles();
Iterator<String> it = handles.iterator();
String first = it.next();
String second = it.next();
driver.switchTo().window(first);
driver.close();
driver.switchTo().window(second);
AThe driver.quit() was not called, so the session is invalid.
BAfter closing the first window, the driver still tries to switch to it, causing NoSuchWindowException.
CThe driver.close() closes all windows, so second window does not exist.
DThe window handles set is empty after close(), so switching fails.
Attempts:
2 left
💡 Hint
Think about which window is active after close() and what windows remain.
❓ framework
expert
3:00remaining
Best practice for closing browser in test framework
In a Selenium Java test framework using JUnit, which is the best practice to ensure all browser windows are closed after each test, even if the test fails?
ACall <code>driver.quit()</code> inside a method annotated with <code>@AfterEach</code>.
BCall <code>driver.close()</code> at the end of each test method.
CCall <code>driver.quit()</code> inside the test method only if no exceptions occur.
DDo not close the browser; let the OS clean up after tests finish.
Attempts:
2 left
💡 Hint
Think about test lifecycle and resource cleanup.

Practice

(1/5)
1. What does the close() method do in Selenium WebDriver?
easy
A. Closes the current browser window or tab only
B. Closes all browser windows and ends the WebDriver session
C. Keeps the browser open but clears cookies
D. Minimizes the browser window

Solution

  1. Step 1: Understand the purpose of close()

    The close() method is designed to close only the current browser window or tab that the WebDriver is controlling.
  2. Step 2: Differentiate from quit()

    Unlike quit(), which closes all windows and ends the session, close() affects only one window or tab.
  3. Final Answer:

    Closes the current browser window or tab only -> Option A
  4. Quick Check:

    close() closes one window [OK]
Hint: Close closes one window; quit closes all [OK]
Common Mistakes:
  • Confusing close() with quit()
  • Thinking close() ends the WebDriver session
  • Assuming close() clears cookies
2. Which of the following is the correct syntax to close all browser windows and end the WebDriver session in Java Selenium?
easy
A. driver.close();
B. driver.quit();
C. driver.exit();
D. driver.shutdown();

Solution

  1. Step 1: Recall the method to end WebDriver session

    The method quit() is used to close all browser windows and end the WebDriver session.
  2. Step 2: Check syntax correctness

    The correct syntax in Java Selenium is driver.quit();. Other options like exit() or shutdown() do not exist in WebDriver API.
  3. Final Answer:

    driver.quit(); -> Option B
  4. Quick Check:

    Use quit() to close all windows [OK]
Hint: Use quit() to close all windows and end session [OK]
Common Mistakes:
  • Using close() instead of quit() to end session
  • Using non-existent methods like exit() or shutdown()
  • Missing parentheses in method call
3. Consider the following Java Selenium code snippet:
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
driver.close();
driver.get("https://google.com");

What will happen when this code runs?
medium
A. The browser opens example.com and google.com in the same tab without closing
B. The browser opens example.com, closes the tab, then opens google.com in a new window
C. The browser opens example.com and google.com in two tabs without closing any
D. The browser opens example.com, closes the tab, then throws an exception on the next get() call

Solution

  1. Step 1: Analyze the effect of driver.close()

    The close() method closes the current browser window or tab but does not end the WebDriver session. However, if it is the only window, the session becomes invalid.
  2. Step 2: Understand the next get() call after close()

    After closing the only window, calling driver.get() will cause a NoSuchWindowException or similar because there is no open window to navigate.
  3. Final Answer:

    The browser opens example.com, closes the tab, then throws an exception on the next get() call -> Option D
  4. Quick Check:

    close() closes window; next get() fails if no window [OK]
Hint: Close closes tab; next commands fail if no window open [OK]
Common Mistakes:
  • Assuming get() opens new window after close()
  • Thinking close() ends session like quit()
  • Ignoring exceptions after closing last window
4. You have this Java Selenium code:
driver.close();
driver.quit();

What is the problem with this code?
medium
A. No problem; calling close() before quit() works without throwing exceptions
B. close() and quit() do the same thing, so one is redundant
C. Calling quit() before close() is required to avoid errors
D. Calling close() before quit() may cause quit() to throw an exception if no windows remain

Solution

  1. Step 1: Understand the effect of close()

    Calling close() closes the current window. If it is the last window, no windows remain open, but the WebDriver session remains active.
  2. Step 2: Analyze quit() after close()

    Calling quit() after close() safely terminates the session even with no windows left; no exception is thrown.
  3. Final Answer:

    No problem; calling close() before quit() works without throwing exceptions -> Option A
  4. Quick Check:

    close() then quit() succeeds [OK]
Hint: close() followed by quit() is safe; no exceptions [OK]
Common Mistakes:
  • Thinking close() before quit() throws exception
  • Confusing close() and quit() effects
  • Believing quit() must precede close()
5. You want to close only the current tab but keep the WebDriver session active to open a new tab later. Which method should you use and why?
hard
A. Use driver.quit() because it closes all tabs and ends session cleanly
B. Use driver.quit() and then create a new WebDriver instance
C. Use driver.close() because it closes only the current tab and keeps session alive
D. Use driver.close() and then call driver.quit() immediately

Solution

  1. Step 1: Identify the requirement to keep session alive

    To keep the WebDriver session active, you must not call quit() because it ends the session and closes all windows.
  2. Step 2: Choose method that closes only current tab

    The close() method closes only the current tab or window but keeps the WebDriver session alive, allowing further commands like opening new tabs.
  3. Final Answer:

    Use driver.close() because it closes only the current tab and keeps session alive -> Option C
  4. Quick Check:

    close() closes tab; quit() ends session [OK]
Hint: Close closes tab; quit ends session completely [OK]
Common Mistakes:
  • Using quit() and losing session
  • Calling quit() then expecting to open new tabs
  • Confusing close() with quit() effects
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