Selenium + Java · Lesson 44 of 44

0/44 done

Module 44: Selenium Best Practices

44. Enterprise Selenium Best Practices & Clean Automation Code

18 minlesson

Your task

Implement calculateTestHealthScore:

  1. Accept int totalTests, int passedTests, int flakyRetries.
  2. If totalTests == 0, return 0.0.
  3. Calculate score = (((double) passedTests - (flakyRetries * 0.5)) / totalTests) * 100.0.
  4. Return Math.max(0.0, score).

Write it in the editor, then press Check answer (Ctrl + Enter).

The 7 Golden Rules of Selenium Automation

  1. Never use Thread.sleep: Always use explicit condition-based waits.
  2. Never share WebDriver instances across parallel tests: Use ThreadLocal.
  3. Encapsulate locators in Page Objects: Tests must never contain raw By locators.
  4. Assert in Tests, not Page Objects: Page objects provide actions and state; test methods assert expectations.
  5. Always clean up: Ensure driver.quit() runs in teardown hooks.
  6. Prefer By.id and unique data-testid attributes: Avoid brittle multi-level absolute paths.
  7. Ensure test independence: Every test must be able to run in isolation in any order.

Key Takeaways & Walkthrough Summary

In this final module of Selenium with Java, we review the golden rules of enterprise test automation. You will learn how to maintain high test stability, write DRY and maintainable Java code, ensure complete test independence, and eliminate flakiness. Congratulations on completing the course!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What is the biggest mistake beginner automation engineers make?

Sprinkling Thread.sleep() and hardcoded delays throughout test code instead of understanding and applying proper condition synchronization.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Finish course