From 44e6caefca873b36b61330c9cf0e8cd7229755a3 Mon Sep 17 00:00:00 2001 From: Jeff Weiss Date: Thu, 17 May 2012 12:22:39 -0400 Subject: [PATCH 1/2] allow fill-form to also take a list, so that the items are filled in in-order. --- project.clj | 2 +- src/com/redhat/qe/auto/selenium/selenium.clj | 21 +++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/project.clj b/project.clj index 9ac4c28..1a0dba1 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject com.redhat.qe/extended-selenium "1.0.3" +(defproject com.redhat.qe/extended-selenium "1.0.3.1" :description "An extension of the selenium RC client with extra logging and convenience methods" :java-source-path "src" :dependencies [[org.seleniumhq.selenium.client-drivers/selenium-java-client-driver "1.0.2"] diff --git a/src/com/redhat/qe/auto/selenium/selenium.clj b/src/com/redhat/qe/auto/selenium/selenium.clj index 8aaa833..863bc66 100644 --- a/src/com/redhat/qe/auto/selenium/selenium.clj +++ b/src/com/redhat/qe/auto/selenium/selenium.clj @@ -57,16 +57,19 @@ will be looked up and converted to String locators (see locator-args)" :else (browser setText el val)))) (defn fill-form - "Fills in a standard HTML form. items-map is a mapping of locators - of form elements, to the string values that should be selected or - entered. 'submit' is a locator for the submit button to click at - the end. Optional no-arg fn argument post-fn will be called after the - submit click." - [items-map submit & [post-fn]] - (let [filtered (select-keys items-map - (for [[k v] items-map :when (not (nil? v))] k))] + "Fills in a standard HTML form. items is a mapping of locators of + form elements, to the string values that should be selected or + entered. If you care about the order the items are filled in, use a + list instead of a map. 'submit' is a locator for the submit button + to click at the end. Optional no-arg fn argument post-fn will be + called after the submit click." + [items submit & [post-fn]] + (let [ordered-items (if (sequential? items) + (partition 2 items) + (into [] items)) + filtered (filter #(not= nil (second %)) ordered-items )] (when (-> filtered count (> 0)) - (doseq [[el val] filtered] + (doseq [[el val] ordered-items] (fill-item el val)) (browser click submit) ((or post-fn load-wait))) From 8c27d85fd21792d7fc4a65c69703a9aaef830f94 Mon Sep 17 00:00:00 2001 From: Jeff Weiss Date: Thu, 17 May 2012 12:37:50 -0400 Subject: [PATCH 2/2] Allow fill-item to also call functions - useful for non-standard inputs. --- src/com/redhat/qe/auto/selenium/selenium.clj | 27 +++++++++++++------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/com/redhat/qe/auto/selenium/selenium.clj b/src/com/redhat/qe/auto/selenium/selenium.clj index 863bc66..850bac0 100644 --- a/src/com/redhat/qe/auto/selenium/selenium.clj +++ b/src/com/redhat/qe/auto/selenium/selenium.clj @@ -50,19 +50,28 @@ will be looked up and converted to String locators (see locator-args)" (defn load-wait [] (browser waitForPageToLoad "60000")) -(defn fill-item [el val] - (let [eltype (browser getElementType el)] - (cond (= eltype "selectlist") (browser select el val) - (= eltype "checkbox") (browser checkUncheck el (boolean val)) - :else (browser setText el val)))) +(defn fill-item + "If el is a function, assume vals are args, and call el with args. Otherwise, + el is an element, and it's filled in with val depending on what + type it is." + [el val] + (if (fn? el) + (apply el val) + (let [eltype (browser getElementType el)] + (cond (= eltype "selectlist") (browser select el val) + (= eltype "checkbox") (browser checkUncheck el (boolean val)) + :else (browser setText el val))))) (defn fill-form "Fills in a standard HTML form. items is a mapping of locators of form elements, to the string values that should be selected or - entered. If you care about the order the items are filled in, use a - list instead of a map. 'submit' is a locator for the submit button - to click at the end. Optional no-arg fn argument post-fn will be - called after the submit click." + entered. You can also map function names to arglists, to perform + other tasks while filling in the form. If you care about the order + the items are filled in, use a list instead of a map. 'submit' is a + locator for the submit button to click at the end. Optional no-arg + fn argument post-fn will be called after the submit click. + Example: + (fill-form [:user 'joe' :password 'blow' choose-type ['manager']] :submit)" [items submit & [post-fn]] (let [ordered-items (if (sequential? items) (partition 2 items)