- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 444字
- 2021-06-24 18:31:49
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:
- 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' }
- 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.
- C++ Primer習題集(第5版)
- Visual C++程序設計教程
- 從零開始:數字圖像處理的編程基礎與應用
- PHP 編程從入門到實踐
- Python機器學習實戰
- Julia Cookbook
- Learning Data Mining with R
- Python:Master the Art of Design Patterns
- 表哥的Access入門:以Excel視角快速學習數據庫開發(第2版)
- Mastering Drupal 8 Views
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(入門與提高篇)
- Extending Puppet(Second Edition)
- Unity 2D Game Development Cookbook
- Fast Data Processing with Spark(Second Edition)
- 微前端設計與實現