Skip to content

Commit

Permalink
Implement ->actions macro for creating composite actions using SW's A…
Browse files Browse the repository at this point in the history
…ctions class
  • Loading branch information
semperos committed Feb 18, 2012
1 parent 9396d30 commit 5526869
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
52 changes: 40 additions & 12 deletions src/clj_webdriver/core.clj
Expand Up @@ -29,6 +29,7 @@
[com.opera.core.systems OperaDriver]
[org.openqa.selenium.htmlunit HtmlUnitDriver]
[org.openqa.selenium.support.ui Select]
org.openqa.selenium.interactions.Actions
[java.util Date]
[java.io File]))

Expand Down Expand Up @@ -136,23 +137,26 @@
(defprotocol IActions
"Methods available in the Actions class"
(click-and-hold
[driver]
[driver element] "Drag and drop, either at the current mouse position or in the middle of a given `element`.")
[this]
[this element] "Drag and drop, either at the current mouse position or in the middle of a given `element`.")
(double-click
[driver]
[driver element] "Double click, either at the current mouse position or in the middle of a given `element`.")
(drag-and-drop [driver element-a element-b] "Drag and drop `element-a` onto `element-b`.")
(drag-and-drop-by [driver element x y] "Drag `element` by `x` pixels to the right and `y` pixels down.")
[this]
[this element] "Double click, either at the current mouse position or in the middle of a given `element`.")
(drag-and-drop [this element-a element-b] "Drag and drop `element-a` onto `element-b`.")
(drag-and-drop-by [this element x y] "Drag `element` by `x` pixels to the right and `y` pixels down.")
(key-down
[driver k]
[driver element k] "Press the given key (e.g., (key-press driver :enter))")
[this k]
[this element k] "Press the given key (e.g., (key-press driver :enter))")
(key-up
[driver k]
[driver element k] "Release the given key (e.g., (key-press driver :enter))")
[this k]
[this element k] "Release the given key (e.g., (key-press driver :enter))")
(move-by-offset [driver x y] "Move mouse by `x` pixels to the right and `y` pixels down.")
(move-to-element
[driver element]
[driver element x y] "Move the mouse to the given element, or to an offset from the given element."))
[this element]
[this element x y] "Move the mouse to the given element, or to an offset from the given element.")
(release
[this]
[this element] "Release the left mouse button, either at the current mouse position or in the middle of the given `element`."))


;; ## Starting Driver/Browser ##
Expand Down Expand Up @@ -284,3 +288,27 @@
(.executeScript driver js (to-array js-args)))

(load "core_driver")

(defmacro ->build-composite-action
"Create a composite chain of actions, then call `.build()`. This does **not** execute the actions; it simply sets up an 'action chain' which can later by executed using `.perform()`.
Unless you need to wait to execute your composite actions, you should prefer `->actions` to this macro."
[driver & body]
`(let [act# (:actions ~driver)]
(doto act#
~@body
.build)))

(defmacro ->actions
[driver & body]
`(let [act# (:actions ~driver)]
(doto act#
~@body
.perform)
~driver))

;; e.g.
;; Action dragAndDrop = builder.clickAndHold(someElement)
;; .moveToElement(otherElement)
;; .release(otherElement)
;; .build()
67 changes: 64 additions & 3 deletions src/clj_webdriver/core_driver.clj
Expand Up @@ -441,11 +441,13 @@
(let [act (:actions driver)]
(.perform (.doubleClick act (:webelement element))))))

(drag-and-drop [driver element-a element-b]
(drag-and-drop
[driver element-a element-b]
(let [act (:actions driver)]
(.perform (.dragAndDrop act (:webelement element-a) (:webelement element-b)))))

(drag-and-drop-by [driver element x y]
(drag-and-drop-by
[driver element x y]
(let [act (:actions driver)]
(.perform (.dragAndDropBy act (:webelement element) x y))))

Expand Down Expand Up @@ -476,4 +478,63 @@
(.perform (.moveToElement act (:webelement element)))))
([driver element x y]
(let [act (:actions driver)]
(.perform (.moveToElement act (:webelement element) x y))))))
(.perform (.moveToElement act (:webelement element) x y)))))

(release
([driver]
(let [act (:actions driver)]
(.release act)))
([driver element]
(let [act (:actions driver)]
(.release act element)))))

(extend-type org.openqa.selenium.interactions.Actions

IActions
(click-and-hold
([act]
(.perform (.clickAndHold act)))
([act element]
(.perform (.clickAndHold act (:webelement element)))))

(double-click
([act]
(.perform (.doubleClick act)))
([act element]
(.perform (.doubleClick act (:webelement element)))))

(drag-and-drop
[act element-a element-b]
(.perform (.dragAndDrop act (:webelement element-a) (:webelement element-b))))

(drag-and-drop-by
[act element x y]
(.perform (.dragAndDropBy act (:webelement element) x y)))

(key-down
([act k]
(.perform (.keyDown act (key-code k))))
([act element k]
(.perform (.keyDown act (:webelement element) (key-code k)))))

(key-up
([act k]
(.perform (.keyUp act (key-code k))))
([act element k]
(.perform (.keyUp act (:webelement element) (key-code k)))))

(move-by-offset
[act x y]
(.perform (.moveByOffset act x y)))

(move-to-element
([act element]
(.perform (.moveToElement act (:webelement element))))
([act element x y]
(.perform (.moveToElement act (:webelement element) x y))))

(release
([act]
(.release act))
([act element]
(.release act (:webelement element)))))

0 comments on commit 5526869

Please sign in to comment.