Selenium WebDriver - should explicit waits be held in the test method or within the page object?



  • As the question title says: If you're putting a test together and you need to wait for an item to be displayed (as an example), is it best to put the explicit wait within the test or against the page?

    Any particular reason why one option may be chosen over the other?

    Thanks,



  • The choice of such design lies mainly on the product under test, tool, and the language you use.

    My personal preference is to:

    1. Add it in Page objects if it's being reused many times
    2. If it's just for 1 test case then, have it in the test suite itself.

    The reason for following the practice of not having assertions in page objects was to promote easy debugging. If something fails the engineer knows at which file the assertion was thrown. But nowadays there are tools and highly precise stack traces that point out to the exact line and file at which execution failed.

    So do whatever that makes your code clean and easy to read and maintain, there are no hard rules.

    But if you really want to, then return a true if wait pass else catch the timeout exception and return a false, and validate it in the test scripts. But I prefer to have it as it is in page object and letting it fail with a custom message.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2