Selenide: A Powerful Testing Framework
Tags: testing, java, selenide
Published: 2020-06-30 07:57:12

Many testers think about starting their adventure into test automated by looking at [Selenium](https://www.selenium.dev/). 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](https://selenide.org/) 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](https://www.selenium.dev/documentation/en/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:**

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

**Selenide using Firefox driver:**

```java
open(https://sonalake.com); this opens a Firefox browser with the provided url.
```

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

```java
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
```

## **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.

<div class="table-2">

<table style="height: 140px;" width="769">

<thead>

<tr>

<th align="left">Selenium notation:</th>

<th align="left">Selenide notation:</th>

</tr>

</thead>

<tbody>

<tr>

<td align="left">driver.findElement(By.tagName(“li”));</td>

<td align="left">**$**(“li”);</td>

</tr>

<tr>

<td align="left">driver.findElement**s**(By.tagName(“li”));</td>

<td align="left">**$**(“li”);</td>

</tr>

<tr>

<td align="left">driver.findElement(By.id(“header”).findElement(By.id(“menu”))  
.findElements(By.className(“item”));</td>

<td align="left">$(“#header”).find(“#menu).findAll(“.item”);</td>

</tr>

</tbody>

</table>

</div>

## **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:**

```java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(  ExpectedConditions.visibilityOfElementLocated(By.className("className")));
```


**Selenide:**

```java
$(“.className”).waitUntil(visible, 10000);
```

## **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:**

```java
WebElement header = driver.findElement(By.tagName("h1"));
assertEquals(“Header Text", header.getText());
```

**Selenide:**
```java
$("h1").shouldHave(text("Header Text"));
```

## **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](https://selenide.org/quick-start.html). Alternatively, you can download the [sample framework project](https://bitbucket.org/kubathorgal/example-framework/src/master/) I prepared.

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

![](https://sonalake.com/wp-content/uploads/2020/06/sample-form.png)

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”

```java
$("input").stream().filter(el -> el.getAttribute("name").contains("your-name"))
           .findFirst().get().setValue("John");
```

2.  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”

```java
$("input").stream().filter(el -> el.getAttribute("name").contains("your-email"))
           .findFirst().get().setValue("john@sonalake.com");
```

3.  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”

```java
$("input").stream().filter(el -> el.getAttribute("name").contains("your-topic"))
           .findFirst().get().setValue("I want to work here");
```

4.  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.

```java
$(".wpcf7-file").uploadFile(new File("src/CV_.pdf"));
```

5.  Find text field for your message and type “some text”

```java
$("textarea").setValue("some text");
```

6.  Find all mandatory checkboxes and click each one of them.

```java
$("input.pi-acceptance").stream().forEach(el -> { el.click();});
```

7.  Click Submit button

```java
$(".askjobbutton").click();
```

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](https://selenide.org).

### More Reading

*   Selenide + Cucumber:  
    [https://github.com/selenide-examples/cucumber/tree/master/src/test/resources/org/selenide/examples/cucumber](https://github.com/selenide-examples/cucumber/tree/master/src/test/resources/org/selenide/examples/cucumber)
*   Selenide + JBehave:  
    [https://gist.github.com/mp911de/009ea539271da0633c59](https://gist.github.com/mp911de/009ea539271da0633c59)
*   Selenide + GEB:  
    [https://github.com/selenide-examples/geb-maven-tutorial/blob/master/src/test/groovy/stepdefs/IndexStepDefs.groovy](https://github.com/selenide-examples/geb-maven-tutorial/blob/master/src/test/groovy/stepdefs/IndexStepDefs.groovy)
*   Selenide + Gauge:  
    [https://github.com/selenide-examples/GaugeSelenideExample/blob/master/specs/example.spec](https://github.com/selenide-examples/GaugeSelenideExample/blob/master/specs/example.spec)