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:
parent
a10ca91700
commit
b1989bd79b
3 changed files with 39 additions and 10 deletions
|
@ -14,8 +14,9 @@ let stop (driver_process, session_id) =
|
|||
;;
|
||||
|
||||
let main session_id =
|
||||
let body = navigate "https://twitter.com/i/flow/login" session_id in
|
||||
print_endline body
|
||||
ignore (navigate "https://twitter.com/i/flow/login" session_id);
|
||||
(* Extra wait to be sure the page is loaded *)
|
||||
Unix.sleep 5
|
||||
;;
|
||||
|
||||
let () =
|
||||
|
|
|
@ -19,3 +19,10 @@ let navigate_payload url = fmt {|
|
|||
"url": "%s"
|
||||
}
|
||||
|} url
|
||||
|
||||
let execute_payload src = fmt {|
|
||||
{
|
||||
"script": "%s",
|
||||
"args": []
|
||||
}
|
||||
|} src
|
||||
|
|
37
lib/net.ml
37
lib/net.ml
|
@ -2,6 +2,7 @@ open Cohttp_lwt_unix
|
|||
open Utils
|
||||
|
||||
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 headers = Cohttp.Header.init_with "Content-Type" "application/json" in
|
||||
|
@ -42,14 +43,34 @@ let get_session () =
|
|||
| _ -> failwith "Invalid JSON"
|
||||
;;
|
||||
|
||||
let close_session id =
|
||||
execute_delete_request (fmt "%s/%s" driver_url id) = "{\"value\":null}"
|
||||
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)
|
||||
;;
|
||||
|
||||
let navigate url session_id =
|
||||
ignore
|
||||
(execute_post_request
|
||||
(fmt "%s/%s/url" driver_url session_id)
|
||||
(Json.navigate_payload url));
|
||||
execute_get_request (fmt "%s/%s/title" driver_url session_id)
|
||||
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)
|
||||
| _ -> 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))
|
||||
|
|
Loading…
Reference in a new issue