Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring remote connection capabilities #303

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))