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 22:13:26 +02:00
|
|
|
let get_session ?(headless = true) () =
|
|
|
|
let response =
|
|
|
|
execute_post_request (fmt "%s" driver_url) (Json.connection_payload headless)
|
|
|
|
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
|
2024-03-09 14:49:13 +01:00
|
|
|
| ("error", `String err) :: ("message", `String msg) :: _ ->
|
|
|
|
raise (Any (Printf.sprintf "%s - %s" err msg))
|
2023-05-13 13:33:46 +02:00
|
|
|
| _ :: rest -> find_session_id rest
|
2023-05-13 18:02:46 +02:00
|
|
|
| [] -> raise (Any "Session ID not found")
|
2023-05-13 13:33:46 +02:00
|
|
|
in
|
|
|
|
find_session_id (Yojson.Safe.Util.to_assoc value)
|
2023-05-13 18:02:46 +02:00
|
|
|
| _ -> raise (Any "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-14 22:52:36 +02:00
|
|
|
type strategy =
|
|
|
|
| XPath of string
|
|
|
|
| CSS of string
|
2023-05-13 20:31:46 +02:00
|
|
|
|
|
|
|
let get_strategy = function
|
|
|
|
| XPath xpath -> "xpath", xpath
|
2023-05-14 22:52:36 +02:00
|
|
|
| CSS css -> "css selector", css
|
2023-05-13 20:31:46 +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
|
2024-01-26 15:31:24 +01:00
|
|
|
| `String res ->
|
|
|
|
if not (res = "complete")
|
|
|
|
then (
|
|
|
|
Unix.sleep 2;
|
|
|
|
wait_for_load session_id)
|
|
|
|
| _ -> raise (Any "Error when waiting for page to load"))
|
2023-05-13 18:02:46 +02:00
|
|
|
| _ -> raise (Any "wait_for_load | Invalid JSON")
|
2023-05-13 13:33:46 +02:00
|
|
|
;;
|
2023-05-13 17:10:19 +02:00
|
|
|
|
2023-05-13 20:31:46 +02:00
|
|
|
let navigate ?(wait = true) session_id url =
|
2023-05-13 17:10:19 +02:00
|
|
|
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))
|
2023-05-13 20:31:46 +02:00
|
|
|
|
2023-05-15 14:29:29 +02:00
|
|
|
let parser response =
|
2023-05-13 20:31:46 +02:00
|
|
|
match Yojson.Safe.from_string response with
|
|
|
|
| `Assoc fields ->
|
|
|
|
(match List.assoc "value" fields with
|
2024-01-26 15:31:24 +01:00
|
|
|
| `List l ->
|
|
|
|
List.fold_left
|
|
|
|
(fun acc x ->
|
|
|
|
match x with
|
|
|
|
| `Assoc subfields ->
|
|
|
|
List.fold_left
|
|
|
|
(fun acc' (_, value) ->
|
|
|
|
match value with
|
|
|
|
| `String str -> str :: acc'
|
|
|
|
| _ -> acc')
|
|
|
|
acc
|
|
|
|
subfields
|
|
|
|
| _ -> acc)
|
|
|
|
[]
|
|
|
|
l
|
|
|
|
| _ -> [])
|
2023-05-15 14:29:29 +02:00
|
|
|
| _ -> raise (Any "finder parser | Invalid JSON")
|
|
|
|
;;
|
|
|
|
|
|
|
|
let find session_id strategy =
|
|
|
|
let engine, search = get_strategy strategy in
|
|
|
|
let response =
|
|
|
|
execute_post_request
|
|
|
|
(fmt "%s/elements" (driver session_id))
|
|
|
|
(Json.find_payload engine search)
|
|
|
|
in
|
|
|
|
parser response
|
|
|
|
;;
|
|
|
|
|
|
|
|
let find_in_element session_id strategy element =
|
|
|
|
let engine, search = get_strategy strategy in
|
|
|
|
let response =
|
|
|
|
execute_post_request
|
|
|
|
(fmt "%s/element/%s/elements" (driver session_id) element)
|
|
|
|
(Json.find_payload engine search)
|
|
|
|
in
|
|
|
|
parser response
|
2023-05-13 20:31:46 +02:00
|
|
|
;;
|
2023-05-13 22:13:26 +02:00
|
|
|
|
2023-05-15 11:44:21 +02:00
|
|
|
let send_keys session_id element_id data =
|
2023-05-14 01:44:11 +02:00
|
|
|
ignore
|
|
|
|
(execute_post_request
|
|
|
|
(fmt "%s/element/%s/value" (driver session_id) element_id)
|
2023-05-15 11:44:21 +02:00
|
|
|
(Json.send_keys_payload data))
|
|
|
|
;;
|
|
|
|
|
|
|
|
let click session_id element_id =
|
|
|
|
ignore
|
|
|
|
(execute_post_request
|
|
|
|
(fmt "%s/element/%s/click" (driver session_id) element_id)
|
|
|
|
Json.empty)
|
2023-05-13 22:13:26 +02:00
|
|
|
;;
|
2023-05-15 12:11:59 +02:00
|
|
|
|
2023-05-15 14:29:29 +02:00
|
|
|
let get_attribute session_id element_id attribute =
|
2023-05-15 12:11:59 +02:00
|
|
|
let response =
|
2023-05-15 14:29:29 +02:00
|
|
|
execute_get_request
|
|
|
|
(fmt "%s/element/%s/attribute/%s" (driver session_id) element_id attribute)
|
2023-05-15 12:11:59 +02:00
|
|
|
in
|
|
|
|
match Yojson.Safe.from_string response with
|
|
|
|
| `Assoc fields ->
|
|
|
|
(match List.assoc "value" fields with
|
2024-01-26 15:31:24 +01:00
|
|
|
| `String href -> href
|
|
|
|
| _ as e ->
|
|
|
|
raise (Any (fmt "Unexpected response from driver: %s" (Yojson.Safe.to_string e))))
|
2023-05-15 14:29:29 +02:00
|
|
|
| _ -> raise (Any "get_attribute | Invalid JSON")
|
2023-05-15 12:11:59 +02:00
|
|
|
;;
|
2023-05-15 12:48:38 +02:00
|
|
|
|
|
|
|
let refresh_page ?(wait = true) session_id =
|
|
|
|
ignore (execute_post_request (fmt "%s/refresh" (driver session_id)) Json.empty);
|
|
|
|
if wait then wait_for_load session_id
|
|
|
|
;;
|