Should I try to write tests that work with the browser (for example, using selenium or something else)?

It is clear enough to write tests for things that happen on the server side, but I heard that it is really difficult to write unit test for the contents of the user interface and that they are fragile and unreliable. I would like to have more confidence that the changes I make do not break down the main parts of the import pages on my site, though.

Any thoughts or concerns?

+6
unit-testing selenium
source share
5 answers

I used them as part of the development. The nice thing about them is that you can execute them also in IE (for this you need a selenium server). I used them only at the development stage and did not consider them as unit tests. if I use certain user interface logic they really help.

The most important thing is to assign an id to all used html elements. without it, the tests are very fragile. using comments also helps.

+2
source share

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")); 
+4
source share

This is easy, can be done by a not-so-technical person, and is certainly worth the effort. Testing the user interface is difficult for desktop applications, not so difficult for web applications, because you can move controls, and selenium can still find them in HTML. There is no such luck for weak desktop developers.

Of course, there is always the opportunity to overdo it to such an extent that it does not give you much chance compared to efforts, but it is the same with conventional unit testing. You need to know when to stop.

+3
source share

The more tests you write, the better. When the time comes, you need to change something on the page created by the application - your tests will tell you something will break.

Yes, they are fragile, and yes, itโ€™s a pain that restrains the crazy choices that work correctly ...

+1
source share

You can use browsershots.org to check the appearance of your pages in almost every browser.

0
source share

All Articles