0
0
Selenium Pythontesting~15 mins

Parameterize with test data in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Login functionality with multiple user credentials
Preconditions (5)
Step 1: Open the login page URL.
Step 2: Enter the email address from the test data into the email input field with id 'email'.
Step 3: Enter the password from the test data into the password input field with id 'password'.
Step 4: Click the login button with id 'loginBtn'.
Step 5: Wait for the page to load and verify the URL is 'https://example.com/dashboard'.
Step 6: Log out to return to the login page before the next test data is used.
✅ Expected Result: For each set of credentials, the user successfully logs in and is redirected to the dashboard page.
Automation Requirements - Selenium with Python unittest
Assertions Needed:
Verify the current URL after login matches 'https://example.com/dashboard'.
Best Practices:
Use parameterization to run the same test with multiple sets of data.
Use explicit waits to wait for page elements or URL changes.
Use Page Object Model to separate page interactions from test logic.
Clean up after each test by logging out to reset state.
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.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')
        self.logout_button = (By.ID, 'logoutBtn')

    def open(self):
        self.driver.get('https://example.com/login')

    def login(self, email, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).clear()
        self.driver.find_element(*self.email_input).send_keys(email)
        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 logout(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.logout_button)).click()

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

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

    def setUp(self):
        self.login_page.open()

    def test_login_with_multiple_credentials(self):
        test_data = [
            ('user1@example.com', 'Password1!'),
            ('user2@example.com', 'Password2!'),
            ('user3@example.com', 'Password3!')
        ]
        for email, password in test_data:
            with self.subTest(email=email):
                self.login_page.login(email, password)
                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'Login failed for {email}')
                self.login_page.logout()

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

This script uses the Selenium WebDriver with Python's unittest framework to automate login tests with multiple user credentials.

LoginPage class: Encapsulates the login page elements and actions. This follows the Page Object Model to keep code organized.

TestLogin class: Contains the test case. It opens the login page before each test and closes the browser after all tests.

Parameterization: The test_login_with_multiple_credentials method loops over a list of email/password pairs. Each pair is tested separately using subTest for clear reporting.

Assertions: After login, the test waits explicitly for the URL to change to the dashboard URL and asserts it matches exactly.

Cleanup: After each login, the test logs out to reset the state for the next iteration.

This approach ensures the test is maintainable, clear, and reliable.

Common Mistakes - 4 Pitfalls
Hardcoding test data inside the test method without parameterization
Using implicit waits or time.sleep instead of explicit waits
Not cleaning up after each test iteration (e.g., not logging out)
Using brittle locators like absolute XPaths
Bonus Challenge

Now add data-driven testing with 3 different inputs using unittest's parameterized library or pytest parametrize.

Show Hint