0
0
Selenium Pythontesting~15 mins

Why CI integration enables continuous testing in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Verify login functionality with CI integration for continuous testing
Preconditions (3)
Step 1: Open the login page URL in a browser
Step 2: Enter 'testuser@example.com' in the email input field
Step 3: Enter 'TestPass123!' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
Step 6: Verify the URL contains '/dashboard'
Step 7: Verify the welcome message 'Welcome, Test User!' is displayed
✅ Expected Result: User is successfully logged in, dashboard page loads with correct URL and welcome message, and test passes automatically in CI pipeline on code push
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL contains '/dashboard'
Verify welcome message text is 'Welcome, Test User!'
Best Practices:
Use explicit waits to wait for elements
Use Page Object Model to separate page locators and actions
Use assertions from unittest or pytest
Integrate test script with CI pipeline to run on code push
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'login-btn')

    def enter_email(self, email):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.email_input)
        ).send_keys(email)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.password_input)
        ).send_keys(password)

    def click_login(self):
        WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.login_button)
        ).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver
        self.welcome_message = (By.ID, 'welcome-msg')

    def get_welcome_text(self):
        return WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.welcome_message)
        ).text

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')

    def test_login_success(self):
        login_page = LoginPage(self.driver)
        login_page.enter_email('testuser@example.com')
        login_page.enter_password('TestPass123!')
        login_page.click_login()

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

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

        dashboard_page = DashboardPage(self.driver)
        welcome_text = dashboard_page.get_welcome_text()
        self.assertEqual(welcome_text, 'Welcome, Test User!')

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

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

This script uses Selenium with Python's unittest framework to automate the login test.

Page Object Model: We created LoginPage and DashboardPage classes to keep locators and actions organized. This makes maintenance easier.

Explicit waits: We wait for elements to be visible or clickable before interacting. This avoids flaky tests.

Assertions: We check that the URL contains '/dashboard' and the welcome message text matches exactly.

Setup and teardown: The browser opens before each test and closes after to keep tests isolated.

This test can be integrated into a CI pipeline to run automatically on every code push, enabling continuous testing by catching issues early.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'It makes tests slower and flaky because it waits fixed time regardless of element readiness.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions."}
Hardcoding XPath selectors that are brittle
Not closing the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid).

Show Hint