Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Examples

Daniel Gregoire edited this page Aug 6, 2015 · 1 revision

Feel free to post snippets of commonly-used functionality below. This may be breeding ground for future built-in clj-webdriver functionality.

Please separate entries with headers that briefly state what the snippet does.

Finding by Regex (filter)

(defn filter-elements-by-regex
  "Given a collection of Element records, filter the collection by the regular expression values for the respective attributes in the `attr-val` map. Entries should be HTML attributes."
  [attr-val elements]
  (let [attr-vals-with-regex (into {}
                                   (filter
                                    #(let [[k v] %] (= java.util.regex.Pattern (class v)))
                                    attr-val))]
    (flatten (for [[attr value] attr-vals-with-regex]
               (reduce (fn [state item]
                         (if ((fnil (partial re-find value) "")
                              (attribute item (name attr)))
                           (conj state item)
                           state))
                       [] elements)))))

;; Usage (Taxi API)
(set-driver! {:browser :firefox} "https://github.com")

;; How many links are on the home page?
(count (elements "a"))
;=> 61

;; How about finding the "Explore" link using a Regex?
(filter-elements-by-regex {:text #"(?i)explore"} (elements "a"))
;=> (#clj_webdriver.element.Element{:webelement #<Tag: <a>, Text: Explore GitHub, Href: https://github.com/explore, Object: org.openqa.selenium.remote.RemoteWebElement@460960f8>})

Custom Firefox Profile with Custom Config

(require '[clj-webdriver.core :as cw])
(require '[clj-webdriver.firefox :as ff])

(def firebug-extension
  "/path/to/firebug-1.9.2.xpi")

(def firefox-profile-json "/path/to/firefox/profile")

(spit firefox-profile-json
      (ff/json
       (doto (ff/new-profile)
         (ff/enable-extension firebug-extension)
         (ff/set-preferences {"extensions.firebug.showFirstRunPage" false}))))

(defn new-firefox-browser []
  (cw/new-driver {:browser :firefox
                  :profile (ff/profile-from-json (slurp firebug-profile-json))}))

How to input text inside an iframe

Reference: Link1 Link2

(switch-to-frame 0)
(switch-to-active)
(send-keys "body" "Thanks to noidi_ :)")
(switch-to-default) ;This is switch back from iframe.
(click (find-element {:tag :button :type :submit})) ;to submit :)

Browser X is not working with Latest Version of Web-Driver

When the latest version of "browser X" doesn't work with clj-webdriver, the simplest way to fix that before clj-webdriver does another release, you can exclude clj-webdriver's dependency on selenium-server in your own project.clj, and then add it back as a dependency in your own code base. Something like this:

(defproject your-project "0.0.1"
  ;Your code
  :dependencies [[org.clojure/clojure "1.4.0"]
                 ;Other dependecies
                 [clj-webdriver "0.6.0-beta3"
                  :exclusions [org.seleniumhq.selenium/selenium-server]]
                 [org.seleniumhq.selenium/selenium-server "2.31.0"]])

Wait and Click

Wait until an element 'e' exists or fn 'e' returns true, and then click the same element OR optionally, another element 'x'. This appears to be quite a common pattern, and the concision helps.

One could think of it as a cousin of quick-fill-submit.

Thanks to @kapilreddy for this version of the utility function.

(defn wait-&-click
  "Wait until an element 'e' exists or fn 'e' returns true, and then click the same element OR
   optionally, another element 'x'."
  ([e]
     (wait-&-click e e))
  ([e x]
     (cond
      (string? e) (wait-until #(exists? e))
      (fn? e) (wait-until e))
     (cond
      (string? x) (click x)
      (element? x) (core/click x))))

A pattern to use with quick-fill-submit

This can be particularly helpful in case certain form data are required at several places. Areas of application include testing registration forms, login forms, content creation forms etc.

;; First express data for target form(s), and then
;; apply quick-fill-submit to selected form data.
(def form-data
  "Designed for use with the \"quick-fill-submit\" helper function."
  {:signup-form-foo [{"#Email" "foo@moo.too"}
                     {"#Passwd" "somethingreallysecure"}
                     {"#signIn" click}])


(defn some-function []
...
...
  (apply quick-fill-submit (:signup-form-foo form-data)) ...)