diff --git a/README.md b/README.md index b8745840..82897ae6 100644 --- a/README.md +++ b/README.md @@ -986,6 +986,11 @@ skipped or have nil values. Some of them, if not passed, are taken from the ;; :err (aliases :error, :severe, :crit, :critical), :all. When not passed, ;; :all is set. :log-level :err ;; to show only errors but not debug + + ;; Paths to the driver's log files as strings. + ;; When not set, the output goes to /dev/null (or NUL on Windows) + :log-stdout + :log-stderr ;; Path to a custorm browser profile. See the section below. :profile "/Users/ivan/Library/Application Support/Firefox/Profiles/iy4iitbg.Test" diff --git a/src/etaoin/api.clj b/src/etaoin/api.clj index c1ae7468..678ccde2 100644 --- a/src/etaoin/api.clj +++ b/src/etaoin/api.clj @@ -2895,6 +2895,8 @@ profile headless log-level + log-stdout + log-stderr args-driver path-driver download-dir @@ -2939,7 +2941,8 @@ proc-args (drv/get-args @driver) _ (log/debugf "Starting process: %s" (str/join \space proc-args)) - process (proc/run proc-args)] + process (proc/run proc-args {:log-stdout log-stdout + :log-sttderr log-stderr})] (swap! driver assoc :env env ;; todo process env :process process) diff --git a/src/etaoin/proc.clj b/src/etaoin/proc.clj index e4115a6a..6b510d68 100644 --- a/src/etaoin/proc.clj +++ b/src/etaoin/proc.clj @@ -1,28 +1,44 @@ (ns etaoin.proc - (:require [clojure.java.io :as io]) - (:import java.lang.Runtime - java.lang.IllegalThreadStateException - java.io.IOException)) + (:require [clojure.java.io :as io] + [clojure.string :as str]) + (:import java.lang.IllegalThreadStateException + java.io.IOException)) + +(def windows? (str/starts-with? (System/getProperty "os.name") "Windows")) + +(defn get-null-file ^java.io.File + [] + (if windows? + (io/file "NUL") + (io/file "/dev/null"))) + +(defn get-log-file ^java.io.File + [file-path] + (if file-path + (io/file file-path) + (get-null-file))) (defn java-params ^"[Ljava.lang.String;" [params] (->> params (map str) (into-array String))) -(defn run [args] - (let [binary (first args) - readme-link "https://github.com/igrishaev/etaoin#installing-the-browser-drivers" - pb (java.lang.ProcessBuilder. (java-params args))] - (.redirectOutput pb (java.io.File/createTempFile "driver.out" ".log")) - (.redirectError pb (java.io.File/createTempFile "driver.err" ".log")) - (try - (.start pb) - (catch java.io.IOException e - (throw (ex-info - (format "Cannot find a binary file `%s` for the driver. +(defn run + ([args] (run args {})) + ([args {:keys [log-stdout log-stderr]}] + (let [binary (first args) + readme-link "https://github.com/igrishaev/etaoin#installing-the-browser-drivers" + pb (java.lang.ProcessBuilder. (java-params args))] + (.redirectOutput pb (get-log-file log-stdout)) + (.redirectError pb (get-log-file log-stderr)) + (try + (.start pb) + (catch java.io.IOException e + (throw (ex-info + (format "Cannot find a binary file `%s` for the driver. Please ensure you have the driver installed and specify the path to it. For driver installation, check out the official readme file from Etaoin: %s" binary readme-link) - {:args args} e)))))) + {:args args} e))))))) ;; todo store those streams diff --git a/test/etaoin/api_test2.clj b/test/etaoin/api_test2.clj index a5b26d4b..6027966b 100644 --- a/test/etaoin/api_test2.clj +++ b/test/etaoin/api_test2.clj @@ -5,7 +5,7 @@ (deftest test-firefox-driver-args (let [args (atom [])] - (with-redefs-fn {#'etaoin.proc/run #(reset! args %)} + (with-redefs-fn {#'etaoin.proc/run (fn [a _] (reset! args a))} #(do (testing "No custom args" (-> (create-driver :firefox {:port 1234}) (run-driver {}))