Selenide is a library for easier using of Selenium WebDriver for automated tests in Java.
@Test public void testLogin() { open("/login"); setValue(By.name("user.name"), "johny"); followLink(By.id("submit")); waitUntil(By.id("username"), hasText("Hello, Johny!")); assertThat($("#insuranceDetailsHeader").getText(), equalTo("Страховые полисы")); assertThat($$("#paymentScheduleTable tr").size(), equalTo(7)); }
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. WebDriver will be automatically created/deleted during test start/finished.
Probably two the most noticeable methods are (inspired from jQuery):
$("#insuranceDetailsHeader") - returns WebElement matching given CSS selector $$("#insuranceDetailsHeader tr td") - returns list of WebElements matching given CSS selector
Just put selenide.jar to your project. Include the following methods and let’s start!
include static com.codeborne.selenide.Navigation.* include static com.codeborne.selenide.DOM.*
Selenium is not a testing library, it just allows you to manipulate browser. Selenide provides a concise API for using Selenium WebDriver in UI tests:
- Transparent WebDriver
-
You don’t need to operate with WebDriver directly. With Selenide, setUp/tearDown methods do it for you.
- Convenience methods
-
Selenide contains convenient methods for operating controls like textfield, radiobutton and selectbox.
- Ajax support
-
when testing Ajax applications, we often need to wait until some element changes its state. Selenide has built-in methods for waiting.
- Screenshots
-
Selenide automatically takes screenshot of the browser window if test has failed.
- Selenium bugs
-
We know that Selenium has some bugs/disadvantages. For instance, when settings field value, the “onChange” event is not always triggered. Selenide contains workarounds for such issues.
- IE
-
Our experience says that Selenium has some problems with IE. Selenide contains several workarounds for IE problems.
You can find more details below.
Selenide contains several methods that make your tests shorter and more readable:
@Test public void canFillComplexForm() { open("/client/registration"); setValue(By.name("user.name"), "johny"); selectRadio("user.gender", "male"); selectOption(By.name("user.preferredLayout"), "plain"); selectOptionByText(By.name("user.securityQuestion"), "What is my first car?"); followLink(By.id("submit")); }
Combining JUnit and Selenium, you can write tests like this:
UGLY EXAMPLES: assertTrue(webdriver.findElement(By.id("topic")).isDisplayed()); assertEquals("Agile engineering practices", webdriver.findElement(By.id("topic")).getText());
With Selenide you can express the same test in a more functional style which is more readable:
GOOD EXAMPLES: assertElement(By.id("topic"), visible); assertElement(By.id("topic"), hasText("Agile engineering practices"));
Other available methods include:
assertHidden(By.id("topic")); assertElement(By.id("topic"), hidden); assertElement(By.id("password"), hasAttribute("name", "user.password")); assertElement(By.id("securityQuestion"), hasOptions()); assertElement(By.id("password"), hasClass("okMessage")); assertElement(By.id("password"), hasNotClass("errorField"));
When testing Ajax applications we often need to wait until some element changes its state. Selenide has built-in methods for waiting:
Selenide allows testing such cases in one-line:
waitFor(By.id("topic")); waitUntil(By.id("topic"), hidden); waitUntil(By.id("password"), hasNotClass("errorField"));
etc.
Our experience says that Selenium WebDriver doesn’t always work correctly with IE browser. But we typically want to test our code with IE because many users still use it.
-
Body tag doesn’t appear immediately - we need to wait for it.
-
IE caches pages - we need to clear cache after every test and generate unique urls.
-
IE crashed when running too many tests at a time. Making pauses between helps.
Selenide contains some workarounds for IE. It was sufficient to make IE working in our projects, but probably you will encounter more problems in your projects - feel free to report them and suggest your workarounds!
Just add a Selenide dependency to your pom, ivy or gradle dependencies configuration:
testCompile 'com.codeborne:selenide:1.6-SNAPSHOT'
Tutorial is in progress now, but you can take a look at a example project which already uses Selenide: github.com/asolntsev/hangman-java/blob/master/src/test/java/ee/era/hangman/uitest/HangmanSpec.java
Until tutorial is ready, you can find some Selenide usages there.
Here we will provide some examples how Selenide can be used to write short and expressive acceptance tests.
LONG: assertEquals("EPP", getElement(By.tagName("title")).getText()); SHORTER: assertElement(By.tagName("title"), hasText("EPP")); SHORT: assertEquals("EPP", title()); LONG: assertEquals(2, getElements(By.className("tellimus")).size()); SHORT: assertEquals(2, $$(".tellimus").size()); LONG: assertThat(getElement(By.id("documentsTable")).findElement(By.tagName("tbody")).findElements(By.tagName("tr")).size(), equalTo(4)); SHORT: assertThat($$("#documentsTable tbody tr").size(), equalTo(4));
Selenide was created by Codeborne, an software development company based in Tallinn, Estonia.
- Author
-
Andrei Solntsev <[email protected]> Anon Keks <[email protected]> Maksim Säkki <[email protected]> Vadim Gerasimov <[email protected]>
- Version
-
1.0
- Licence