0
0
Selenium Pythontesting~15 mins

Python environment setup in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify Python Selenium environment setup by opening a browser and navigating to example.com
Preconditions (4)
Step 1: Open a terminal or command prompt
Step 2: Create a new virtual environment named 'venv' using 'python -m venv venv'
Step 3: Activate the virtual environment
Step 4: Install Selenium package using 'pip install selenium'
Step 5: Write a Python script that imports Selenium WebDriver
Step 6: In the script, initialize Chrome WebDriver with the correct path to ChromeDriver
Step 7: Use the driver to open the URL 'https://example.com'
Step 8: Verify that the page title contains the word 'Example Domain'
Step 9: Close the browser
✅ Expected Result: The script runs without errors, opens Chrome browser, navigates to https://example.com, verifies the page title contains 'Example Domain', and closes the browser
Automation Requirements - unittest with Selenium WebDriver
Assertions Needed:
Page title contains 'Example Domain'
Best Practices:
Use explicit waits if needed
Use setUp and tearDown methods for environment setup and cleanup
Use virtual environment for package isolation
Use correct ChromeDriver path
Handle exceptions gracefully
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
Not activating the virtual environment before installing Selenium
Using hardcoded absolute paths for ChromeDriver without considering different machines
Not waiting for the page to load or title to be present before asserting
Not closing the browser after test execution
Bonus Challenge

Now add data-driven testing to open three different URLs and verify their titles

Show Hint