From 6a5f516f91ff33ba1543826af80fa8646aa548b2 Mon Sep 17 00:00:00 2001 From: Uunnamed Date: Fri, 21 Aug 2020 12:37:01 +0300 Subject: [PATCH] refactoring remote connection capabilities --- src/etaoin/api.clj | 156 ++------------------------------------------- 1 file changed, 5 insertions(+), 151 deletions(-) diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index dcf74dfa..868065e9 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -3146,29 +3146,11 @@ `run-driver` and `connect-driver` may accept." ([type] (boot-driver type {})) - ([type opt] - (-> type - (create-driver opt) - (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)))) - + ([type {host :host :as opt}] + (cond-> type + true (create-driver opt) + (not host) (run-driver opt) + true (connect-driver opt)))) (defn quit "Closes the current session and stops the driver." @@ -3305,131 +3287,3 @@ `(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))