selenium-python-framework/ ├── src/ │ ├── pages/ │ │ ├── __init__.py │ │ └── login_page.py │ ├── tests/ │ │ ├── __init__.py │ │ └── test_login.py │ ├── utils/ │ │ ├── __init__.py │ │ └── browser_manager.py │ └── config/ │ ├── __init__.py │ └── settings.py ├── requirements.txt ├── pytest.ini └── README.md
Why browser control is the foundation in Selenium Python - Framework Benefits
- Browser Control Layer: Manages starting, stopping, and controlling the browser using Selenium WebDriver. This is the foundation because it allows tests to interact with the web application.
- Page Object Layer: Contains classes representing web pages. Uses browser control to find and interact with page elements.
- Test Layer: Contains test cases that use page objects to perform actions and verify results.
- Utilities Layer: Helper functions like browser setup, waits, and logging.
- Configuration Layer: Stores environment settings, browser options, and credentials.
Use a settings.py file to store environment URLs, browser types, and credentials. Use environment variables or command-line options to switch between environments (e.g., dev, staging, production) and browsers (e.g., Chrome, Firefox).
Example snippet in settings.py:
ENVIRONMENTS = {
"dev": "https://dev.example.com",
"staging": "https://staging.example.com",
"prod": "https://www.example.com"
}
BROWSER = "chrome" # or "firefox"
CREDENTIALS = {
"username": "testuser",
"password": "password123"
}Browser control code reads these settings to launch the correct browser and navigate to the right URL.
Use pytest with plugins like pytest-html to generate readable test reports showing pass/fail results and screenshots on failure.
Integrate the test suite into CI/CD pipelines (e.g., GitHub Actions, Jenkins) to run tests automatically on code changes. Browser control ensures tests run in real browsers, catching UI issues early.
- Centralize Browser Control: Manage browser setup and teardown in one place to avoid duplication and errors.
- Use Explicit Waits: Wait for elements to be ready before interacting to avoid flaky tests.
- Keep Tests Independent: Each test should start with a fresh browser state to prevent side effects.
- Parameterize Browsers and Environments: Make it easy to run tests on different browsers and environments without code changes.
- Clean Up Resources: Always close browsers after tests to free system resources.
Where in this folder structure would you add a new browser setup for running tests on Firefox?
Answer: In src/utils/browser_manager.py, update or add functions to initialize the Firefox WebDriver using configuration settings.
Practice
Solution
Step 1: Understand browser control role
Browser control lets Selenium open and interact with web pages just like a user would.Step 2: Compare options
Options B, C, and D are incorrect because browser control does not only test backend, does not fully replace manual testing, and usually opens a browser window.Final Answer:
Because it allows automated interaction with web pages like a real user -> Option AQuick Check:
Browser control = user-like interaction [OK]
- Thinking browser control only tests backend
- Believing it fully replaces manual testing
- Assuming tests run without opening a browser
Solution
Step 1: Recall Selenium syntax for Chrome
The correct syntax to start Chrome is using webdriver.Chrome() with capital C in Chrome.Step 2: Check incorrect options
Options A, C, and D have wrong order or wrong case which causes syntax errors.Final Answer:
driver = webdriver.Chrome() -> Option CQuick Check:
Correct class and method names = driver = webdriver.Chrome() [OK]
- Using lowercase 'chrome' instead of 'Chrome'
- Swapping order of webdriver and Chrome
- Calling methods that don't exist
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://example.com')
element = driver.find_element(By.TAG_NAME, 'h1')
print(element.text)
driver.quit()Solution
Step 1: Understand code flow
The code opens Chrome, navigates to example.com, finds the first <h1> tag, prints its text, then closes browser.Step 2: Analyze output possibilities
Since find_element is correct and element.text returns the text inside <h1>, it prints that text. driver.quit() runs after print, so output appears.Final Answer:
Prints the text inside the first <h1> tag on example.com -> Option BQuick Check:
find_element + element.text = printed text [OK]
- Thinking driver.quit() stops print output
- Confusing find_element syntax
- Expecting URL instead of element text
from selenium import webdriver
driver = webdriver.Chrome
driver.get('https://example.com')Solution
Step 1: Check webdriver.Chrome usage
webdriver.Chrome is a class and must be called with parentheses to create an instance.Step 2: Verify other parts
Import is correct, URL is valid, and driver.get() is a valid method. The only error is missing parentheses.Final Answer:
Missing parentheses after webdriver.Chrome -> Option AQuick Check:
Instantiate webdriver.Chrome() with () [OK]
- Forgetting parentheses after class name
- Assuming driver.get() is invalid
- Thinking import is wrong
Solution
Step 1: Understand browser control purpose
Browser control is needed to open the web page and simulate user actions like typing and clicking.Step 2: Evaluate other options
Options A, B, and C are incorrect because browser control does not replace assertions, does not auto-validate inputs, and does not run tests without browser window by default.Final Answer:
Because browser control lets you open the page and interact with elements like a user -> Option DQuick Check:
Browser control = user interaction base [OK]
- Thinking browser control auto-validates inputs
- Believing tests run without browser window
- Assuming no need for assertions
Ready to prove it?
Pass the test and this pattern moves unknown
