0
0
Selenium Pythontesting~15 mins

Running Selenium in CI pipeline in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Run Selenium test on login page in CI pipeline
Preconditions (3)
Step 1: Open the login page URL in a browser
Step 2: Enter 'admin@test.com' in the email input field with id 'email'
Step 3: Enter 'Pass123!' in the password input field with id 'password'
Step 4: Click the login button with id 'login-btn'
Step 5: Wait for the page to load and verify the URL contains '/dashboard'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page URL containing '/dashboard'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Best Practices:
Use explicit waits to wait for elements and page load
Use By.ID locator strategy for stable element selection
Use setup and teardown methods to initialize and quit WebDriver
Run tests headlessly in CI environment
Use environment variables or config for URLs and credentials
Automated Solution
Selenium Python
import os
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoginTest(unittest.TestCase):
    def setUp(self):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        self.driver = webdriver.Chrome(options=chrome_options)
        self.driver.implicitly_wait(5)
        self.base_url = os.getenv('BASE_URL', 'https://example.com/login')

    def test_login(self):
        driver = self.driver
        driver.get(self.base_url)

        email_input = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'email'))
        )
        email_input.send_keys('admin@test.com')

        password_input = driver.find_element(By.ID, 'password')
        password_input.send_keys('Pass123!')

        login_button = driver.find_element(By.ID, 'login-btn')
        login_button.click()

        WebDriverWait(driver, 10).until(
            EC.url_contains('/dashboard')
        )

        self.assertIn('/dashboard', driver.current_url)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

This script uses Python's unittest framework with Selenium WebDriver.

In setUp, we configure Chrome to run headlessly for CI environments and set the base URL from an environment variable for flexibility.

The test_login method opens the login page, waits explicitly for the email input to appear, enters credentials, clicks login, then waits for the URL to contain '/dashboard' to confirm successful login.

Assertions check the URL to verify the test passed.

Finally, tearDown quits the browser to clean up.

This structure ensures the test runs reliably in a CI pipeline without opening a visible browser window.

Common Mistakes - 5 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It causes flaky tests because fixed waits may be too short or too long depending on environment speed.', 'correct_approach': "Use Selenium's WebDriverWait with expected conditions to wait dynamically for elements or URL changes."}
Hardcoding URLs and credentials in the test code
{'mistake': 'Running tests with visible browser windows in CI', 'why_bad': 'CI servers usually have no display, causing tests to fail or hang.', 'correct_approach': "Run browsers in headless mode using options like ChromeOptions with '--headless'."}
Not quitting the WebDriver after tests
Using brittle locators like absolute XPaths
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials to verify both successful and unsuccessful login attempts.

Show Hint