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