Let’s face it, they are slow, brittle and expensive to maintain. But then again there is no real replacement for making browser-based end-to-end tests. And as long as you keep the system test suite small, isolated and concise there shouldn’t be much to maintain.
On code testing code
Let’s keep it in mind that for any good system should include the following type of testing
- many unit tests to test that the code honors its contact, how else can you know?
- some integration tests for important flows through integrated components functionally
- few system tests for important end-to-end flows, this tests that the system stands up
- little manual testing using the human-eye for things that are hard to detect like usability
For this post i’m going to focus on the few system tests and how to make them as stable, readable and simple as possible. If you have heard of page object pattern, data-tags and selenium grids then this post is redundant to you. But if it is news for you then please read on.
They are the three most important patterns to follow if you’re going to make selenium tests. Here’s what they do and why they help.
Page Object Pattern and data tags
Single purpose classes that are responsible to act as an API for a test that is responsible for resolving
ids or classes. This makes for readable tests. By using a tag called data-test that holds an unique identifiera that never changes we can lock the test onto that tag no matter which type of element or classes it has. This is preferrable to relying on either
classes which are subject to change if the page changes. This will alleviate tests breaking because someone on the frontend refactors CSS classes, and leave the breakages that means something is missing, invisible or immovable.
This kind of page object contains the page-specific element names and maps them the variabler that makes sense for the content. The suoerclass takets care of initializing @FindBy-annotated variabler lazily, meaning it is evaluated upin accessing the element. Something like
A simple grid
A selenium grid is a server that runs selenium tests as a service. There are naturally many cloud providers for this use case but there are some FOSS disruptors in that space asswell. I have especially taken heart to selenoid, that spawns docker containers containing insividual isolated browsers in a headless way that is VERY suitable for running Quick system tests upon a new deploy.
A Java Project
Include the following dependencies in your pom.xml, build.xml or build.gradle to run selenium-java-remote using JUnit 4. We’re going to stay away from large frameworks and simply run Selenium Tests as a regular unit-test. Use whatever loggning library you like and replace my
println, logging is not the focus of this article. 😉
Here’s a tiny SeleniumTestBase.java that initializes the Page Objects and creates the remote webdriver.
Any JUnit test must extend this class and will initialize a remote webdriver per each test in this case. This can also be used to run as many test-methods in parallel as there are concurrent containers in the grid. This is when Selenoid shines the brightest, because it will create a lightweight container for each test method making for awesome idempotent and isolated tests. The TestBase will parse if a test has failed and put a recording of it into the target folder if you’re running maven.
This results in small, stable and readable tests such as this one.
I hope that helps you running better, faster and more stable Selenium-based test at all levels!