Automation Best Practices and Anti-Patterns
What Should You Automate?
| Good candidates | Poor candidates |
|---|---|
| Repeated regression checks | One-time checks |
| Critical paths: login, search, checkout | Rapidly changing screens |
| Data-heavy checks | Visual design opinions (look and feel) |
| Cross-browser checks | Things that need human judgment |
| Stable features | Exploratory 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-pattern | Better |
|---|---|
time.sleep(5) everywhere | WebDriverWait with a condition |
| Absolute XPath | ID, data-testid, short CSS |
| Long end-to-end tests with 60 steps | Several short tests |
| Tests that depend on previous tests | Independent tests with their own data |
| Assertions in many places in page objects | Assert in the test |
| Copy-pasted code | Helpers and page objects |
| Catching exceptions and ignoring them | Let the test fail with a clear message |
| Testing third-party sites (Google, ads) | Test your application |
| Logging in through the UI in every test | Login by API or reuse the session |
| Ignoring failing tests | Fix, 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.

