selenium-python-project/ ├── tests/ │ ├── test_login.py │ └── test_search.py ├── pages/ │ ├── login_page.py │ └── search_page.py ├── utils/ │ └── helpers.py ├── config/ │ ├── config.yaml │ └── credentials.yaml ├── drivers/ │ └── chromedriver.exe (or chromedriver for Mac/Linux) ├── requirements.txt ├── pytest.ini └── conftest.py
Python environment setup in Selenium Python - Framework Patterns
- Driver Layer: Manages browser drivers (e.g., ChromeDriver) to control browsers.
- Page Objects: Python classes representing web pages with methods to interact with page elements.
- Tests: Test scripts using pytest that call page objects to perform actions and assertions.
- Utilities: Helper functions for common tasks like waits, logging, or data handling.
- Configuration: Files storing environment settings, URLs, credentials, and browser options.
Use config.yaml to store environment URLs and browser settings. Use credentials.yaml to keep usernames and passwords safe and separate from code.
Use pytest.ini to configure pytest options like markers and test paths.
Use conftest.py to define fixtures that setup and teardown browser sessions and load config data.
Example config.yaml snippet:
environment: base_url: "https://example.com" browser: "chrome"
Use pytest built-in reporting with options for verbose output and junit XML reports.
Integrate with CI/CD tools like GitHub Actions or Jenkins to run tests automatically on code changes.
Example pytest command for reports:
pytest tests/ --junitxml=reports/results.xml -v
Reports help quickly see which tests passed or failed after each run.
- Keep browser drivers in a dedicated
drivers/folder and update them regularly. - Separate test data and credentials from code using config files and environment variables.
- Use pytest fixtures in
conftest.pyfor reusable setup and teardown logic. - Organize tests and page objects clearly to keep code easy to maintain.
- Use explicit waits in Selenium to handle dynamic page elements reliably.
Where in this folder structure would you add a new page object for the Login page?
Practice
Solution
Step 1: Understand virtual environment purpose
A virtual environment keeps project packages separate from global Python packages.Step 2: Relate to Selenium testing
This isolation prevents conflicts between different projects' dependencies, ensuring stable tests.Final Answer:
To isolate project dependencies and avoid conflicts -> Option BQuick Check:
Virtual environment = isolate dependencies [OK]
- Thinking virtual env speeds up browser
- Assuming it writes test scripts automatically
- Believing it changes screen resolution
venv on Windows?Solution
Step 1: Identify Windows activation command
On Windows, virtual environments activate withvenv\Scripts\activate.Step 2: Compare other options
source venv/bin/activateis for Unix/Linux; others are incorrect syntax.Final Answer:
venv\Scripts\activate -> Option CQuick Check:
Windows activate = venv\Scripts\activate [OK]
- Using Unix command on Windows
- Typing activate without path
- Confusing venv creation with activation
import selenium print(selenium.__version__)
What will happen if Selenium is not installed in this environment?
Solution
Step 1: Understand import behavior
If a module is not installed, Python raises ModuleNotFoundError on import.Step 2: Check code effect
Since Selenium is missing,import seleniumfails with error, no print occurs.Final Answer:
It raises a ModuleNotFoundError -> Option AQuick Check:
Missing module import = ModuleNotFoundError [OK]
- Expecting automatic install
- Thinking it prints None
- Assuming version prints without error
venv\Scripts\activate but got an error. What is the likely problem?Solution
Step 1: Identify path style mismatch
Linux uses forward slashes andvenv/bin/activate, not Windows backslashes.Step 2: Understand error cause
Using Windows path on Linux causes 'file not found' error when activating.Final Answer:
Using Windows path on Linux -> Option DQuick Check:
Linux activate path uses forward slashes [OK]
- Assuming virtual env not created
- Blaming Python installation
- Confusing Selenium install with activation
Solution
Step 1: Create virtual environment first
Usepython -m venv envto create isolated environment.Step 2: Activate environment before installing packages
Activate withsource env/bin/activateso pip installs inside it.Step 3: Install Selenium inside activated environment
Runpip install seleniumto add Selenium only to this project.Final Answer:
python -m venv env; source env/bin/activate; pip install selenium -> Option AQuick Check:
Create, activate, then install Selenium [OK]
- Installing Selenium before activating env
- Activating before creating env
- Skipping environment creation
Ready to prove it?
Pass the test and this pattern moves unknown
