31 / 80 · 12 Programming for QA · OOP for Testing← prev⊞ allnext →☰ Read as one page
4.5Encapsulation: Hiding Implementation Details
Page objects should hide implementation details from tests. Tests should never contain locators, waits, or driver commands.
# BAD: locators and waits in the test
def test_add_to_cart(driver):
driver.find_element(By.CSS_SELECTOR, ".product-card button.add").click()
WebDriverWait(driver, 10).until(
EC.text_to_be_present_in_element(
(By.CSS_SELECTOR, ".cart-count"), "1"
)
)
# GOOD: all details inside the page object
def test_add_to_cart(driver):
product_page = ProductPage(driver)
product_page.add_first_item_to_cart()
assert product_page.get_cart_count() == 1