Selenide – concise UI Test in Java

Reading Time: 3 minutes

Selenide is a framework for test automation powered by Selenium Webdriver. 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.  It comes with powerful selectors, easy configuration, concise fluent API for tests, Ajax support for stable tests.

In most applications with ajax supports I had to wait for some time to get the expected state of an element, so sometimes in the scripting if we add an implicit wait, the test will usually fail. So to overcome that selenide has some inbuilt methods such as should(), shouldBe, shouldHave() for the positive outcomes of a such ajax state change and shouldn’t(), shouldNotBe(), shouldNothave() for the determination of the negative ajax state outcomes.

Why Selenide?

To eliminate the persisting error points in the Selenium web driver, Selenide was introduced. It was introduced as a wrapper class for the Selenium WebDriver. With Selenide we can build testing scripts within the inbuilt assertions. For web applications, we typically write scripts that open the application in the web browser (either real or headless) and start clicking buttons. We call them user interface (UI) tests.

By using Selenide we can build an automation project which will have less code and high value with vigorous test capabilities. And with a powerful IDE Selenide actually doesn’t need documentation. 

The most common tool for UI tests in Java today is Selenium WebDriver. But it has lots of disadvantages. Selenide has been designed to be very easy usable from any IDE without the need for reading documentation. You just write $(“selector”). – and the IDE suggests all possible variants once you pressed the dot.

The main limitations of Selenium that we covered with Selenide are as follows:
  • Smart Waiting
  • Ajax support
  • Automated screenshots
  • Transparent browser

There is bit difference between writing the selenide element and the selenium element. 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 $. You can use the Page Factory notation in Selenium but it’s still more code than in Selenide.

Selenium Version :

driver.findElement(By.tagName("li"));
driver.findElements(By.cssSelector("li"));
driver.findElements(By.id("menu"));

Selenide Version :

$("li"); //$ for element
$$("li"); //$$ for more than one element
$$("#menu");

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

Selenium using Chrome Driver :

System.setProperty("webdriver.chrome.driver",
   "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://knoldus.com");

Selenide using Chrome Driver :

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
System.setProperty("selenide.browser", "Chrome");
open(https://knoldus.com); this opens a Chrome browser with the provided url.

For Firefox Browser. its just one line of code.

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

It’s that Simple .

How to use Selenide for your project?

For Maven:

<dependency>
    <groupId>com.knoldus</groupId>
    <artifactId>selenide</artifactId>
    <version>6.0.3</version>
    <scope>test</scope>
</dependency>

For Gradle :

dependencies {
  testImplementation 'com.knoldus:selenide:6.0.3'
}

Waiting for elements in Selenide

The wait commands are essential when it comes to executing Selenium tests. They help to observe and troubleshoot issues that may occur due to variation in time lag.

While running Selenium tests, it is common for testers to get the message “Element Not Visible Exception“. This appears when a particular web element with which WebDriver has to interact, takes time while loading. To prevent this Exception, Selenium Wait Commands must be used.

Some time we use explicit wait to wait for few seconds.

In Selenium :

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

In Selenide:

$(“#id”).waitUntil(visible, 10000);

Assertions in Selenide

JUnit and Testng 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:

In Selenium :

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

In Selenide:

$("h3").shouldHave(text("Header Text"));

Selenide provides readable, simple locators, built-in waits, readable methods and actions. For more examples and documentation, please visit the Selenide website. That’s all for today. Happy Learning!

Scala Future

Written by 

Saumya is a Software Consultant at Knoldus Software LLP. She has done B.Tech from Quantum School of Technology, Roorkee. She has good knowledge of Devops technologies like Ansible, Terraform, Docker, Concourse, Jenkins, Kubernetes. She is very enthusiastic and energetic. Apart from technology, she is interested in various sports.

1 thought on “Selenide – concise UI Test in Java5 min read

  1. Hello.
    “Selenide using Chrome Driver :” can be done even simpler:
    Configuration.browser = “chrome”;
    There is a built-in webdrivermanager which will automatically download driver binary.
    “Waiting for elements in Selenide” is not necessary, because Selenide automatically waits for element. The default timeout is 4 seconds and can be changed via Configuration.timeout.
    It is also worth mentioning the built-in proxy which helps to download files without “a href”. And many other cool features.

Comments are closed.

%d bloggers like this: