Why Selenium Users Might Like Selenide
Selenide is really nice and capable tool for writing functional/acceptance tests for your browser-based UI. Its declarative style makes your tests readable and easily exposes their intent. Good readability of the tests is good quality to have, because your test are supposed to describe and document your application’s behavior. Writing tests using Selenide is easy, as well. To quote authors of Selenide, “Just type: $(selector). - and IDE suggests you all the options.” If you're using Selenium for browser-based UI acceptance testing, I encourage you to check Selenide out and give it a try.
6 Selenide Benefits
As we mentioned above, Selenide offers a number of benefits for users who want to write functional/acceptance tests for brower-based UI. From internal and external quality metrics, to custom browser capabilities,it offers robust testing functionality for browser-based UI developers.
1. Selenide's Internal and External Quality Metrics
Software has two quality metrics, external and internal quality. External quality is how well the system meets the needs of its customers and users (is it functional, reliable, available, responsive, etc.), and internal quality is how well it meets the needs of its developers and administrators (is it easy to understand, easy to change, etc.).
For measuring external quality, we have to write acceptance tests that test our system end-to-end. An end-to-end test interacts with the system only from the outside: through its user interface, by sending messages as if from third-party systems, by invoking its web services, by parsing reports, and so on.
At ZeroTurnaround, we rely on Selenium for acceptance testing, so it was cool to find out a couple of months ago about Selenide (we suspect it’s pronounced as SEL-le-nide, rhymes with tide), which we now use for tests for UI activities in LiveRebel, our “bulletproof release tool”. Selenide is a concise and, as Codeborne engineers call it, expressive wrapper API specifically for Selenium WebDriver, which is used to control browser actions. So I thought I’d share a couple interesting/notable things about Selenide with you here.
2. Selectors in Selenide
An integral part of functional/acceptance UI test is selection of an element. You need to select an element to interact with it, check its state, wait for some condition to come true, etc. Selenide makes this as easy and fluent as possible.
Selenide offers following (jQuery-like) ways for selecting element/elements:
|$(String cssSelector)||Returns SelenideElement.|
|$$(String cssSelector)||Returns ElementsCollection.|
There are multiple useful static factory methods in the Selectors class for creating different By selectors.
|byText||Search by text (substring).|
|withText||Search by exact text string.|
|by||Search by attribute.|
|byTitle||Search by "title" attribute.|
|byValue||Search by "value" attribute.|
As elements in the DOM are hierarchical, sometimes you may want to select some element that resides in another element. In this case, Selenide allows you to chain selecters.
For example, you can select some row from a table in the following way:
SelenideElement roundHouseRow = $(“.table”).$(byText(“Norris”));
3. Conditions in Selenide
Nowadays, many websites use AJAX to change some parts of the website dynamically, which means we need to wait for some conditions to come true when we test our application. For that purpose, Selenide has a Condition class. Unlike Selenium, Selenide waits for some predefined timeout for the given Condition to come true. By default, the timeout to wait for is 4 seconds, this is of course configurable (via
selenide.timeout property). Conditions are also used for testing that webpage has expected elements and that elements have the desired state.
Some out-of-the-box Conditions you can use:
- matchText(String text) - for matching substring
- text(String text) - for matching whole text
- not(Condition condition) - for negating condition
As Condition class is abstract, users can easily define new Conditions that one might need.
Here is a little example that checks that a query button is present and visible on the page and clicking on it produces expected result.
SelenideElement queryButton = $(“#query-button”); queryButton.shouldBe(present, visible); queryButton.click(); SelenideElement result = $(“#query-result”).waitUntil(present); result.shouldHave(text(“Chuck”));
From this example, we can also see that Selenide has put an effort into making all the statements as readable as possible, even by non-technical staff. +10 for that!
HTML has multiple constructs that consist of multiple elements, like tables with multiple rows, multiple options for a selected element, radio button groups, different kinds of lists and more. We might have multiple elements that have the same name, or we want to perform operations on all elements that have the same class. So we’ll need a construct that allows us to perform operations on multiple elements, and Selenide has handy ways to work with collections of elements: ElementsCollection.
You can perform following actions on collections of elements:
|shouldHave||$$("#mytable tbody tr").shouldHave(size(2))|
From time to time, your tests are going to fail. That’s ok! For cases like this, we would like to have as much information as possible to quickly hunt down the reason of the failure. To help you figure out what happened, Selenide lets you take a snapshot of the browser window upon test failure, and screenshot of the webpage along with the HTML content is stored for your debugging pleasure. It’s also set up to use with JUnit and TestNG. Cool, right?
Using with JUnit runner
@Rule public final ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
Using with TestNG runner
Annotate your test class with
6. Selenide Custom Browser Capabilities
-Dbrowser parameter (Deprecated in Selenide 2.4), or passing the driver to
WebDriverRunner.setDriver (the currently-recommended way) before starting the test.
All seems pretty simple, right?
Looking for other ways to test your Java application? These resources are worth a look.
- Testing Microservices in Java
- How to Speed Up TDD
- Tips for Testing Java Code
- Spock Testing Groovy and Java Applications
If you want to save time on Java development, check out JRebel. Schedule a 20-minute personalized demo to see how much time you can save by skipping redeploys.