diff --git a/java/src/org/openqa/selenium/chrome/ChromeDriverService.java b/java/src/org/openqa/selenium/chrome/ChromeDriverService.java index 4490db98eb662..78c7763e2ab2c 100644 --- a/java/src/org/openqa/selenium/chrome/ChromeDriverService.java +++ b/java/src/org/openqa/selenium/chrome/ChromeDriverService.java @@ -29,6 +29,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + +import com.google.common.io.ByteStreams; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chromium.ChromiumDriverLogLevel; @@ -287,12 +289,7 @@ public Builder withReadableTimestamp(Boolean readableTimestamp) { @Override protected void loadSystemProperties() { - if (getLogFile() == null) { - String logFilePath = System.getProperty(CHROME_DRIVER_LOG_PROPERTY); - if (logFilePath != null) { - withLogFile(new File(logFilePath)); - } - } + parseLogOutput(CHROME_DRIVER_LOG_PROPERTY); if (disableBuildCheck == null) { this.disableBuildCheck = Boolean.getBoolean(CHROME_DRIVER_DISABLE_BUILD_CHECK); } @@ -322,17 +319,17 @@ protected List createArgs() { List args = new ArrayList<>(); args.add(String.format("--port=%d", getPort())); - // Readable timestamp and append logs only work if a file is specified - // Can only get readable logs via arguments; otherwise send service output as directed + // Readable timestamp and append logs only work if log path is specified in args + // Cannot use logOutput because goog:loggingPrefs requires --log-path get sent if (getLogFile() != null) { args.add(String.format("--log-path=%s", getLogFile().getAbsolutePath())); - if (readableTimestamp != null && readableTimestamp.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(readableTimestamp)) { args.add("--readable-timestamp"); } - if (appendLog != null && appendLog.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(appendLog)) { args.add("--append-log"); } - withLogFile(null); // Do not overwrite in sendOutputTo() + withLogOutput(ByteStreams.nullOutputStream()); // Do not overwrite log file in getLogOutput() } if (logLevel != null) { @@ -341,7 +338,7 @@ protected List createArgs() { if (allowedListIps != null) { args.add(String.format("--allowed-ips=%s", allowedListIps)); } - if (disableBuildCheck != null && disableBuildCheck.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(disableBuildCheck)) { args.add("--disable-build-check"); } @@ -352,10 +349,7 @@ protected List createArgs() { protected ChromeDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - ChromeDriverService service = - new ChromeDriverService(exe, port, timeout, args, environment); - service.sendOutputTo(getLogOutput(CHROME_DRIVER_LOG_PROPERTY)); - return service; + return new ChromeDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); } diff --git a/java/src/org/openqa/selenium/edge/EdgeDriverService.java b/java/src/org/openqa/selenium/edge/EdgeDriverService.java index 601a22afb2be9..ea5742faf6c5b 100644 --- a/java/src/org/openqa/selenium/edge/EdgeDriverService.java +++ b/java/src/org/openqa/selenium/edge/EdgeDriverService.java @@ -30,6 +30,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + +import com.google.common.io.ByteStreams; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.chromium.ChromiumDriverLogLevel; @@ -255,12 +257,7 @@ public Builder withReadableTimestamp(Boolean readableTimestamp) { @Override protected void loadSystemProperties() { - if (getLogFile() == null) { - String logFilePath = System.getProperty(EDGE_DRIVER_LOG_PROPERTY); - if (logFilePath != null) { - withLogFile(new File(logFilePath)); - } - } + parseLogOutput(EDGE_DRIVER_LOG_PROPERTY); if (disableBuildCheck == null) { this.disableBuildCheck = Boolean.getBoolean(EDGE_DRIVER_DISABLE_BUILD_CHECK); } @@ -290,32 +287,32 @@ protected List createArgs() { List args = new ArrayList<>(); args.add(String.format("--port=%d", getPort())); - // Readable timestamp and append logs only work if a file is specified - // Can only get readable logs via arguments; otherwise send service output as directed + // Readable timestamp and append logs only work if log path is specified in args + // Cannot use logOutput because goog:loggingPrefs requires --log-path get sent if (getLogFile() != null) { args.add(String.format("--log-path=%s", getLogFile().getAbsolutePath())); - if (readableTimestamp != null && readableTimestamp.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(readableTimestamp)) { args.add("--readable-timestamp"); } - if (appendLog != null && appendLog.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(appendLog)) { args.add("--append-log"); } - withLogFile(null); // Do not overwrite in sendOutputTo() + withLogOutput(ByteStreams.nullOutputStream()); // Do not overwrite log file in getLogOutput() } if (logLevel != null) { args.add(String.format("--log-level=%s", logLevel.toString().toUpperCase())); } - if (silent != null && silent.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(silent)) { args.add("--silent"); } - if (verbose != null && verbose.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(verbose)) { args.add("--verbose"); } if (allowedListIps != null) { args.add(String.format("--allowed-ips=%s", allowedListIps)); } - if (disableBuildCheck != null && disableBuildCheck.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(disableBuildCheck)) { args.add("--disable-build-check"); } @@ -326,9 +323,7 @@ protected List createArgs() { protected EdgeDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - EdgeDriverService service = new EdgeDriverService(exe, port, timeout, args, environment); - service.sendOutputTo(getLogOutput(EDGE_DRIVER_LOG_PROPERTY)); - return service; + return new EdgeDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); } diff --git a/java/src/org/openqa/selenium/firefox/GeckoDriverService.java b/java/src/org/openqa/selenium/firefox/GeckoDriverService.java index 0022be7c992b8..99378a3ef434e 100644 --- a/java/src/org/openqa/selenium/firefox/GeckoDriverService.java +++ b/java/src/org/openqa/selenium/firefox/GeckoDriverService.java @@ -245,6 +245,7 @@ public GeckoDriverService.Builder withProfileRoot(File root) { @Override protected void loadSystemProperties() { + parseLogOutput(GECKO_DRIVER_LOG_PROPERTY); if (logLevel == null) { String logFilePath = System.getProperty(GECKO_DRIVER_LOG_LEVEL_PROPERTY); if (logFilePath != null) { @@ -304,9 +305,7 @@ protected List createArgs() { protected GeckoDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - GeckoDriverService service = new GeckoDriverService(exe, port, timeout, args, environment); - service.sendOutputTo(getLogOutput(GECKO_DRIVER_LOG_PROPERTY)); - return service; + return new GeckoDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); } diff --git a/java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java b/java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java index 555122530db3a..b69a0f4977111 100644 --- a/java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java +++ b/java/src/org/openqa/selenium/ie/InternetExplorerDriverService.java @@ -187,12 +187,7 @@ public Builder withSilent(Boolean silent) { @Override protected void loadSystemProperties() { - if (getLogFile() == null) { - String logFilePath = System.getProperty(IE_DRIVER_LOGFILE_PROPERTY); - if (logFilePath != null) { - withLogFile(new File(logFilePath)); - } - } + parseLogOutput(IE_DRIVER_LOGFILE_PROPERTY); if (logLevel == null) { String level = System.getProperty(IE_DRIVER_LOGLEVEL_PROPERTY); if (level != null) { @@ -233,7 +228,7 @@ protected List createArgs() { if (extractPath != null) { args.add(String.format("--extract-path=\"%s\"", extractPath.getAbsolutePath())); } - if (silent != null && silent.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(silent)) { args.add("--silent"); } @@ -244,10 +239,7 @@ protected List createArgs() { protected InternetExplorerDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - InternetExplorerDriverService service = - new InternetExplorerDriverService(exe, port, timeout, args, environment); - service.sendOutputTo(getLogOutput(IE_DRIVER_LOGFILE_PROPERTY)); - return service; + return new InternetExplorerDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); } diff --git a/java/src/org/openqa/selenium/remote/service/DriverService.java b/java/src/org/openqa/selenium/remote/service/DriverService.java index f10c3dd71c0dc..138619a2c4879 100644 --- a/java/src/org/openqa/selenium/remote/service/DriverService.java +++ b/java/src/org/openqa/selenium/remote/service/DriverService.java @@ -437,40 +437,36 @@ protected Duration getDefaultTimeout() { return DEFAULT_TIMEOUT; } - protected OutputStream getLogOutput(String logProperty) { - if (logOutputStream != null) { - return logOutputStream; - } - + protected OutputStream getLogOutput() { try { - File logFileLocation = getLogFile(); - String logLocation; - - if (logFileLocation == null) { - logLocation = System.getProperty(logProperty); - } else { - logLocation = logFileLocation.getAbsolutePath(); - } - - if (logLocation == null) { - return ByteStreams.nullOutputStream(); - } - - switch (logLocation) { - case LOG_STDOUT: - return System.out; - case LOG_STDERR: - return System.err; - case LOG_NULL: - return ByteStreams.nullOutputStream(); - default: - return new FileOutputStream(logLocation); - } + return logOutputStream != null ? logOutputStream : new FileOutputStream(logFile); } catch (FileNotFoundException e) { throw new RuntimeException(e); } } + protected void parseLogOutput(String logProperty) { + if (getLogFile() != null) { + return; + } + + String logLocation = System.getProperty(logProperty, LOG_NULL); + switch (logLocation) { + case LOG_STDOUT: + withLogOutput(System.out); + break; + case LOG_STDERR: + withLogOutput(System.err); + break; + case LOG_NULL: + withLogOutput(ByteStreams.nullOutputStream()); + break; + default: + withLogFile(new File(logLocation)); + break; + } + } + /** * Creates a new service to manage the driver server. Before creating a new service, the builder * will find a port for the server to listen to. @@ -490,6 +486,8 @@ public DS build() { List args = createArgs(); DS service = createDriverService(exe, port, timeout, args, environment); + service.sendOutputTo(getLogOutput()); + port = 0; // reset port to allow reusing this builder return service; diff --git a/java/src/org/openqa/selenium/safari/SafariDriverService.java b/java/src/org/openqa/selenium/safari/SafariDriverService.java index b06ab082cfa65..26367c77ae4f1 100644 --- a/java/src/org/openqa/selenium/safari/SafariDriverService.java +++ b/java/src/org/openqa/selenium/safari/SafariDriverService.java @@ -31,6 +31,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + +import com.google.common.io.ByteStreams; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.net.PortProber; @@ -168,7 +170,7 @@ protected void loadSystemProperties() { @Override protected List createArgs() { List args = new ArrayList<>(Arrays.asList("--port", String.valueOf(getPort()))); - if (diagnose != null && diagnose.equals(Boolean.TRUE)) { + if (Boolean.TRUE.equals(diagnose)) { args.add("--diagnose"); } return args; @@ -178,6 +180,7 @@ protected List createArgs() { protected SafariDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { + withLogOutput(ByteStreams.nullOutputStream()); return new SafariDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); diff --git a/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java b/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java index c14336b3852ee..55b483c39d548 100644 --- a/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java +++ b/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java @@ -31,6 +31,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; + +import com.google.common.io.ByteStreams; import org.openqa.selenium.Capabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.net.PortProber; @@ -172,7 +174,7 @@ protected void loadSystemProperties() { @Override protected List createArgs() { List args = new ArrayList<>(Arrays.asList("--port", String.valueOf(getPort()))); - if (this.diagnose) { + if (Boolean.TRUE.equals(diagnose)) { args.add("--diagnose"); } return args; @@ -182,6 +184,7 @@ protected List createArgs() { protected SafariTechPreviewDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { + withLogOutput(ByteStreams.nullOutputStream()); return new SafariTechPreviewDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e);