add screenshot, wait for load, ability to run JS, add extra time in bonus so the twitter login page is loaded

This commit is contained in:
Mylloon 2023-05-13 17:10:19 +02:00
parent a10ca91700
commit b1989bd79b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 39 additions and 10 deletions

View file

@ -14,8 +14,9 @@ let stop (driver_process, session_id) =
;; ;;
let main session_id = let main session_id =
let body = navigate "https://twitter.com/i/flow/login" session_id in ignore (navigate "https://twitter.com/i/flow/login" session_id);
print_endline body (* Extra wait to be sure the page is loaded *)
Unix.sleep 5
;; ;;
let () = let () =

View file

@ -19,3 +19,10 @@ let navigate_payload url = fmt {|
"url": "%s" "url": "%s"
} }
|} url |} url
let execute_payload src = fmt {|
{
"script": "%s",
"args": []
}
|} src

View file

@ -2,6 +2,7 @@ open Cohttp_lwt_unix
open Utils open Utils
let driver_url = "http://127.0.0.1:4444/session" let driver_url = "http://127.0.0.1:4444/session"
let driver id = fmt "%s/%s" driver_url id
let send_post_request url json = let send_post_request url json =
let headers = Cohttp.Header.init_with "Content-Type" "application/json" in let headers = Cohttp.Header.init_with "Content-Type" "application/json" in
@ -42,14 +43,34 @@ let get_session () =
| _ -> failwith "Invalid JSON" | _ -> failwith "Invalid JSON"
;; ;;
let close_session id = let close_session id = execute_delete_request (driver id) = "{\"value\":null}"
execute_delete_request (fmt "%s/%s" driver_url id) = "{\"value\":null}"
let execute_sync session_id src =
execute_post_request
(fmt "%s/execute/sync" (driver session_id))
(Json.execute_payload src)
;; ;;
let navigate url session_id = let rec wait_for_load session_id =
ignore let response = execute_sync session_id "return document.readyState" in
(execute_post_request match Yojson.Safe.from_string response with
(fmt "%s/%s/url" driver_url session_id) | `Assoc fields ->
(Json.navigate_payload url)); (match List.assoc "value" fields with
execute_get_request (fmt "%s/%s/title" driver_url session_id) | `String res ->
if not (res = "complete")
then (
Unix.sleep 1;
wait_for_load session_id)
| _ -> failwith "Error when waiting for load")
| _ -> failwith "Invalid JSON"
;; ;;
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))