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

Composite Actions

semperos edited this page Feb 18, 2012 · 4 revisions

As part of an effort to support more complex user interactions, Selenium-WebDriver has added an Actions class that handles things like drag-and-drop functionality (see this page on their wiki).

Clj-webdriver provides equivalent Clojure functions for individual methods in that class, as well as two specialized threading macros to help build composite actions and execute them. See the following two examples:

;; Compose several advanced actions and immediately perform them on the page
(->actions b
  (click-and-hold (find-element b {:id "draggable"}))
  (move-to-element (find-element b {:id "droppable"}))
  (release (find-element b {:id "droppable"})))

;; Build a composite action and perform it later
(def my-comp-act (->build-composite-action b
                   (click-and-hold (find-element b {:id "draggable"}))
                   (move-to-element (find-element b {:id "droppable"}))
                   (release (find-element b {:id "droppable"}))))

(perform my-comp-act)

The following actions are available for composite actions:

  • click-and-hold
  • double-click
  • drag-and-drop
  • drag-and-drop-by
  • key-down
  • key-up
  • move-by-offset
  • move-to-element
  • release

If you want to use these in one-off situations, they can be used individually against a Driver instance.

Underlying Implementation

If you need to compose actions as seen in the above examples, note that clj-webdriver has extended org.openqa.selenium.interactions.Actions and CompositeAction of the same package. This allows the two macros ->actions and ->build-composite-action to encapsulate the use of these Java objects, while making it easy to string together the advanced actions listed above.

The ->actions macro threads an instance of Actions that is attached to every Driver record, finally calling the .perform() method of the Actions class and returning the Driver originally passed in. The ->build-composite-action macro also threads an instance of Actions attached to the given Driver, but returns a CompositeAction object. The perform function is simply a wrapper around the .perform() method of the CompositeAction class.

Two important notes:

  1. Only the above functions are supported with composite actions, due to Selenium-WebDriver's API.
  2. The ->actions and ->build-composite-action macros are threading macros, so you should not pass in a driver instance to the functions in the body of each macro.
Clone this wiki locally