selenium is useless, only driver needed - prepare for requesting data from it
This commit is contained in:
parent
03626d1abb
commit
349952d116
4 changed files with 47 additions and 27 deletions
29
bin/main.ml
29
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)
|
||||
;;
|
||||
|
|
|
@ -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)
|
2
lib/dune
2
lib/dune
|
@ -1,4 +1,4 @@
|
|||
(library
|
||||
(name pusk)
|
||||
(modules utils selenium_init)
|
||||
(modules utils drivers net)
|
||||
(libraries cohttp-lwt-unix))
|
||||
|
|
9
lib/net.ml
Normal file
9
lib/net.ml
Normal file
|
@ -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)
|
||||
;;
|
Loading…
Reference in a new issue