From d7a01d58f4226c2d32ce3cb49df6206c2be60451 Mon Sep 17 00:00:00 2001 From: "Alex.Shi" Date: Thu, 20 Aug 2020 21:51:22 +0300 Subject: [PATCH] add fns for connecting to remote drivers (#302) --- src/etaoin/api.clj | 147 ++++++++++++++++++++++++++++++++++++++ test/etaoin/api_test2.clj | 2 - 2 files changed, 147 insertions(+), 2 deletions(-) diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index 2a383e36..dcf74dfa 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -3152,6 +3152,24 @@ (run-driver opt) (connect-driver opt)))) +(defn boot-driver-remote + "Two-in-one: creates a driver, and creates a new + session. Returns the driver instance. + + Arguments: + + - `type` a keyword determines a driver type. + + - `opt` a map of all possible parameters that `create-driver`, + `connect-driver` may accept." + ([type] + (boot-driver-remote type {})) + ([type opt] + (-> type + (create-driver opt) + (connect-driver opt)))) + + (defn quit "Closes the current session and stops the driver." [driver] @@ -3286,3 +3304,132 @@ [opt bind & body] `(with-driver :edge (assoc ~opt :headless true) ~bind ~@body)) + +;; remote section + +(def firefox-remote + "Connects to a remote Firefox driver. A shortcut for `boot-driver-remote`." + (partial boot-driver-remote :firefox)) + +(def edge-remote + "Connects to a remote Edge driver. A shortcut for `boot-driver-remote`." + (partial boot-driver-remote :edge)) + +(def chrome-remote + "Connects to a remote Chrome driver. A shortcut for `boot-driver-remote`." + (partial boot-driver-remote :chrome)) + +(def phantom-remote + "Connects to a remote Phantom.js driver. A shortcut for `boot-driver-remote`." + (partial boot-driver-remote :phantom)) + +(def safari-remote + "Connects to a remote Safari driver. A shortcut for `boot-driver-remote`." + (partial boot-driver-remote :safari)) + +(defn chrome-headless-remote + "Connects to a remote headless Chrome driver. A shortcut for `boot-driver-remote`." + ([] + (chrome-headless-remote {})) + ([opt] + (boot-driver-remote :chrome (assoc opt :headless true)))) + +(defn firefox-headless-remote + "Connects to a remote headless Firefox driver. A shortcut for `boot-driver-remote`." + ([] + (firefox-headless-remote {})) + ([opt] + (boot-driver-remote :firefox (assoc opt :headless true)))) + +(defn edge-headless-remote + "Connects to a remote headless Edge driver. A shortcut for `boot-driver-remote`." + ([] + (edge-headless-remote {})) + ([opt] + (boot-driver-remote :edge (assoc opt :headless true)))) + +(defmacro with-driver-remote + "Performs the body within a driver session. + + Connects to remote a driver of a given type. Binds the driver instance to a + passed `bind` symbol. Executes the body once the driver has + connected. Disconnect the driver finally (even if an exception + occurred). + + Arguments: + + - `type` is a keyword what driver type to start. + + - `opt` is a map with driver's options. See `boot-driver-remote` for more + details. + + - `bind` is a symbol to bind a driver reference. + + Example: + + (with-driver-remote :firefox {} driver + (go driver \"http://example.com\")) + " + [type opt bind & body] + `(client/with-pool {} + (let [~bind (boot-driver-remote ~type ~opt)] + (try + ~@body + (finally + (disconnect-driver ~bind)))))) + +(defmacro with-firefox-remote + "Performs the body with Firefox session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :firefox ~opt ~bind + ~@body)) + +(defmacro with-chrome-remote + "Performs the body with Chrome session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :chrome ~opt ~bind + ~@body)) + +(defmacro with-edge-remote + "Performs the body with Edge session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :edge ~opt ~bind + ~@body)) + +(defmacro with-phantom-remote + "Performs the body with Phantom.js session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :phantom ~opt ~bind + ~@body)) + +(defmacro with-safari-remote + "Performs the body with Safari session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :safari ~opt ~bind + ~@body)) + +(defmacro with-chrome-headless-remote + "Performs the body with headless Chrome session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :chrome (assoc ~opt :headless true) ~bind + ~@body)) + +(defmacro with-firefox-headless-remote + "Performs the body with headless Firefox session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :firefox (assoc ~opt :headless true) ~bind + ~@body)) + +(defmacro with-edge-headless-remote + "Performs the body with headless Edge session. A shortcut for + `with-driver-remote`." + [opt bind & body] + `(with-driver-remote :edge (assoc ~opt :headless true) ~bind + ~@body)) diff --git a/test/etaoin/api_test2.clj b/test/etaoin/api_test2.clj index 2e73423a..e260b210 100644 --- a/test/etaoin/api_test2.clj +++ b/test/etaoin/api_test2.clj @@ -31,8 +31,6 @@ (is (= ["geckodriver" "--port" 1234 "--marionette-port" 2821] (:args @driver))))))) - -;; TODO: https://github.com/igrishaev/etaoin/issues/296 (deftest test-chrome-profile (let [profile-path (str (Files/createTempDirectory "chrome-profile"