0
0
Testing Fundamentalstesting~15 mins

Test case writing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with valid credentials
Preconditions (2)
Step 1: Enter 'user@example.com' in the email input field
Step 2: Enter 'Password123' in the password input field
Step 3: Click the 'Login' button
✅ Expected Result: User is redirected to the dashboard page and sees a welcome message
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL is the dashboard URL
Verify the welcome message is displayed on the dashboard
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use meaningful locators like ID or name attributes
Organize code using Page Object Model for maintainability
Automated Solution
Testing Fundamentals
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, 'loginBtn')

    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, 'welcomeMsg')

    def is_welcome_message_displayed(self):
        return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.welcome_message)).is_displayed()

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

    def test_valid_login(self):
        login_page = LoginPage(self.driver)
        login_page.enter_email('user@example.com')
        login_page.enter_password('Password123')
        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)
        self.assertTrue(dashboard_page.is_welcome_message_displayed())

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

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

This test script uses Selenium with Python and unittest framework.

We define two page classes: LoginPage and DashboardPage to organize locators and actions, following the Page Object Model.

In test_valid_login, we enter the email and password, click login, then wait for the dashboard URL and check the welcome message is visible.

Explicit waits ensure elements are ready before interacting, avoiding flaky tests.

The setUp and tearDown methods open and close the browser cleanly for each test.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that rely on full element paths
Not verifying the expected page or element after login
Bonus Challenge

Now add data-driven testing with 3 different sets of valid credentials

Show Hint