2023-05-13 10:15:35 +02:00
|
|
|
open Cohttp_lwt_unix
|
2023-05-13 12:37:12 +02:00
|
|
|
open Utils
|
|
|
|
|
2023-05-13 15:57:03 +02:00
|
|
|
let driver_url = "http://127.0.0.1:4444/session"
|
2023-05-13 17:10:19 +02:00
|
|
|
let driver id = fmt "%s/%s" driver_url id
|
2023-05-13 10:15:35 +02:00
|
|
|
|
|
|
|
let send_post_request url json =
|
|
|
|
let headers = Cohttp.Header.init_with "Content-Type" "application/json" in
|
|
|
|
let body = Cohttp_lwt.Body.of_string json in
|
2023-05-13 15:57:03 +02:00
|
|
|
Lwt.bind
|
|
|
|
(Client.post ~headers ~body (Uri.of_string url))
|
|
|
|
(fun (_response, body) -> Cohttp_lwt.Body.to_string body)
|
2023-05-13 10:40:13 +02:00
|
|
|
;;
|
|
|
|
|
2023-05-13 16:17:18 +02:00
|
|
|
let send_get_request url =
|
|
|
|
Lwt.bind
|
|
|
|
(Client.get (Uri.of_string url))
|
|
|
|
(fun (_response, body) -> Cohttp_lwt.Body.to_string body)
|
|
|
|
;;
|
|
|
|
|
2023-05-13 12:37:12 +02:00
|
|
|
let send_delete_request url =
|
2023-05-13 15:57:03 +02:00
|
|
|
Lwt.bind
|
|
|
|
(Client.delete (Uri.of_string url))
|
|
|
|
(fun (_response, body) -> Cohttp_lwt.Body.to_string body)
|
2023-05-13 12:37:12 +02:00
|
|
|
;;
|
|
|
|
|
2023-05-13 16:17:18 +02:00
|
|
|
let execute_post_request url json = Lwt_main.run (send_post_request url json)
|
|
|
|
let execute_get_request url = Lwt_main.run (send_get_request url)
|
|
|
|
let execute_delete_request url = Lwt_main.run (send_delete_request url)
|
2023-05-13 12:37:12 +02:00
|
|
|
|
2023-05-13 15:57:03 +02:00
|
|
|
(* Server MUST be started already *)
|
2023-05-13 13:06:25 +02:00
|
|
|
let get_session () =
|
2023-05-13 16:17:18 +02:00
|
|
|
let response = execute_post_request (fmt "%s" driver_url) Json.connection_payload in
|
2023-05-13 13:33:46 +02:00
|
|
|
match Yojson.Safe.from_string response with
|
|
|
|
| `Assoc fields ->
|
|
|
|
let value = List.assoc "value" fields in
|
|
|
|
let rec find_session_id = function
|
|
|
|
| ("sessionId", `String session_id) :: _ -> session_id
|
|
|
|
| _ :: rest -> find_session_id rest
|
|
|
|
| [] -> failwith "Session ID not found"
|
|
|
|
in
|
|
|
|
find_session_id (Yojson.Safe.Util.to_assoc value)
|
2023-05-13 17:21:05 +02:00
|
|
|
| _ -> failwith "get_session | Invalid JSON"
|
2023-05-13 12:37:12 +02:00
|
|
|
;;
|
|
|
|
|
2023-05-13 17:10:19 +02:00
|
|
|
let close_session id = execute_delete_request (driver id) = "{\"value\":null}"
|
|
|
|
|
|
|
|
let execute_sync session_id src =
|
|
|
|
execute_post_request
|
|
|
|
(fmt "%s/execute/sync" (driver session_id))
|
|
|
|
(Json.execute_payload src)
|
2023-05-13 15:57:03 +02:00
|
|
|
;;
|
|
|
|
|
2023-05-13 17:10:19 +02:00
|
|
|
let rec wait_for_load session_id =
|
|
|
|
let response = execute_sync session_id "return document.readyState" in
|
|
|
|
match Yojson.Safe.from_string response with
|
|
|
|
| `Assoc fields ->
|
|
|
|
(match List.assoc "value" fields with
|
|
|
|
| `String res ->
|
|
|
|
if not (res = "complete")
|
|
|
|
then (
|
|
|
|
Unix.sleep 1;
|
|
|
|
wait_for_load session_id)
|
2023-05-13 17:21:05 +02:00
|
|
|
| _ -> failwith "Error when waiting for page to load")
|
|
|
|
| _ -> failwith "wait_for_load | Invalid JSON"
|
2023-05-13 13:33:46 +02:00
|
|
|
;;
|
2023-05-13 17:10:19 +02:00
|
|
|
|
|
|
|
let navigate ?(wait = true) url session_id =
|
|
|
|
let res =
|
|
|
|
execute_post_request (fmt "%s/url" (driver session_id)) (Json.navigate_payload url)
|
|
|
|
in
|
|
|
|
if wait then wait_for_load session_id;
|
|
|
|
res
|
|
|
|
;;
|
|
|
|
|
|
|
|
let screenshot session_id = execute_get_request (fmt "%s/screenshot" (driver session_id))
|