Sunday, November 27, 2011

What I've learned from Salty

I spent the weekend hacking at my Salty library for Clojure. Salty is a thin wrapper around the Selenium WebDriver framework, and it allows you to programmatically control a web browser like Firefox, as I've mentioned before. It turns out there is already a Clojure lib for WebDriver---clj-webdriver---that is full-featured, easy to use, and very mature. For that reason, I'm probably not going to invest too much more effort in Salty. But that's fine: I mostly wanted to try this as a learning exercise, and clj-webdriver makes it easier for me to check my homework. Here's a few notes on what I think I got right and what I think I got wrong.

Sunday, November 20, 2011

Re-formatting variable names

(Series: From PHP to Clojure)

Here's a simple problem: as part of my Salty lib, I want a function that will take camel-case variable names, as used by Java, and convert them to the dash format that's idiomatic in Clojure. In other words, I want a function that will take the string "someVariableName" (camel case)and return "some-variable-name" (with dashes).


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...