Back in October, I mentioned that we were using Clojure to drive our WebDriver tests (Selenium) but I didn't go into much detail. I actually wrote a blog post back then about it which shows how I controlled when the browser opened and closed (for a single
test or a suite of tests), as well as showing what a test looked like:
Automated Browser-based Testing with Clojure
We're really liking Clojure as a testing language since we can easily create domain-specific functions that encapsulate certain actions within our web site so that our tests read much more cleanly. An example from that blog post:
And by way of explanation:
The login and go-to functions are just conveniences (in testsuite.core) to login to a specified website with a given username and password (:eg represents eligiblegreeks.com) and go to a specific URL. This test verifies that if you make a change (on this test profile), you get the success message and you still get the partial profile message.
One of the real pleasures of working with WebDriver in Clojure is that it's easy to develop tests in the REPL, driving the web browser interactively as you type in code and evaluate it, form by form. You can tell the browser to navigate around your application, develop domain specific functions to encapsulate that logic, and verify assumptions about the application's behavior, directly and interactively - without any sort of edit, compile, run cycle.
We've also been using Clojure to test-drive the development of a new REST API we are working on. We wrote simple Clojure functions to perform GET and POST operations on the REST API endpoint (using clj-http) and then we can write tests like:
Very clear and expressive. This code is using Jay Fields' "Expectations" library for a more BDD-style of test. The WebDriver test above uses Clojure's built-in testing library.