0
0
Testing Fundamentalstesting~15 mins

Defect lifecycle in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify Defect Lifecycle Status Transitions
Preconditions (2)
Step 1: Open the defect details page
Step 2: Change defect status from 'New' to 'Assigned'
Step 3: Save the defect
Step 4: Verify the status is updated to 'Assigned'
Step 5: Change defect status from 'Assigned' to 'Fixed'
Step 6: Save the defect
Step 7: Verify the status is updated to 'Fixed'
Step 8: Change defect status from 'Fixed' to 'Retest'
Step 9: Save the defect
Step 10: Verify the status is updated to 'Retest'
Step 11: Change defect status from 'Retest' to 'Closed'
Step 12: Save the defect
Step 13: Verify the status is updated to 'Closed'
✅ Expected Result: The defect status changes correctly through the lifecycle states: New -> Assigned -> Fixed -> Retest -> Closed, and each status update is saved and displayed properly.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify defect status text matches expected status after each update
Verify defect status element is visible and enabled before interaction
Best Practices:
Use explicit waits to wait for elements to be clickable or visible
Use Page Object Model to separate page interactions
Use clear and maintainable locators (id, name, or CSS selectors)
Include meaningful assertion messages
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 DefectLifecyclePage:
    def __init__(self, driver):
        self.driver = driver
        self.status_dropdown = (By.ID, 'defect-status')
        self.save_button = (By.ID, 'save-defect')
        self.status_text = (By.ID, 'current-status')

    def select_status(self, status):
        dropdown = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.status_dropdown))
        dropdown.click()
        option = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, f"//select[@id='defect-status']/option[text()='{status}']")))
        option.click()

    def save_defect(self):
        save_btn = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.save_button))
        save_btn.click()

    def get_status(self):
        status_element = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.status_text))
        return status_element.text

class TestDefectLifecycle(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://defect-tracker.example.com/defect/123')
        # Assume user is already logged in for simplicity
        self.page = DefectLifecyclePage(self.driver)

    def test_defect_status_transitions(self):
        statuses = ['Assigned', 'Fixed', 'Retest', 'Closed']
        for status in statuses:
            self.page.select_status(status)
            self.page.save_defect()
            current_status = self.page.get_status()
            self.assertEqual(current_status, status, f"Expected status to be '{status}' but got '{current_status}'")

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

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

This script uses Selenium with Python and unittest framework to automate the defect lifecycle status changes.

The DefectLifecyclePage class represents the defect details page. It uses explicit waits to ensure elements are clickable or visible before interacting. This avoids timing issues.

The test_defect_status_transitions method changes the defect status through the lifecycle states one by one. After each change, it saves and verifies the status text matches the expected value.

Assertions include clear messages to help understand failures.

The setUp and tearDown methods handle browser start and cleanup.

This structure follows the Page Object Model for maintainability and uses best practices like explicit waits and meaningful assertions.

Common Mistakes - 3 Pitfalls
Using hardcoded sleep (time.sleep) instead of explicit waits
Using brittle XPath selectors with absolute paths
Not verifying the status after each update
Bonus Challenge

Now add data-driven testing with 3 different defect IDs to verify lifecycle transitions for multiple defects.

Show Hint