0

Does anyone write unit tests for page objects and related UI test infrastructure? If so, what suggestions do you have for designing those unit tests? Do you mock WebDriver instances, or take another approach?

Background: I realize this is a mind-bending question for some people. “Test the tests?” No, I’m not talking about testing tests. I’m talking about testing domain objects and test framework code that tests use. Consider this typical technology stack for a well architected UI test:

  • Test cases
  • Domain objects - page objects in the case of Selenium, or a DSL, or generic classes in the case of non-UI tests
  • Test framework - xUnit, PyTest, TestNG, etc.
  • Language libraries, including assertion handling
  • OS code, way down at the bottom

People write unit tests for all these layers except the tests themselves. Good developers write unit tests for the domain objects they create. And one type of domain object - in the case of Selenium UI test suites - is page objects. I know some people unit test these classes, but I haven’t seen any good articles about it.

Do you have experience testing at this layer for UI tests that use the Selenium toolset? If so, I’d love to hear your experience about whether you use general mocking tools (PyTest MagicMock or Java Mockito, for example) or use built-in Selenium tricks.

3

For the test infrastructure code (helper and utility classes, custom logic, etc) - unit testing might make sense in case if you are providing your framework to a 3rd party usage (that can be a community in case of opensource or even the other team within your organization). Approach it as a regular code and follow the common unit testing approaches.

For the page objects - the page objects are usually mirroring the actual SUT logic which can be changed due to the different reasons, I believe that it does not make sense to write unit tests since the SUT behavior is outside of your control and in overall that seems to be an overkill.

  • I think that is the most insightful thing I’ve read here on this topic. Thank you! Do you know of any books or articles that expand on this? – Todd Bradley Aug 26 at 3:04
  • @ToddBradley - The Art of Unit Testing by Roy Osherove as a general guide. - Test-Driven Development by Example by Kent Beck if you want to switch into TDD approach and master it while developing your framework. - Working Effectively with Legacy Code by Michael C. Feathers, when things are already done and you need to deal with it. – 501NotImplemented Aug 26 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.