Skip to content

Commit

Permalink
refactoring remote connection capabilities (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uunnamed authored Aug 21, 2020
1 parent d7a01d5 commit f6e3295
Showing 1 changed file with 5 additions and 151 deletions.
156 changes: 5 additions & 151 deletions src/etaoin/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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))

0 comments on commit f6e3295

Please sign in to comment.