DEV Community

Cover image for Selenide: A Powerful Testing Framework
Jakub Skibiński for Sonalake

Posted on • Originally published at sonalake.com

Selenide: A Powerful Testing Framework

Many testers think about starting their adventure into test automated by looking at Selenium. Most of us heard about Selenium WebDriver, but when we start to dig deeper, it we soon discover that it’s not so easy to create your own framework. Most tutorials about automation tests assume significant prior knowledge or experience. At this point many people give up because they don’t find anything “easy”. What’s needed is an easier path into the world of automation testing. Selenide provides this.

Some time ago a friend of mine showed me a Selenium-based framework called Selenide. I had some experience in Selenium so I wanted to know if there are any other similar, easier, faster frameworks which I can use in my daily work. I discovered that without any experience, everyone can prepare their own skeleton for writing automated test scripts using Selenide and it will be faster than preparing the same skeleton with Selenium. Why Selenide? Well, it can be a good start for beginners and a new tool for advanced automation testers.

So, what is Selenide and why it can be used both for beginners and advanced automation testers?

Selenide is a testing framework powered by Selenium WebDriver. As the creators page says: it comes with powerful selectors, easy configuration, concise fluent API for tests, Ajax support for stable tests.

Drivers for most common browsers are bundled with Selenide.

You’ll need to configure your browser driver. The examples below show how to do it for Firefox.

Selenium using Firefox driver:

System.setProperty("webdriver.gecko.driver",
   "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver webDriver = new FirefoxDriver();
webDriver.get("https://sonalake.com");
Enter fullscreen mode Exit fullscreen mode

Selenide using Firefox driver:

open(https://sonalake.com); this opens a Firefox browser with the provided url.
Enter fullscreen mode Exit fullscreen mode

If you want to use another driver like chrome driver, use this code before calling open() method:

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
Enter fullscreen mode Exit fullscreen mode

Why is Selenide good for beginners?

When working with Selenium directly you need to write more code to select the desired element. To an element you need to type the following: driver.findElement(By……..) in Selenide the driver.findElement is simplified to $. Of course you can use the Page Factory notation in Selenium but it’s still more code than in Selenide.

Isn’t it great?

But what if you want to select a collection of elements? In Selenium you need to use driver.findElements… in Selenide this would be $$. This helps you to write less code, and more importantly, more readable code.

What else does Selenide give you?

If you are looking for elements using console you use almost the same notation. Why? Because in jQuery we also use $ to find an element and $$ to find the collection of elements.

Selenium notation: Selenide notation:
driver.findElement(By.tagName(“li”)); **$**(“li”);
driver.findElement**s**(By.tagName(“li”)); **$**(“li”);
driver.findElement(By.id(“header”).findElement(By.id(“menu”)) .findElements(By.className(“item”)); $(“#header”).find(“#menu).findAll(“.item”);

Waiting for elements

Writing a wait mechanism in Selenium is quite complicated. For example, let’s assume that we click a Login button on a web page.

  • We don’t really know how much time we need to wait until we’ll be logged in or not.
  • We could assume that login action takes 5, 10 or more seconds. For this purpose we might use an implicit wait. But this is not a good approach, because what if the server returns a response after 1 second? Then our test will wait these additional 4 or 9 seconds. Even worse, what if it took longer than 10 seconds – our test would fail.

In Selenium we use an Explicit wait to handle this problem. Let’s compare Selenium and Selenide doing exactly the same thing.

Selenium:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(  ExpectedConditions.visibilityOfElementLocated(By.className("className")));
Enter fullscreen mode Exit fullscreen mode

Selenide:

$(.className).waitUntil(visible, 10000);
Enter fullscreen mode Exit fullscreen mode

Assertions

JUnit provides useful assertions but Selenide has assertions dedicated to use them to verify elements in a web browser. You can easily guess what a certain assertion does. Let’s look at the example:

Selenium:

WebElement header = driver.findElement(By.tagName("h1"));
assertEquals(Header Text", header.getText());
Enter fullscreen mode Exit fullscreen mode

Selenide:

$("h1").shouldHave(text("Header Text"));
Enter fullscreen mode Exit fullscreen mode

Is it possible to create a framework in less than 10 minutes ?

If you want to write the project from scratch, the Selenide creators have prepared a quick start guide. Alternatively, you can download the sample framework project I prepared.

In my example I chose a contact form as an example.

It contains a void method called verifyForm(). Before this method I added annotation "@ Test" which refers to JUnit library. Each line of this method is a step of our test. It looks a little bit messy, but I’m just showing you the concept of this framework. Patterns like Page Object pattern can be easily used with this framework. I will describe each step of our test and appropriate methods that represent it in the code.

  1. Find all inputs using $$ sign, get name attribute of each and find the one for which name attribute contains “your-name”, find first one and type “John”
$("input").stream().filter(el -> el.getAttribute("name").contains("your-name"))
           .findFirst().get().setValue("John");
Enter fullscreen mode Exit fullscreen mode
  1. Find all inputs using $$ sign, get name attribute of each and find the one for which name attribute contains “your-email”, find first one and type “john@sonalake.com
$("input").stream().filter(el -> el.getAttribute("name").contains("your-email"))
           .findFirst().get().setValue("john@sonalake.com");
Enter fullscreen mode Exit fullscreen mode
  1. Find all inputs using $$ sign, get name attribute of each and find the one for which name attribute contains “your-topic”, find first one and type “I want to work here”
$("input").stream().filter(el -> el.getAttribute("name").contains("your-topic"))
           .findFirst().get().setValue("I want to work here");
Enter fullscreen mode Exit fullscreen mode
  1. Find element “.wpc7-file” and upload file which first should be added to the project by yourself. In my case it’s CV_.pdf added to the src folder. Using uploadFile method this file will be added to the contact form.
$(".wpcf7-file").uploadFile(new File("src/CV_.pdf"));
Enter fullscreen mode Exit fullscreen mode
  1. Find text field for your message and type “some text”
$("textarea").setValue("some text");
Enter fullscreen mode Exit fullscreen mode
  1. Find all mandatory checkboxes and click each one of them.
$("input.pi-acceptance").stream().forEach(el -> { el.click();});
Enter fullscreen mode Exit fullscreen mode
  1. Click Submit button
$(".askjobbutton").click();
Enter fullscreen mode Exit fullscreen mode

Now run the code and verify that everything has passed. Experiment with this code – that’s the best way to learn a new framework!

Summary

For anyone who is interested to start their journey with test automation I would highly recommend starting it with Selenide. Selenide provides readable, simple locators, built in waits, readable methods and actions. For more examples and documentation, please visit the Selenide website.

More Reading

Top comments (0)