Skip to content

Instantly share code, notes, and snippets.

@JensPiegsa
Last active January 20, 2024 11:45
Show Gist options
  • Save JensPiegsa/8ef91c938323ef77d929b77722ed5a8b to your computer and use it in GitHub Desktop.
Save JensPiegsa/8ef91c938323ef77d929b77722ed5a8b to your computer and use it in GitHub Desktop.

Selenide cheat sheet

Selenide is a Java framework for test automation powered by Selenium WebDriver that brings the following advantages:

  • Concise fluent API for tests
  • Ajax support for stable tests
  • Powerful selectors
  • Simple configuration

Official Documentation

(Some examples below also use BDDAssertions from AssertJ).

Maven dependency

<dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>selenide</artifactId>
    <version>5.14.2</version>
    <scope>test</scope>
</dependency>

Java VM options

-Dwebdriver.chrome.driver=tools/webdriver/chromedriver85.exe
-Dselenide.baseUrl=https://example.com/app
-Dselenide.browser=chrome
-Dselenide.startMaximized=true

Imports

import static com.codeborne.selenide.Condition.*;
import static com.codeborne.selenide.CollectionCondition.*;
import static com.codeborne.selenide.Selectors.*;
import static com.codeborne.selenide.Selenide.*;

General

Open URL (relative to base URL)

open("/");

Selections

Select element containing text (sub-string)

$(withText("User")).click();

Select element containing whole text (exact match)

$(byText("User")).click();

Select element by class name

$(byClassName("ui-columntoggler")).click()

Select element by tag name

$(byTagName("button")).click()

Select element by CSS selector

$(byCssSelector("main h1")).shouldHave(text("Users"));

Select element by placeholder text of input field

$(by("placeholder", "Enter firstname")).sendKeys("julia");

Select element by text in sub-section of page

$(byTagName("main")).find(byText("Roles")).click();

Select set of elements

$$(byTagName("h1")).findBy(text("FAQ")).shouldBe(visible);

Assertions

Assert page title

then(title()).contains("Anmeldung");
  • caution: title() does not wait for any potential AJAX responses here

Assert element is visible

$(byText("Populations")).shouldBe(visible);

Assert text matches regular expression

$(byTagName("h1")).should(matchText("Entry [0-9]+"))

Assert size of element set

$$(byTagName("h2")).shouldHave(sizeGreaterThanOrEqual(5));

More complex examples

Use PrimeFaces DataTable column filter

final WebElement rolesColumnHeader = $(byCssSelector(".ui-datatable thead"))
        .find(byText("Roles"));

actions().click(rolesColumnHeader)
        .sendKeys(Keys.TAB)
        // filter drop down menu by typing starting letters        
        .sendKeys("admin") 
        .build().perform();

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment