Form Helpers

semperos edited this page Feb 27, 2012 · 9 revisions

While clj-webdriver provides individual functions for interacting with HTML forms, if you don't need to monitor a form step-by-step, filling it out should be quick 'n' easy.

Quick Fill

The quick-fill function provides this (and is currently the only function in this namespace). You pass quick-fill a seq of maps, it acts on elements based on the contents of those maps, and then returns a seq of all the elements that were targeted. Each map consists of:

  • A key that describes how to find an element
  • A value that describes what to do with that element

For example:

(quick-fill a-driver
  [{{:name "foobar"}  click},
   {{:name "wowza"}   #(input-text % "Text to type.")}])

As you can see, the how looks like what you'd send to find-element or use in cache rules. The what to do is just a function that accepts one argument: the element that is found. This means that for single-arity functions like click, you can just pass the function, whereas functions of higher arity need to be "wrapped" with either an anonymous or partial function that accepts one argument (as in the case of input-text above).

Form Submission

As soon as the current page reloads or changes in a Selenium-WebDriver browser instance, Selenium-WebDriver's "cache" of element objects for that page expires, and Selenium-WebDriver will throw an exception if you attempt to reference an element object for an "expired" page. For this reason, the default quick-fill behavior of returning a seq of all targeted elements will throw an exception if you submit the form.

If you want to submit the page as part of a set of quick-fill actions, use quick-fill-submit instead. It will return nil instead of the elements affected, thus allowing you to submit the form.


As conveniences for common use cases, the following shortcuts are built into quick-fill:

  • If a key is a string (instead of a find-element style query), it is assumed to be the id attribute of the element you're targeting
  • If a value is a string (instead of a function), it is assumed to be a string that you want to input into the target element (i.e. with the input-text function)

For example:

(quick-fill a-driver
  [{"target-id"      click},
   {{:name "wowza"}  "Text to type.")}
   {"another-id"     "More text to type"}])