-
Notifications
You must be signed in to change notification settings - Fork 598
Selenide vs Selenium
Andrei Solntsev edited this page Sep 29, 2025
·
20 revisions
This page brings an overview of how Selenide API is simpler and more powerful than Selenium WebDriver API.
Remember, this doesn't mean that Selenium WebDriver is "bad":
- Selenium WebDriver just has lower-level API which is suitable for wider range of tasks.
- Selenide API is higher-level, but it was designed specifically for UI- and Acceptance testing.
Though Selenium WebDriver is a great library for operating web browser, it's API is too low-level. Developer needs to write some boilerplate code to create/shutdown webdriver, to search radio buttons, to wait for javascript interactions etc. With Selenide You don't need to operate with Selenium WebDriver directly, don't need to wait for ajax requests etc.
So, let's compare how to do primitive actions with both libraries. Probably the most tasty feature is the Ajax support.
DesiredCapabilities desiredCapabilities = DesiredCapabilities.htmlUnit();
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDSELECTIONERROR, true);
desiredCapabilities.setCapability(HtmlUnitDriver.INVALIDXPATHERROR, false);
desiredCapabilities.setJavascriptEnabled(true);
WebDriver driver = new HtmlUnitDriver(desiredCapabilities);open("/my-application/login");
// And run tests with option -Dbrowser=htmlunit (or "chrome" or "ie", default value is "firefox")if (driver != null) {
driver.close();
}// Do not care! Selenide closes the browser automatically.
With Selenide You don't need to operate with Selenium WebDriver directly. WebDriver will be automatically created/deleted during test start/finished.
WebElement customer = driver.findElement(By.id("customerContainer"));WebElement customer = $("#customerContainer"); or a longer conservative option:
WebElement customer = $(By.id("customerContainer"));assertEquals("Customer profile", driver.findElement(By.id("customerContainer")).getText());$("#customerContainer").shouldHave(text("Customer profile"));FluentWait<By> fluentWait = new FluentWait<By>(By.tagName("TEXTAREA"));
fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);
fluentWait.until(new Predicate<By>() {
public boolean apply(By by) {
try {
return browser.findElement(by).isDisplayed();
} catch (NoSuchElementException ex) {
return false;
}
}
});
assertEquals("John", browser.findElement(By.tagName("TEXTAREA")).getAttribute("value"));
$("TEXTAREA").shouldHave(value("John"));This command automatically waits until element gets visible AND gets expected value.
Default timeout is 4 seconds and it's configurable.
assertTrue(driver.findElement(By.id("customerContainer")).getAttribute("class").indexOf("errorField") > -1);$("#customerContainer").shouldHave(cssClass("errorField"));No way (except XPath)
WebElement customer = $(byText("Customer profile")); WebElement element = driver.findElement(By.id("customerContainer"));
assertTrue(Pattern.compile(".*profile.*", DOTALL).matcher(element.getText()).matches());$("#customerContainer").should(matchText("profile"));try {
WebElement element = driver.findElement(By.id("customerContainer"));
fail("Element should not exist: " + element);
}
catch (WebDriverException itsOk) {}$("#customerContainer").shouldNot(exist);WebElement parent = driver.findElement(By.id("customerContainer"));
WebElement element = parent.findElement(By.className("user_name"));$("#customerContainer").find(".user_name");WebElement element = driver.findElements(By.tagName("li")).get(5);$("li", 5); try {
Alert alert = checkAlertMessage(expectedConfirmationText);
alert.accept();
} catch (UnsupportedOperationException alertIsNotSupportedInHtmlUnit) {
return;
}
Thread.sleep(200); // sometimes it will fail confirm("Are you sure to delete your profile?");or
dismiss("Are you sure to delete your profile?"); WebElement element = driver.findElement(By.id("customerContainer"));
System.out.println("tag: " + element.getTag());
System.out.println("text: " + element.getText());
System.out.println("id: " + element.getAttribute("id"));
System.out.println("name: " + element.getAttribute("name"));
System.out.println("class: " + element.getAttribute("class"));
System.out.println("value: " + element.getAttribute("value"));
System.out.println("visible: " + element.isDisplayed());
// etc. System.out.println($("#customerContainer"));
// output looks like this: "<option value=livemail.ru checked=true selected:true>@livemail.ru</option>" if (driver instanceof TakesScreenshot) {
File scrFile = ((TakesScreenshot) webdriver).getScreenshotAs(OutputType.FILE);
File targetFile = new File("c:\temp\" + fileName + ".png");
FileUtils.copyFile(scrFile, targetFile);
} takeScreenShot("my-test-case");For JUnit users it's even more simpler:
public class MyTest {
@Rule // automatically takes screenshot of every failed test
public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
} for (WebElement radio : driver.findElements(By.name("sex"))) {
if ("woman".equals(radio.getAttribute("value"))) {
radio.click();
}
}
throw new NoSuchElementException("'sex' radio field has no value 'woman'"); selectRadio(By.name("sex"), "woman"); webdriver.navigate().to(webdriver.getCurrentUrl()); refresh(); webdriver.getCurrentUrl();
webdriver.getTitle();
webdriver.getPageSource(); url();
title();
source();Then try it yourself! Let's start writing concise UI Tests!