From c7a97305a220bfe610c733315ad72c5001573441 Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Tue, 28 Jul 2020 15:54:09 +0300 Subject: [PATCH] add edge support --- src/etaoin/api.clj | 24 +++++++++++++++++++++-- src/etaoin/driver.clj | 12 ++++++++++-- test/etaoin/api_test.clj | 42 +++++++++++++++++++++------------------- 3 files changed, 54 insertions(+), 24 deletions(-) diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index ae5945d5..5477d9d5 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -1572,7 +1572,7 @@ dispatch-driver) (defmethods get-log-types - [:chrome :edge :phantom] ;;TODO only jwp edge not supported + [:chrome :phantom] [driver] (:value (execute {:driver driver :method :get @@ -2291,6 +2291,11 @@ [driver & body] `(when-predicate #(firefox? ~driver) ~@body)) +(defmacro when-edge + "Executes the body only if the driver is Edge." + [driver & body] + `(when-predicate #(edge? ~driver) ~@body)) + (defmacro when-safari "Executes the body only if the driver is Safari." [driver & body] @@ -3000,6 +3005,7 @@ (partial boot-driver :firefox)) (def edge + "Launches Edge driver. A shortcut for `boot-driver`." (partial boot-driver :edge)) (def chrome @@ -3028,6 +3034,13 @@ ([opt] (boot-driver :firefox (assoc opt :headless true)))) +(defn edge-headless + "Launches headless Edge driver. A shortcut for `boot-driver`." + ([] + (edge-headless {})) + ([opt] + (boot-driver :edge (assoc opt :headless true)))) + (defmacro with-driver "Performs the body within a driver session. @@ -3049,7 +3062,7 @@ (with-driver :firefox {} driver (go driver \"http://example.com\")) -" + " [type opt bind & body] `(client/with-pool {} (let [~bind (boot-driver ~type ~opt)] @@ -3106,3 +3119,10 @@ [opt bind & body] `(with-driver :firefox (assoc ~opt :headless true) ~bind ~@body)) + +(defmacro with-edge-headless + "Performs the body with headless Edge session. A shortcut for + `with-driver`." + [opt bind & body] + `(with-driver :edge (assoc ~opt :headless true) ~bind + ~@body)) diff --git a/src/etaoin/driver.clj b/src/etaoin/driver.clj index 03feaa40..412f48f8 100644 --- a/src/etaoin/driver.clj +++ b/src/etaoin/driver.clj @@ -28,6 +28,9 @@ Safari endpoints https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/WebDriverEndpointDoc/Commands/Commands.html + Edge capabilities and endpoints + https://docs.microsoft.com/en-us/microsoft-edge/webdriver + JSON Wire protocol (obsolete) https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol @@ -120,7 +123,12 @@ (defmethod options-name :safari [driver] - :safariOptions) ;; todo check + :safariOptions) + +(defmethod options-name + :edge + [driver] + :edgeOptions) (defmethod options-name :opera @@ -232,7 +240,7 @@ driver) (defmethods set-headless - [:chrome :firefox] + [:edge :chrome :firefox] [driver] (-> driver (assoc :headless true) diff --git a/test/etaoin/api_test.clj b/test/etaoin/api_test.clj index c6375297..ea82841a 100644 --- a/test/etaoin/api_test.clj +++ b/test/etaoin/api_test.clj @@ -42,7 +42,8 @@ (def default-opts {:chrome {:args ["--headless" "--no-sandbox"]} :firefox {:args ["--headless"]} - :safari {:path-driver "/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver"}}) + :safari {:path-driver "/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver"} + :edge {:args ["--headless"]}}) (def drivers (or (get-drivers-from-env) @@ -333,14 +334,15 @@ ;; monitor, the next two test will fail due to window error. (deftest test-window-position - (when-not-phantom *driver* - (let [{:keys [x y]} (get-window-position *driver*)] - (is (numeric? x)) - (is (numeric? y)) - (set-window-position *driver* (+ x 10) (+ y 10)) - (let [{x' :x y' :y} (get-window-position *driver*)] - (is (not= x x')) - (is (not= y y')))))) + (when-not-drivers + [:phantom :edge] *driver* + (let [{:keys [x y]} (get-window-position *driver*)] + (is (numeric? x)) + (is (numeric? y)) + (set-window-position *driver* (+ x 10) (+ y 10)) + (let [{x' :x y' :y} (get-window-position *driver*)] + (is (not= x x')) + (is (not= y y')))))) (deftest test-window-size (testing "getting size" @@ -353,17 +355,16 @@ (is (not= height height')))))) (deftest test-maximize - (when-not-drivers - [:chrome :firefox :phantom] *driver* - (let [{:keys [x y]} (get-window-position *driver*) - {:keys [width height]} (get-window-size *driver*)] - (maximize *driver*) - (let [{x' :x y' :y} (get-window-position *driver*) - {width' :width height' :height} (get-window-size *driver*)] - (is (not= x x')) - (is (not= y y')) - (is (not= width width')) - (is (not= height height')))))) + (when-not-headless *driver* + (let [{:keys [x y]} (get-window-position *driver*) + {:keys [width height]} (get-window-size *driver*)] + (maximize *driver*) + (let [{x' :x y' :y} (get-window-position *driver*) + {width' :width height' :height} (get-window-size *driver*)] + (is (not= x x')) + (is (not= y y')) + (is (not= width width')) + (is (not= height height')))))) (deftest test-active-element (testing "active element" @@ -510,6 +511,7 @@ (let [js-url (-> "js/inject.js" io/resource str)] (testing "adding a script" (add-script *driver* js-url) + (wait 1) (let [result (js-execute *driver* "return injected_func();")] (is (= result "I was injected"))))))