0
0
Selenium Pythontesting~15 mins

Why evidence collection supports debugging in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Collect evidence during test failure for debugging
Preconditions (3)
Step 1: Open the web application login page
Step 2: Enter 'wronguser' in the username field
Step 3: Enter 'wrongpass' in the password field
Step 4: Click the login button
Step 5: Verify that the error message 'Invalid credentials' is displayed
Step 6: If the error message is not displayed, take a screenshot and save the page source
✅ Expected Result: The error message 'Invalid credentials' is displayed. If not, evidence (screenshot and page source) is collected for debugging.
Automation Requirements - Selenium with Python unittest
Assertions Needed:
Verify error message text is exactly 'Invalid credentials'
Verify screenshot and page source are saved when assertion fails
Best Practices:
Use explicit waits to wait for elements
Use try-except blocks to capture failures and collect evidence
Save evidence files with timestamped filenames
Use Page Object Model for element locators
Automated Solution
Selenium Python
import unittest
import os
from datetime import datetime
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.username_input = (By.ID, 'username')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'login-btn')
        self.error_message = (By.ID, 'error-msg')

    def enter_username(self, username):
        self.driver.find_element(*self.username_input).clear()
        self.driver.find_element(*self.username_input).send_keys(username)

    def enter_password(self, password):
        self.driver.find_element(*self.password_input).clear()
        self.driver.find_element(*self.password_input).send_keys(password)

    def click_login(self):
        self.driver.find_element(*self.login_button).click()

    def get_error_message_text(self):
        return self.driver.find_element(*self.error_message).text

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)
        self.login_page = LoginPage(self.driver)
        if not os.path.exists('evidence'):
            os.makedirs('evidence')

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

    def save_evidence(self, name_prefix):
        timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
        screenshot_path = f'evidence/{name_prefix}_screenshot_{timestamp}.png'
        page_source_path = f'evidence/{name_prefix}_page_source_{timestamp}.html'
        self.driver.save_screenshot(screenshot_path)
        with open(page_source_path, 'w', encoding='utf-8') as f:
            f.write(self.driver.page_source)

    def test_invalid_login_shows_error(self):
        self.login_page.enter_username('wronguser')
        self.login_page.enter_password('wrongpass')
        self.login_page.click_login()

        try:
            error_element = self.wait.until(EC.visibility_of_element_located(self.login_page.error_message))
            error_text = error_element.text
            self.assertEqual(error_text, 'Invalid credentials')
        except Exception as e:
            self.save_evidence('invalid_login_failure')
            raise e

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

This test script automates the manual test case to verify the error message on invalid login.

LoginPage class: Encapsulates element locators and actions for the login page, following the Page Object Model for maintainability.

TestLogin class: Sets up the browser, navigates to the login page, and runs the test.

The test enters wrong credentials, clicks login, and waits explicitly for the error message to appear.

If the error message is not found or text does not match, the test catches the exception, saves a screenshot and page source with a timestamped filename in an 'evidence' folder, then re-raises the exception to fail the test.

This evidence collection helps debugging by preserving the exact page state when the failure happened.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Not catching exceptions to save evidence on failure
Hardcoding file names for screenshots
Locating elements directly in test methods instead of Page Object
Bonus Challenge

Now add data-driven testing with 3 different invalid username/password combinations

Show Hint