Advice or articles about unit testing page objects and related UI test infrastructure code



  • 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.



  • 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.



Suggested Topics