27. Integrating Clojure by Scripting Web Tests

Imagine the following scenario while coding on your application. “Wow! I never would have thought that touching the FactoryController method would impact the user report! It’s a good thing we had the test on that page!”

Testing suites add concrete business value. They also fly completely under the radar of any standards body in your organization—and so are a rich place for you to try out new ideas.

In this recipe we’ll use WebDriver to open a Google search page and do a search. Then we’ll test the results against our expectations. Let’s get started.

Assumptions

In this chapter we assume you have Firefox installed on your machine; if not, install it now.

Benefits

The benefit of this chapter is that when using Clojure in your day job, one of the great places you can add value with a minimum of risk to your production applications is in your test suite. At the end of this chapter you’ll get a feel for writing WebDriver tests in Clojure.

The Recipe—Code

1. Create a new Leiningen project web-test in your projects directory, and change to that directory:

lein new web-test
cd web-test

2. Modify the projects.clj to look like the following:

(defproject web-test "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.7.0-beta2"]
                 [org.seleniumhq.selenium/selenium-java "2.46.0"]
                 [org.seleniumhq.selenium/selenium-server "2.46.0"]]
  :main web-test.core)

3. Modify src/core.clj to look like the following:

(ns web-test.core
  (:gen-class)
  (:import (org.openqa.selenium.firefox.FirefoxDriver)
           (org.openqa.selenium.support.ui.WebDriverWait)
           (org.openqa.selenium.By)))

(defn do-search [driver]
  (.get driver "http://www.google.com/")
  (let [element1 (.findElement driver (org.openqa.selenium.By/name "q"))]
    (.sendKeys element1 (into-array ["sydney"]))
    (.click (.findElement driver (org.openqa.selenium.By/name "btnG")))))

(defn do-wait [driver]
  (let [wait (org.openqa.selenium.support.ui.WebDriverWait. driver 10)
        condition
        (proxy [org.openqa.selenium.support.ui.ExpectedCondition] []
          (apply [^org.openqa.selenium.WebDriver d]
            (.findElement d (org.openqa.selenium.By/id "resultStats"))))]
    (.until wait condition)))

(defn test-results [driver]
  (let [pageResults (.getPageSource driver)
        result1 (.contains pageResults "Harbor")
        result2 (.contains pageResults "Harbour")]
    (println (str "Is Harbor present? " result1))
    (println (str "Is Harbour present? " result2))))

(defn -main[& args]
  (let [driver (org.openqa.selenium.firefox.FirefoxDriver.)]
    (do-search driver)
    (do-wait driver)
    (test-results driver)
    (.close driver)))

Testing the Recipe

At your command prompt, run the following:

lein run

The Firefox web browser should open, do a Google search, and close, and then you should get the following result in your command prompt:

Is Harbor present? false
Is Harbour present? true

Gosh—it might be time to plan your next holiday!

Notes on the Recipe

This all starts at the –main function that is called from the Leiningen project.clj file. This instantiates the Firefox driver that is used by the rest of the code.

The do-search function opens up a new Google search page. It then enters the term “sydney” into the search field and clicks the Search button.

Note that the condition is a proxy on the Selenium WebDriver ExpectedCondition. We use this in combination with wait to allow us to wait until the search results are loaded, by providing a block until the element specified exists. This is useful on pages that are JavaScript heavy—like the Google search results page.

The do-wait function runs this wait condition, enabling the control flow to block until results come back.

The test results page examines the resulting page source to see if our results came back. It then prints out the results of that check.

For future reference, you may wish to look at the GitHub site at https://github.com/semperos/clj-webdriver.

Conclusion

We’ve used Clojure with WebDriver to check the results on a web page. You can extend this to use Clojure in your workplace and gain greater assurance about your system quality.

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

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