Navigation Menu

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

Commit

Permalink
Update README to use Github-style md for code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
semperos committed Jul 14, 2011
1 parent 77e36c8 commit 88cc5b5
Showing 1 changed file with 90 additions and 80 deletions.
170 changes: 90 additions & 80 deletions README.md
Expand Up @@ -8,38 +8,44 @@ This is a Clojure library for driving a web browser using Selenium-WebDriver as


Use/require the library in your code: Use/require the library in your code:


(use 'clj-webdriver.core) ```clj
(use 'clj-webdriver.core)
```


Start up a browser: Start up a browser:


(def b (start :firefox "https://github.com")) ```clj
(def b (start :firefox "https://github.com"))
```


At the moment, the best documentation is the source code itself. While there are many functions in the core namespace, they're mostly short and straightforward wrappers around WebDriver API's. For the task of finding elements on the page, I've added some utility functions at the end of the core namespace. At the moment, the best documentation is the source code itself. While there are many functions in the core namespace, they're mostly short and straightforward wrappers around WebDriver API's. For the task of finding elements on the page, I've added some utility functions at the end of the core namespace.


Here's an example of logging into Github: Here's an example of logging into Github:


;; Start the browser and bind it to `b` ```clj
(def b (start :firefox "https://github.com")) ;; Start the browser and bind it to `b`

(def b (start :firefox "https://github.com"))
;; Click "Login" link
(-> b ;; Click "Login" link
(find-it {:text "Login"}) (-> b
click) (find-it {:text "Login"})

click)
;; Input username/email into the "Login or Email" field
(-> b ;; Input username/email into the "Login or Email" field
(find-it {:class "text", :name "login"}) ; use multiple attributes (-> b
(input-text "username")) (find-it {:class "text", :name "login"}) ; use multiple attributes

(input-text "username"))
;; Input password into the "Password" field
(-> b ;; Input password into the "Password" field
(find-it {:xpath "//input[@id='password']"}) ; :xpath and :css options (-> b
(input-text "password")) (find-it {:xpath "//input[@id='password']"}) ; :xpath and :css options

(input-text "password"))
;; Click the "Log in" button"
(-> b ;; Click the "Log in" button"
(find-it :input {:value #"(?i)log"}) ; use of regular expressions (-> b
click) (find-it :input {:value #"(?i)log"}) ; use of regular expressions
click)
```


The key functions for finding an element on the page are `find-it` and `find-them`. The `find-it` function returns the first result that matches the criteria, while `find-them` returns a seq of all matches for the given criteria. Both support the same syntax and set of attributes. The key functions for finding an element on the page are `find-it` and `find-them`. The `find-it` function returns the first result that matches the criteria, while `find-them` returns a seq of all matches for the given criteria. Both support the same syntax and set of attributes.


Expand All @@ -57,71 +63,75 @@ As mentioned above, the `find-it` and `find-them` functions share the same featu


To demonstrate how to use arguments in different ways, consider the following example. If I wanted to find `<a href="/contact" id="contact-link" class="menu-item" name="contact">Contact Us</a>` in a page and click on it I could perform any of the following: To demonstrate how to use arguments in different ways, consider the following example. If I wanted to find `<a href="/contact" id="contact-link" class="menu-item" name="contact">Contact Us</a>` in a page and click on it I could perform any of the following:


(-> b ```clj
(find-it :a) ; assuming its the first <a> on the page (-> b
click) (find-it :a) ; assuming its the first <a> on the page

click)
(-> b
(find-it {:id "contact-link"}) ; :id is unique, so only one is needed (-> b
click) (find-it {:id "contact-link"}) ; :id is unique, so only one is needed

click)
(-> b
(find-it {:class "menu-item", :name "contact"}) ; use multiple attributes (-> b
click) (find-it {:class "menu-item", :name "contact"}) ; use multiple attributes

click)
(-> b
(find-it :a {:class "menu-item", :name "contact"}) ; specify tag (-> b
click) (find-it :a {:class "menu-item", :name "contact"}) ; specify tag

click)
(-> b
(find-it :a {:text "Contact Us"}) ; special :text attribute, uses XPath's (-> b
click) ; text() function to find the element (find-it :a {:text "Contact Us"}) ; special :text attribute, uses XPath's

click) ; text() function to find the element
(-> b
(find-it :a {:class #"(?i)menu-"}) ; use Java-style regular (-> b
click) ; expressions (find-it :a {:class #"(?i)menu-"}) ; use Java-style regular

click) ; expressions
(-> b
(find-it [:div {:id "content"}, :a {:id "contact-link"}]) ; hierarchical/ancestry-based query (-> b
click) ; equivalent to (find-it [:div {:id "content"}, :a {:id "contact-link"}]) ; hierarchical/ancestry-based query
; //div[@id='content']//a[@id='contact-link'] click) ; equivalent to

; //div[@id='content']//a[@id='contact-link']
(-> b
(find-it [:div {:id "content"}, :a {}]) ; ancestry-based query, tag with (-> b
click) ; no attributes (empty map required) (find-it [:div {:id "content"}, :a {}]) ; ancestry-based query, tag with

click) ; no attributes (empty map required)
(-> b
(find-it {:xpath "//a[@id='contact-link']"}) ; XPath query (-> b
click) (find-it {:xpath "//a[@id='contact-link']"}) ; XPath query

click)
(-> b
(find-it {:css "a#contact-link"}) ; CSS selector
click)


(-> b
(find-it {:css "a#contact-link"}) ; CSS selector
click)
```


So, to describe the general pattern of interacting with the page: So, to describe the general pattern of interacting with the page:


(-> browser-instance ```clj
(find-it options) (-> browser-instance
(do-something-with-the-element)) (find-it options)
(do-something-with-the-element))
```


### Firefox Functionality ### Firefox Functionality


Support for Firefox currently exceeds that for all other browsers, most notably via support for customizable Firefox profiles. I've included support for several of these advanced featues in the `clj-webdriver.firefox` namespace. Here are a few examples (borrowed from [here][wd-ruby-bindings]: Support for Firefox currently exceeds that for all other browsers, most notably via support for customizable Firefox profiles. I've included support for several of these advanced featues in the `clj-webdriver.firefox` namespace. Here are a few examples (borrowed from [here][wd-ruby-bindings]:


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

(require '[clj-webdriver.firefox :as ff])
(def b (new-driver :firefox
(doto (ff/new-profile) (def b (new-driver :firefox
;; Enable Firebug (doto (ff/new-profile)
(ff/enable-extension "/path/to/extensions/firebug.xpi"))) ;; Enable Firebug
(ff/enable-extension "/path/to/extensions/firebug.xpi")))
;; Auto-download certain file types to a specific folder
(ff/set-preferences {:browser.download.dir "C:/Users/semperos/Desktop", ;; Auto-download certain file types to a specific folder
:browser.download.folderList 2 (ff/set-preferences {:browser.download.dir "C:/Users/semperos/Desktop",
:browser.helperApps.neverAsk.saveToDisk "application/pdf"}))) :browser.download.folderList 2

:browser.helperApps.neverAsk.saveToDisk "application/pdf"})))
```


## Running Tests ## Running Tests
Expand Down

0 comments on commit 88cc5b5

Please sign in to comment.