From 349952d116ebad0686b822d70b130a56b6dc347d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 13 May 2023 10:15:35 +0200 Subject: [PATCH] selenium is useless, only driver needed - prepare for requesting data from it --- bin/main.ml | 29 +++++++++++++++++++----- lib/{selenium_init.ml => drivers.ml} | 34 ++++++++++++---------------- lib/dune | 2 +- lib/net.ml | 9 ++++++++ 4 files changed, 47 insertions(+), 27 deletions(-) rename lib/{selenium_init.ml => drivers.ml} (72%) create mode 100644 lib/net.ml diff --git a/bin/main.ml b/bin/main.ml index ece9519..a68fc2e 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,13 +1,30 @@ open Pusk.Utils -open Pusk.Selenium_init +open Pusk.Net +open Pusk.Drivers -let main = () +let main = + let json_payload = + {| + { + "capabilities": { + "alwaysMatch": { + "moz:firefoxOptions": { + "args": ["-headless"] + } + } + } + } + |} + in + let body = send_post_request "http://localhost:4444/session" json_payload in + print_endline (Lwt_main.run body) +;; let () = - let selenium = prepare "4.9.0" (Gecko "0.33.0") in - let selenium_pid = run selenium in - print_endline (fmt "Java running in %d" selenium_pid); + let driver = prepare (Gecko "0.33.0") in + let driver_pid = run driver [] in + print_endline (fmt "Driver running as %d" driver_pid); main; - let closed_pid = close selenium_pid in + let closed_pid = close driver_pid in print_endline (fmt "Program %d closed!" closed_pid) ;; diff --git a/lib/selenium_init.ml b/lib/drivers.ml similarity index 72% rename from lib/selenium_init.ml rename to lib/drivers.ml index f9fc7d1..1919576 100644 --- a/lib/selenium_init.ml +++ b/lib/drivers.ml @@ -15,7 +15,7 @@ let rec download uri dest = let uri = Uri.of_string url in let redirect_url = Uri.resolve "" uri uri in download redirect_url dest - | None -> Lwt.fail_with "Redirect location not found") + | None -> failwith "Redirect location not found") else if Cohttp.Code.is_success code then ( print_endline "Downloading..."; @@ -28,18 +28,10 @@ let rec download uri dest = print_endline "Download done!"; Lwt.return_unit) else - Lwt.fail_with + failwith ("Failed to download file. HTTP status: " ^ Cohttp.Code.string_of_status status) ;; -let download_selenium version = - let url = - format_of_string - "https://github.com/SeleniumHQ/selenium/releases/download/selenium-%s/selenium-server-%s.jar" - in - download (Uri.of_string (fmt url version version)) (fmt "./selenium-%s.jar" version) -;; - let download_gecko_driver version output = let url = format_of_string @@ -63,22 +55,24 @@ let run_program_in_background program args = type driver = Gecko of string -let prepare version_selenium driver = - (* Gecko Driver *) - (match driver with +let prepare = function | Gecko version_driver -> - if not (Sys.file_exists "geckodriver") + let driver = "geckodriver" in + if not (Sys.file_exists driver) then ( let archive = fmt "./gecko-%s.tar.gz" version_driver in Lwt_main.run (download_gecko_driver version_driver archive); (* TODO: Use native version instead of relying on Unix tools *) let _ = Sys.command (fmt "tar xvzf %s" archive) in - ())); - (* Selenium *) - let selenium = fmt "selenium-%s.jar" version_selenium in - if not (Sys.file_exists selenium) then Lwt_main.run (download_selenium version_selenium); - selenium + ()); + driver +;; + +let run path args = + let pid = run_program_in_background path args in + (* Wait so we sure the server is up *) + Unix.sleepf 0.5; + pid ;; -let run path = run_program_in_background "java" [ fmt "-jar %s" path; "standalone" ] let close pid = fst (Unix.waitpid [] pid) diff --git a/lib/dune b/lib/dune index 069dba8..01daeb5 100644 --- a/lib/dune +++ b/lib/dune @@ -1,4 +1,4 @@ (library (name pusk) - (modules utils selenium_init) + (modules utils drivers net) (libraries cohttp-lwt-unix)) diff --git a/lib/net.ml b/lib/net.ml new file mode 100644 index 0000000..7023dc7 --- /dev/null +++ b/lib/net.ml @@ -0,0 +1,9 @@ +open Cohttp_lwt_unix + +let send_post_request url json = + let uri = Uri.of_string url in + let headers = Cohttp.Header.init_with "Content-Type" "application/json" in + let body = Cohttp_lwt.Body.of_string json in + Lwt.bind (Client.post ~headers ~body uri) (fun (_response, body) -> + (* Lwt.map (fun body_str -> response, body_str) *) Cohttp_lwt.Body.to_string body) +;;