44. Enterprise Selenium Best Practices & Clean Automation Code

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.

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).
πŸ’‘Common doubts
Did you like the problem?
Lesson 44 of 44
Java
Loading code editor…