Automation Best Practices and Anti-Patterns

Selenium 12 min min read Updated: Nov 16, 2026 Intermediate
Automation Best Practices and Anti-Patterns
Intermediate Topic 5 of 5

What Should You Automate?

Good candidatesPoor candidates
Repeated regression checksOne-time checks
Critical paths: login, search, checkoutRapidly changing screens
Data-heavy checksVisual design opinions (look and feel)
Cross-browser checksThings that need human judgment
Stable featuresExploratory testing

The Test Pyramid

text /\ few UI tests (Selenium): slow, expensive, realistic / \ /----\ some API / integration tests: faster, stable / \ /--------\ many Unit tests: very fast, cheap

Do not test everything through the browser. Test business rules in unit or API tests, and use Selenium for a few important user journeys.

Do

  • Keep tests small with one purpose and a clear name
  • Use API calls or the database for setup (create a user, add data) instead of clicking through the UI
  • Use stable locators and ask for data-testid
  • Use explicit waits for conditions
  • Assert on business results (order confirmed), not on trivial details
  • Clean up or isolate data
  • Review test code like product code
  • Run smoke tests on every commit and the full suite nightly
  • Delete or fix flaky tests quickly

Avoid (Anti-Patterns)

Anti-patternBetter
time.sleep(5) everywhereWebDriverWait with a condition
Absolute XPathID, data-testid, short CSS
Long end-to-end tests with 60 stepsSeveral short tests
Tests that depend on previous testsIndependent tests with their own data
Assertions in many places in page objectsAssert in the test
Copy-pasted codeHelpers and page objects
Catching exceptions and ignoring themLet the test fail with a clear message
Testing third-party sites (Google, ads)Test your application
Logging in through the UI in every testLogin by API or reuse the session
Ignoring failing testsFix, quarantine with a ticket, or delete

Login Once Trick

python @pytest.fixture def logged_in(driver, config): # sign in through the API, then place the session cookie in the browser import requests s = requests.Session() s.post(f"{config['base_url']}/api/login", json={"user": "ana", "password": "x"}) driver.get(config["base_url"]) # domain first for c in s.cookies: driver.add_cookie({"name": c.name, "value": c.value, "path": "/"}) driver.refresh() return driver

Good Assertion Messages

python assert total == expected, f"Cart total wrong: expected {expected}, got {total}"

Maintenance Habits

  • Weekly: look at the slowest and flakiest tests
  • Track pass rate trend
  • Update dependencies (selenium, pytest) regularly
  • Document how to run and debug
  • Share failures with developers: it may be a real bug
Success measure: A good suite is trusted: when it is red, the team investigates. Reliability matters more than the number of tests.

Conclusion

Automate the right things, keep UI tests few and focused, use APIs for setup, use stable locators and waits and treat test code as real code that needs review and care.

Quick Summary: Few meaningful UI tests, stable locators, explicit waits, independent data, fix flakiness fast.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators