Monday, November 7, 2011

Salty: a Clojure wrapper for the Selenium Java WebDriver

After my last post, I thought it might be fun to make a full-blown clojure library for working with Selenium WebDriver. I want to do a lot of Compojure/Noir development, and I can see where it might be handy to automate logging in and clicking things with Firefox and IE.

Just to get started, I've set up a repository on github, and have written one quick and dirty test function.

(defn test-with-google []
(let [driver (FirefoxDriver.)]
(.get driver "http://www.google.com/")
(println "Original page title is " (.getTitle driver))
(let [element (.findElement driver (By/name "q"))]
(.sendKeys element (into-array ["clojure\n"]))
(.submit element)
(-> (WebDriverWait. driver 10)
(.until (proxy [ExpectedCondition] []
(apply [d]
(-> (.getTitle d)
(.toLowerCase)
(.startsWith "clojure"))))))
(println "Page title after searching is " (.getTitle driver))
(.quit driver))))


It's not really useful, it's just a quick spot-check you can run at the REPL to make sure everything's set up right. Make sure salty is in your classpath, and then, at the REPL, type (salty.impl/test-with-google). You should see Firefox start up, open up the main Google page, search for "clojure", and then quit. The output in your REPL should be:
Original page title is  Google
Page title after searching is  clojure - Google Search
nil

More to come...

2 comments:

  1. Check out clj-webdriver too: https://github.com/semperos/clj-webdriver

    ReplyDelete
  2. Ah, I searched for clj-selenium but not clj-webdriver. Thanks for the link.

    ReplyDelete