0
0
Selenium Javatesting~8 mins

Closing browser (close vs quit) in Selenium Java - Framework Approaches Compared

Choose your learning style9 modes available
Framework Mode - Closing browser (close vs quit)
Folder Structure of Selenium Java Test Framework
src/
 └── test/
     └── java/
         ├── com/
         │   └── example/
         │       ├── pages/
         │       │    └── LoginPage.java
         │       ├── tests/
         │       │    └── LoginTest.java
         │       ├── utils/
         │       │    └── WebDriverManager.java
         │       └── config/
         │            └── ConfigReader.java
 └── resources/
     └── testdata/
         └── testdata.csv
Test Framework Layers
  • Driver Layer: Manages WebDriver instances, browser setup, and teardown (e.g., WebDriverManager.java).
  • Page Objects: Classes representing web pages with locators and actions (e.g., LoginPage.java).
  • Tests: Test classes using test frameworks like TestNG or JUnit to run test cases (e.g., LoginTest.java).
  • Utilities: Helper classes for common functions like reading configs, waits, screenshots.
  • Configuration: Files and classes to manage environment settings, browser types, credentials.

Closing Browser Methods:

  • driver.close(): Closes the current browser window. If multiple windows are open, others stay open.
  • driver.quit(): Closes all browser windows and ends the WebDriver session cleanly.
Configuration Patterns
  • Environment Properties: Use config.properties to set URLs, browser types, timeouts.
  • Browser Selection: Pass browser name as a parameter or from config to WebDriverManager to launch desired browser.
  • Credentials: Store securely in config files or environment variables, accessed via ConfigReader.
  • Closing Browser: Decide in test teardown whether to use close() or quit() based on test scope.
Test Reporting and CI/CD Integration
  • Use TestNG or JUnit reports to show test pass/fail results.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests automatically on code changes.
  • Ensure WebDriver sessions are properly closed with quit() to avoid resource leaks in CI environments.
  • Generate HTML or XML reports for easy review.
Best Practices for Closing Browser in Selenium Java Framework
  1. Always use driver.quit() in test teardown: It closes all windows and ends the session, freeing resources.
  2. Use driver.close() only when handling multiple windows: To close a specific window without quitting the session.
  3. Implement browser closing in a centralized place: For example, in an @AfterMethod or @AfterClass annotated method in TestNG.
  4. Handle exceptions during closing: Wrap close/quit calls in try-catch to avoid test failures due to closing errors.
  5. Do not leave browser windows open: This can cause memory leaks and flaky tests.
Self-Check Question

Where in this folder structure would you add the code to properly close the browser after each test?

src/test/java/com/example/tests/LoginTest.java

Or better, in a base test class or utility under tests/ or utils/ to reuse the closing logic.

Key Result
Use driver.quit() in test teardown to close all browser windows and end the WebDriver session cleanly.