forked from semperos/clj-webdriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wire.clj
48 lines (41 loc) · 1.63 KB
/
wire.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
;; JsonWireProtocol work
(ns clj-webdriver.wire
(:use [cheshire.core :only [parse-string]]
[clojure.walk :only [keywordize-keys]])
(:require [clj-http.client :as client]
[clj-webdriver.remote.server :as rs]
[clojure.string :as string])
(:import clj_webdriver.remote.server.RemoteServer))
(def default-wd-url "http://localhost:3001/wd/")
(defn parse-body
"Body comes back as JSON per protocol. Parse it."
[resp]
(parse-string (:body resp)))
(defprotocol IWire
"JsonWireProtocol support"
(execute [server commands])
(status [server]))
(extend-type RemoteServer
IWire
(execute [server commands]
(let [commands (if-not (vector? commands)
(vector commands)
commands)
resp (client/get (str (rs/address server)
(string/join "/" commands)))
body (parse-body resp)]
(keywordize-keys (assoc resp :body body))))
(status [server]
(execute server ["status"])))
(comment
;; The following shows an example of using the JsonWireProtocol directly,
;; as well as a "traditional" example using the clj-webdriver core API.
;;
;; The following would execute /session/:sessionId/url to get the
;; current URL of the driver. The response is a JSON object
;; that conforms to the JsonWireProtocol spec detailed here:
;; http://code.google.com/p/selenium/wiki/JsonWireProtocol
(let [[server driver] (rs/new-remote-session)
session-id (clj-webdriver.remote-driver/session-id driver)
url (execute server ["session" session-id "url"])]
(clj-webdriver.core/to driver "https://github.com")))