Skip to content
Andrei Solntsev edited this page Mar 12, 2023 · 6 revisions

Wait for an event

  $(".username").shouldHave(text("John"));

All "should"- methods automatically wait until web element appears and gets given property (text in this example). Timeout is 4 seconds by default.

Wait for an event with a timeout (longer than default):

  $("#username").shouldHave(text("Hello, Johny!"), Duration.ofSeconds(8));

Note that you need these overloaded should* methods in rare specific cases - when you definitely know that this is a long-lasting query, and event is designed to happen after a pause (longer than default 4 seconds).

Get list of matched elements

  $$("#paymentScheduleTable tr").shouldHave(size(12));

Get element by index

  $("#paymentScheduleTable tr", 5).shouldHave(text("42.00 USD"));

Clear browser cache

  clearBrowserCache();

Concise API

Here we will provide some examples how Selenide can be used to write short and expressive UI tests.

Example 1: Checking the page title

import static com.codeborne.selenide.Selenide.webdriver;
import static com.codeborne.selenide.WebDriverConditions.*;

webdriver().shouldHave(url("https://auth.google.com"));
webdriver().shouldHave(url("https://mastercard.ee"), Duration.ofSeconds(42));
webdriver().shouldNotHave(url("http://yandex.ru");
webdriver().shouldNotHave(urlStartingWith("ftp://"));
webdriver().shouldHave(currentFrameUrl(baseUrl + "/login.html"));
webdriver().shouldHave(currentFrameUrlStartingWith(baseUrl + "/logout.html"));

Example 2: Checking the number of elements

  $$(".item").shouldHave(size(2));

For comparison, Selenium way:

  assertEquals(2, getElements(By.className("item")).size());

Example 3: Finding elements inside parent

  $("#documentsTable", 2).findAll("tbody tr").shouldHave(size(4));

For comparison, Selenium way:

  assertThat( getElement(By.id("documentsTable")).findElement( By.tagName("tbody")).findElements( By.tagName("tr")).size(), equalTo(4));