Python environment setup in Selenium Python - Build an Automation Script
import unittest from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import sys class TestPythonEnvironmentSetup(unittest.TestCase): def setUp(self): # Path to your chromedriver executable chromedriver_path = './chromedriver' # Adjust path as needed service = Service(chromedriver_path) self.driver = webdriver.Chrome(service=service) self.driver.maximize_window() def test_open_example_com(self): self.driver.get('https://example.com') # Wait until title contains 'Example Domain' WebDriverWait(self.driver, 10).until(EC.title_contains('Example Domain')) title = self.driver.title self.assertIn('Example Domain', title, f"Page title was '{title}' but expected to contain 'Example Domain'") def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Python's unittest framework and Selenium WebDriver to automate the manual test case.
setUp(): Initializes the Chrome WebDriver before each test. The Service object is used to specify the path to the ChromeDriver executable. The browser window is maximized for better visibility.
test_open_example_com(): Opens https://example.com and waits explicitly up to 10 seconds for the page title to contain 'Example Domain'. Then it asserts that the title contains the expected text.
tearDown(): Closes the browser after the test to clean up resources.
This structure ensures environment setup and cleanup are handled properly, and the assertion verifies the environment is correctly set up to run Selenium tests.
Now add data-driven testing to open three different URLs and verify their titles
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
