Helper function to populate form fields

36 katselukertaa
Siirry ensimmäiseen lukemattomaan viestiin

Andrew Myers

lukematon,
5.9.2013 klo 9.09.185.9.2013
vastaanottaja clj-we...@googlegroups.com
Hello!

I've been playing around with clj-webdriver the last few days and it's really great - thank you to all involved.

I'm stuck with one thing at the moment though, and I suspect it may be due to my lack of Clojure skills more than a clj-webdriver issue, but I was hoping someone may be kind enough to give me a push in the right direction.

Basically what I've tried to do is write some helper functions to populate fields in a form. The first one is like this:

(defn input-text-in-named-field
  "Puts the text into the field whose name attribute equals n"
  [n t]
  (input-text (find-element {:css (str "input[name='" n "']")}) t))

In my tests this works great if I call it repeatedly, eg:

(input-text-in-named-field "user[email]" "am2...@example.com")
(input-text-in-named-field "user[login]" "am2605")

So then I thought to myself, I could make this easier by passing in a list of name / value pairs. The next function I came up with was:

(defn input-text-in-named-fields
  "Takes a list of name value pairs, and puts the value into the field with the name"
  [p]
  (map #(input-text-in-named-field (first %) (second %)) p))

Now here's the issue.

If I call this from inside a test, like so:

(deftest register-other
  (browser-up)
  (to (get-registration-url))
  (input-text-in-named-fields '(["user[email]" "am2...@example.com"], ["user[login]" "am2605"])))

The fields in the form don't get populated.

But, if I execute (input-text-in-named-fields '(["user[email]" "am2...@example.com"], ["user[login]" "am2605"])) in the repl, it all works perfectly well. So I'm a bit confused as to what is going on. 

Can anyone see what the problem is please? I've put the code on Github if anyone wants to look at it - https://github.com/am2605/clj-webdriver-fun

*Note*: I've since found the quick-fill function which probably makes this unnecessary anyway, however I'd still really like to know what's going on here, as it might help me as I continue to learn Clojure...

Many thanks,
Andrew.

Andrew Myers

lukematon,
8.9.2013 klo 22.27.088.9.2013
vastaanottaja clj-we...@googlegroups.com
For the record, putting a (dorun ...) around it, eg:

(dorun
(map #(input-text-in-named-field (first %) (second %)) p))

Solve my issue.  I'm still finding my feet with all this!

Cheers,
Andrew.

Alex Baranosky

lukematon,
8.9.2013 klo 22.41.428.9.2013
vastaanottaja clj-we...@googlegroups.com
The thing you're doing wrong is using lazy sequences as a way to execute IO.  The better way to write this is:

(doseq [[name value] name-value-pairs]
  (input-text-in-named-field name value))



--
You received this message because you are subscribed to the Google Groups "clj-webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clj-webdrive...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Daniel Gregoire

lukematon,
8.9.2013 klo 23.09.578.9.2013
vastaanottaja clj-we...@googlegroups.com
Alex is spot on -- you've been bitten by Clojure's `map` being lazy. The `doseq` function is a better choice for side-effect-only operations, or if you need to get the results of the calls as well you can use either `(doall (map ...))` to force evaluation of the lazy seq produced by `map`, or you can just use `mapv` if you don't need Clojure's laziness, which produces a regular Clojure vector instead of a lazy seq.

To make your function a little easier to work with, you can go ahead and store your entries as a regular map with `{"user[email]" "f...@bar.com" "user[login]" "foo"}` or as a sorted map (if you care in what order they're entered on the page) with `(sorted-map "user[email]" "f...@bar.com" "user[login]" "foo"). Your input-text-in-named-fields function would remain unchanged, but your intent would be clearer and it'd be easier to write. 

Andrew Myers

lukematon,
9.9.2013 klo 0.15.289.9.2013
vastaanottaja clj-we...@googlegroups.com
Thanks you Alex and Daniel - I appreciate your taking the time to point me in the right direction.

Best regards,
Andrew.
Vastaa kaikille
Vastaa kirjoittajalle
Välitä
0 uutta viestiä