Selenide

What we have seen about Selenium is very cool. It brings the opportunity to probe that our application is doing things well, but sometimes it is a bit tricky to configure and use. Selenide is a project based on Selenium that offers a good syntax for writing tests and makes them more readable. It hides the usage of WebDriver and configurations from you, while still maintaining a high-level of customization:

  1. Like all the other libraries we have used until now, the first step is to add the Gradle dependency:
dependencies { 
    testCompile 'com.codeborne:selenide:2.17' 
}
  1. Let's see how we can write the previous Selenium test using Selenide
    instead. The syntax might be familiar to for those who know JQuery (https://jquery.com/):
public class SelenideTest { 
  @Test 
  public void wikipediaSearchFeature() throws 
InterruptedException {
// Opening Wikipedia page open("http://en.wikipedia.org/wiki/Main_Page"); // Searching TDD $(By.name("search")).setValue("Test-driven development"); // Clicking search button $(By.name("go")).click(); // Checks assertThat(title(),
startsWith("Test-driven development")); } }

This was a more expressive way to write a test. On top of a more fluent syntax, there are some things that happen behind this code and would require additional lines of Selenium. For example, a click action will wait until an element in question is available, and will fail only if the predefined period of time has expired. Selenium, on the other hand, would fail immediately. In today's world, with many elements being loaded dynamically through JavaScript, we cannot expect everything to appear at once. Hence, this Selenide feature proves to be useful and saves us from using repetitive boilerplate code. There are many other benefits Selenide brings to the table. Due to the benefits that Selenide provides when compared with Selenium, it will be our framework of choice throughout this book. Furthermore, there is a whole chapter dedicated to web testing using this framework. Visit http://selenide.org/ for more information on ways to use web drivers in your tests.

No matter whether tests were written with one framework or another, the effect is the same. When tests are run, a Firefox browser window will emerge and execute all steps defined in the test sequentially. Unless a headless browser was chosen as your driver of choice, you will be able to see what is going on throughout the test. If something goes wrong, a failure trace is available. On top of that, we can take browser screenshots at any point. For example, it is a common practice to record the situation at the time of a failure.

The complete source code can be found in the SelenideTest class in the
https://bitbucket.org/vfarcic/tdd-java-ch02-example-web repository.

Armed with a basic knowledge of web-testing frameworks, it is time to take a short look at BDD.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.33.235