UI tests can be slow, fragile, and painful to maintain, but some errors can only be detected in user interface tests. The important question is not whether to write user interface tests, but how to keep your user interface tests useful, stable, and maintainable.
A common mistake is to use UI tests as a replacement for other tests. Theoretically, you can test many functions using user interface tests, but there are many problems with this approach. For starters, some functions can be very difficult to test directly in the user interface (especially in exceptional circumstances). Secondly, if the test fails, it is often difficult to understand what is the source of the problem. Finally, the more code tests you test in user interface tests, the slower the user interface tests run. If you rely only on slow tests, your performance is degraded, which increases the temptation to simply โtemporarilyโ disable broken tests.
My advice is to check as much as possible in unit tests and integration tests, create a good separation between your user interface and your business logic, and use your user interface tests to catch / prevent errors that cannot be tested in other types of tests.
If you have a lot of tests, consider creating multiple sets. I create one set for user interface tests, one for integration tests and one for unit tests. Unit tests are very fast, so I run them when I develop my code (often through TDD). These are the tests that help me to be productive.
I perform integration tests less often (perhaps after I made a few changes). I run UI tests when I'm ready to submit changes (or when I write more UI tests, obviously).
One final tip: Consider writing your user interface tests with a domain-specific language . This makes it easier to understand the tests (since they are read as a set of user steps, and not as a collection of low-level browser actions). It can also simplify the work with code. For example, instead of having each test go through the step-by-step actions of the browser for user login, you can see:
LoginPage loginPage = new LoginPage(selenium); HomePage homePage = loginPage .enterUserName(TestUsers.Alice.USER_NAME) .enterPassword(TestUsers.Alice.PASSWORD) .submit(); assertThat(homePage.getBreadcrumbs(), equalTo("Home"));