0
0
Selenium Pythontesting~15 mins

Data providers pattern in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Login functionality test with multiple user credentials
Preconditions (2)
Step 1: Open the login page URL.
Step 2: For each set of credentials:
Step 3: - Enter the username in the username input field with id 'username'.
Step 4: - Enter the password in the password input field with id 'password'.
Step 5: - Click the login button with id 'loginBtn'.
Step 6: - Verify the result:
Step 7: - If credentials are valid, the URL should change to 'https://example.com/dashboard'.
Step 8: - If credentials are invalid, an error message with id 'errorMsg' should be visible.
✅ Expected Result: For each credential set, the login behaves correctly: successful login redirects to dashboard, failed login shows error message.
Automation Requirements - selenium with unittest
Assertions Needed:
Assert current URL is dashboard URL for valid credentials
Assert error message is displayed for invalid credentials
Best Practices:
Use data provider pattern to supply multiple credentials
Use explicit waits to wait for elements or URL changes
Use Page Object Model to separate page interactions
Keep test code clean and readable
Automated Solution
Selenium Python
import unittest
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

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'https://example.com/login'
        self.username_input = (By.ID, 'username')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')
        self.error_message = (By.ID, 'errorMsg')

    def load(self):
        self.driver.get(self.url)

    def login(self, username, password):
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.username_input)
        )
        self.driver.find_element(*self.username_input).clear()
        self.driver.find_element(*self.username_input).send_keys(username)
        self.driver.find_element(*self.password_input).clear()
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.login_button).click()

    def is_error_displayed(self):
        try:
            return WebDriverWait(self.driver, 5).until(
                EC.visibility_of_element_located(self.error_message)
            ) is not None
        except:
            return False

class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.login_page = LoginPage(cls.driver)

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def data_provider(self):
        return [
            ('validUser1', 'validPass1', True),
            ('invalidUser', 'invalidPass', False),
            ('validUser2', 'wrongPass', False)
        ]

    def test_login_with_multiple_credentials(self):
        for username, password, is_valid in self.data_provider():
            with self.subTest(username=username, password=password):
                self.login_page.load()
                self.login_page.login(username, password)
                if is_valid:
                    WebDriverWait(self.driver, 10).until(
                        EC.url_to_be('https://example.com/dashboard')
                    )
                    current_url = self.driver.current_url
                    self.assertEqual(current_url, 'https://example.com/dashboard',
                                     f'Expected dashboard URL for valid user {username}')
                else:
                    self.assertTrue(self.login_page.is_error_displayed(),
                                    f'Error message should be displayed for invalid user {username}')

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

We define a LoginPage class as a Page Object to handle page actions and locators. This keeps code clean and reusable.

The data_provider method returns multiple sets of username, password, and expected validity.

The test method test_login_with_multiple_credentials loops over these data sets using subTest for clear reporting.

For valid credentials, it waits until the URL changes to the dashboard and asserts it matches.

For invalid credentials, it checks if the error message is visible.

Explicit waits ensure the test waits for elements or URL changes properly, avoiding flaky tests.

Setup and teardown methods open and close the browser once for all tests.

Common Mistakes - 4 Pitfalls
{'mistake': 'Hardcoding waits with time.sleep instead of using explicit waits', 'why_bad': 'Using fixed sleep times can make tests slow or flaky if the page loads faster or slower than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Not clearing input fields before sending keys
Mixing locators or using brittle XPath selectors
Not using data-driven approach and writing separate tests for each credential
Bonus Challenge

Now add data-driven testing with 3 different inputs using unittest's subTest and parameterize the test method.

Show Hint