From 47f2793498b57734586385a7a08271fd9041556a Mon Sep 17 00:00:00 2001 From: "Alex.Shi" Date: Tue, 11 Aug 2020 11:26:01 +0300 Subject: [PATCH] add shortcut wait-has-text-everywhere (#274) * fix wait-has-text * update * update * fix indent --- README.md | 14 ++++++++++++++ src/etaoin/api.clj | 15 ++++++++++++-- test/etaoin/api_test.clj | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 21c5a70d..63b338b0 100644 --- a/README.md +++ b/README.md @@ -1487,6 +1487,20 @@ Example from etaoin test: (is true "text found")))) ``` +Wait text: + +- `wait-has-text` waits until an element has text anywhere inside it (including inner HTML). + + ``` clojure + (wait-has-text driver :wait-span "-secret-") + ``` + +- `wait-has-text-everywhere` like `wait-has-text` but searches for text across the entire page + + ``` clojure + (wait-has-text-everywhere driver "-secret-") + ``` + ## Writing Integration Tests For Your Application ### Basic fixture diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index 9e47e91a..e272e6c5 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -2197,16 +2197,27 @@ Arguments: - `driver`: a driver instance; - - `q`: a query term (see `query`); + - `q`: a query term (see `query`). - `text`: a string to search; - `opt`: a map of options (see `wait-predicate`)." - [driver q text & [opt]] (let [message (format "Wait for %s element has text %s" q text)] (wait-predicate #(has-text? driver q text) (assoc opt :message message)))) +(defn wait-has-text-everywhere + "Like `wait-has-text` but searches for text across the entire page. + + Arguments: + + - `driver`: a driver instance; + - `text`: a string to search; + - `opt`: a map of options (see `wait-predicate`)." + [driver text & [opt]] + (let [q {:xpath "*"}] + (wait-has-text driver q text opt))) + (defn wait-has-class "Waits until an element has specific class. diff --git a/test/etaoin/api_test.clj b/test/etaoin/api_test.clj index 981979d9..2a41ba61 100644 --- a/test/etaoin/api_test.clj +++ b/test/etaoin/api_test.clj @@ -294,6 +294,48 @@ :interval 0.33 :times 7})))))) +(deftest test-wait-has-text-everywhere + (testing "wait for text simple" + (doto *driver* + (refresh) + (wait-visible {:id :document-end}) + (click {:id :wait-button}) + (wait-has-text-everywhere "-secret-")) + (is true "text found")) + (testing "wait for text timeout" + (doto *driver* + (refresh) + (wait-visible {:id :document-end}) + (click {:id :wait-button})) + (try+ + (wait-has-text-everywhere *driver* + "-secret-" + {:timeout 1}) + (is false "should not be executed") + (catch [:type :etaoin/timeout] data + (is (= (-> data (dissoc :predicate :time-rest)) + {:type :etaoin/timeout + :message "Wait for {:xpath \"*\"} element has text -secret-" + :timeout 1 + :interval 0.33 + :times 4}))))) + (testing "wait for non-existing text" + (doto *driver* + (refresh) + (wait-visible {:id :document-end})) + (try+ + (wait-has-text-everywhere *driver* + "-dunno-whatever-foo-bar-" + {:timeout 2}) + (is false "should not be executed") + (catch [:type :etaoin/timeout] data + (is (= (-> data (dissoc :predicate :time-rest)) + {:type :etaoin/timeout + :message "Wait for {:xpath \"*\"} element has text -dunno-whatever-foo-bar-" + :timeout 2 + :interval 0.33 + :times 7})))))) + (deftest test-wait-has-class (is 1) (testing "wait for an element has class"