0
0
Selenium Javatesting~20 mins

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

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