Write Selenium tests that don't suck


Planning

Design Patterns

Development

Code

  • Use a framework that allows you to write once and run everywhere.
  • Test should emulate the action performed by a user.
  • Use Gherkin syntax for readability.
  • Modularize your scenarios so you have many small tests instead of one large test.
  • A test should not be dependent on another. They should be able to run in any order.
  • Colocate test code with feature code so the correct build artifact is used.

Locators

  • Separate locators in their own files.
  • Locators should not be brittle or else tests will be flaky.
  • Use XPath to find elements with text.

Debugging

  • Log each action to the screen and to a file.
    • Include information like user credentials to make troubleshooting easier.
  • Add a flag to control whether the test halts on error.
  • Allow the test to be paused when a breakpoint is hit.
  • Save a screenshot when a test fails.

Tips

  • If a test is flaky, disable it. Then harden the test before reenabling it.
  • Avoid using sleep() to resolve race conditions.

Runtime

  • Minimize setup and teardown time. Aim for fast test execution.
  • Tighten feedback loops so developers can run and troubleshoot tests efficiently.
  • Parallelize test execution.
  • Retry failed tests.


Please support this site and join our Discord!