0
0
Selenium Pythontesting~8 mins

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

Choose your learning style9 modes available
Framework Mode - Closing browser (close vs quit)
Folder Structure
selenium_project/
├── tests/
│   ├── test_login.py
│   └── test_checkout.py
├── pages/
│   ├── base_page.py
│   ├── login_page.py
│   └── checkout_page.py
├── utils/
│   ├── browser_manager.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── test_report.html
└── conftest.py
Test Framework Layers
  • Driver Layer: Manages browser sessions using Selenium WebDriver. Handles opening, closing, and quitting browsers.
  • Page Objects: Classes representing web pages. They use the driver to interact with page elements.
  • Tests: Test scripts that use page objects and driver layer to perform test scenarios.
  • Utilities: Helper functions and browser management code, including methods to close or quit browsers properly.
  • Configuration: Stores environment settings, browser types, and credentials for flexible test runs.
Configuration Patterns
  • Environment Settings: Use config/config.yaml to define URLs, browser types (e.g., Chrome, Firefox), and timeouts.
  • Browser Choice: Parameterize browser selection in browser_manager.py to support multiple browsers.
  • Credentials: Store securely in credentials.yaml and load them in tests or fixtures.
  • Session Management: Use fixtures in conftest.py to initialize and clean up browser sessions, deciding when to call close() or quit().
Test Reporting and CI/CD Integration
  • Generate HTML reports in reports/ folder using pytest-html or similar plugins.
  • Integrate with CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests on code commits.
  • Ensure browser sessions are properly closed or quit after tests to avoid resource leaks in CI environments.
  • Use logs to capture browser open/close events for debugging test failures.
Framework Design Principles
  1. Use driver.close() to close the current browser window only. This is useful when multiple windows or tabs are open and you want to close one without ending the session.
  2. Use driver.quit() to end the entire browser session. This closes all windows and safely ends the WebDriver session, freeing resources.
  3. Always call quit() in test teardown or fixture finalizers. This prevents orphan browser processes and memory leaks.
  4. Manage browser lifecycle centrally in utilities or fixtures. Avoid calling close or quit randomly in tests to keep tests clean and maintainable.
  5. Document the difference clearly for team members. Helps avoid confusion and bugs related to browser session handling.
Self Check

Where in this framework structure would you add a method to safely close the current browser window without quitting the entire session?

Key Result
Use driver.close() to close one window and driver.quit() to end the full browser session, managing them centrally in utilities or fixtures.