Automation maintenance challenges in Testing Fundamentals - Build an Automation Script
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') 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 not None def test_login(): driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.enter_email('user@example.com') login_page.enter_password('Password123') login_page.click_login() WebDriverWait(driver, 10).until( EC.url_contains('/dashboard') ) dashboard_page = DashboardPage(driver) assert dashboard_page.is_welcome_message_displayed(), 'Welcome message not displayed on dashboard' driver.quit()
This test script uses Selenium WebDriver with Python to automate the login functionality.
We use the Page Object Model to keep locators and actions organized in LoginPage and DashboardPage classes. This makes maintenance easier if UI changes.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
Locators use stable IDs instead of brittle XPaths to reduce breakage when UI changes.
The test opens the login page, enters credentials, clicks login, waits for the dashboard URL, and asserts the welcome message is visible.
This structure helps handle automation maintenance challenges by isolating UI changes to page objects and using robust waits and locators.
Now add data-driven testing with 3 different sets of login credentials (valid and invalid).
Practice
Solution
Step 1: Understand the relationship between software and tests
Automated tests check software behavior, so if software changes, tests must reflect those changes.Step 2: Identify the effect of not updating tests
If tests are not updated, they may fail incorrectly, causing confusion and wasted effort.Final Answer:
Because outdated tests may fail even if the software works correctly -> Option CQuick Check:
Update tests when software changes = Avoid false failures [OK]
- Thinking tests never need updates
- Believing tests run independently of software
- Assuming updates slow development unnecessarily
Solution
Step 1: Recall the modern Selenium locator syntax
In recent Selenium versions, locators use By class with find_element(By.ID, 'value').Step 2: Identify the correct syntax among options
driver.find_element(By.ID, 'submitBtn'): driver.find_element(By.ID, 'submitBtn'), which is correct syntax in Python Selenium.Final Answer:
driver.find_element(By.ID, 'submitBtn') -> Option AQuick Check:
Use By.ID with find_element = Correct locator syntax [OK]
- Using deprecated find_element_by_id method
- Passing locator type as string instead of By class
- Confusing id with name locator
def test_sum():
a = 2
b = 3
assert a + b == 6What will be the test result when this runs?
Solution
Step 1: Evaluate the assertion logic
The assertion checks if 2 + 3 equals 6, but 2 + 3 equals 5, so the assertion is false.Step 2: Determine test outcome based on assertion
Since the assertion is false, pytest will mark the test as failed.Final Answer:
Test fails due to wrong assertion -> Option DQuick Check:
2 + 3 != 6 means assertion fails [OK]
- Assuming assertion is true without calculation
- Confusing syntax error with assertion failure
- Thinking test skips without skip decorator
element = driver.find_element(By.ID, 'login') element.click()
But the test fails with NoSuchElementException. What is the most likely cause?
Solution
Step 1: Understand NoSuchElementException meaning
This exception means Selenium could not find the element with the given locator on the page.Step 2: Analyze possible causes
If the element does not exist or is not loaded yet, this error occurs. The syntax and click() method are correct, and driver is assumed initialized.Final Answer:
The element with id 'login' does not exist on the page -> Option BQuick Check:
NoSuchElementException = Missing element on page [OK]
- Blaming click() method for element not found
- Assuming driver is uninitialized without evidence
- Ignoring page load or locator correctness
Solution
Step 1: Understand flaky tests caused by timing
Flaky tests often fail because the test tries to interact before the page or element is ready.Step 2: Evaluate maintenance-friendly solutions
Using explicit waits waits only as long as needed for conditions, improving reliability without slowing tests unnecessarily.Step 3: Compare other options
Fixed long waits slow tests and increase maintenance. Ignoring flaky tests risks missing bugs. Rewriting tests is costly and unrelated to timing issues.Final Answer:
Use explicit waits to wait for specific conditions before actions -> Option AQuick Check:
Explicit waits = Reliable, maintainable tests [OK]
- Using fixed sleeps causing slow, brittle tests
- Ignoring flaky tests instead of fixing
- Thinking rewriting tests solves timing issues
Ready to prove it?
Pass the test and this pattern moves unknown
