Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log level mapping for ChromeDriver #8242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/chrome/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ java_export(
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
"//java/client/src/org/openqa/selenium/remote/http",
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public ChromeDriver(Capabilities capabilities) {
* @see #ChromeDriver(ChromeDriverService, ChromeOptions)
*/
public ChromeDriver(ChromeOptions options) {
this(ChromeDriverService.createDefaultService(), options);
this(ChromeDriverService.createServiceWithConfig(options), options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.openqa.selenium.chrome;

import com.google.common.collect.ImmutableMap;

import java.util.Map;
import java.util.logging.Level;

/**
* <a href="https://source.chromium.org/chromium/chromium/src/+/master:chrome/test/chromedriver/logging.cc">
* Log levels</a> defined by ChromeDriver
*/
public enum ChromeDriverLogLevel {
ALL,
INFO,
DEBUG,
WARNING,
SEVERE,
OFF;

private static final Map<Level, ChromeDriverLogLevel> logLevelToChromeLevelMap
= new ImmutableMap.Builder<Level, ChromeDriverLogLevel>()
.put(Level.ALL, ALL)
.put(Level.FINEST, DEBUG)
.put(Level.FINER, DEBUG)
.put(Level.FINE, DEBUG)
.put(Level.INFO, INFO)
.put(Level.WARNING, WARNING)
.put(Level.SEVERE, SEVERE)
.put(Level.OFF, OFF)
.build();

@Override
public String toString() {
return super.toString().toLowerCase();
}

public static ChromeDriverLogLevel fromString(String text) {
if (text != null) {
for (ChromeDriverLogLevel b : ChromeDriverLogLevel.values()) {
if (text.equalsIgnoreCase(b.toString())) {
return b;
}
}
}
return null;
}

public static ChromeDriverLogLevel fromLevel(Level level) {
return logLevelToChromeLevelMap.getOrDefault(level, ALL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@
import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.service.DriverService;

/**
* Manages the life and death of a ChromeDriver server.
Expand Down Expand Up @@ -121,6 +119,20 @@ public static ChromeDriverService createDefaultService() {
return new Builder().build();
}

/**
* Configures and returns a new {@link ChromeDriverService} using the supplied configuration. In
* this configuration, the service will use the chromedriver executable identified by the
* {@link #CHROME_DRIVER_EXE_PROPERTY} system property. Each service created by this method will
* be configured to use a free port on the current system.
*
* @return A new ChromeDriverService using the supplied configuration from {@link ChromeOptions}.
*/
public static ChromeDriverService createServiceWithConfig(ChromeOptions options) {
return new Builder()
.withLogLevel(options.getLogLevel())
.build();
}

/**
* Builder used to configure new {@link ChromeDriverService} instances.
*/
Expand All @@ -132,6 +144,7 @@ public static class Builder extends DriverService.Builder<
private boolean verbose = Boolean.getBoolean(CHROME_DRIVER_VERBOSE_LOG_PROPERTY);
private boolean silent = Boolean.getBoolean(CHROME_DRIVER_SILENT_OUTPUT_PROPERTY);
private String whitelistedIps = System.getProperty(CHROME_DRIVER_WHITELISTED_IPS_PROPERTY);
private ChromeDriverLogLevel logLevel = null;

@Override
public int score(Capabilities capabilities) {
Expand Down Expand Up @@ -170,6 +183,17 @@ public Builder withVerbose(boolean verbose) {
return this;
}

/**
* Configures the driver server verbosity.
*
* @param logLevel {@link ChromeDriverLogLevel} for desired log level output.
* @return A self reference.
*/
public Builder withLogLevel(ChromeDriverLogLevel logLevel) {
this.logLevel = logLevel;
return this;
}

/**
* Configures the driver server for silent output.
*
Expand Down Expand Up @@ -210,6 +234,14 @@ protected List<String> createArgs() {
}
}

if (logLevel != null) {
withLogLevel(logLevel);
withVerbose(false);
}
if (verbose) {
withLogLevel(ChromeDriverLogLevel.ALL);
}

List<String> args = new ArrayList<>();

args.add(String.format("--port=%d", getPort()));
Expand All @@ -219,8 +251,8 @@ protected List<String> createArgs() {
if (appendLog) {
args.add("--append-log");
}
if (verbose) {
args.add("--verbose");
if (logLevel != null) {
args.add(String.format("--log-level=%s", logLevel.toString().toUpperCase()));
}
if (silent) {
args.add("--silent");
Expand Down
12 changes: 12 additions & 0 deletions java/client/src/org/openqa/selenium/chrome/ChromeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.chrome;

import java.util.Objects;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.remote.BrowserType;
Expand Down Expand Up @@ -49,9 +50,20 @@ public class ChromeOptions extends ChromiumOptions<ChromeOptions> {
* object.
*/
public static final String CAPABILITY = "goog:chromeOptions";
private ChromeDriverLogLevel logLevel;

public ChromeOptions() {

super(CapabilityType.BROWSER_NAME, BrowserType.CHROME, CAPABILITY);
}

public ChromeOptions setLogLevel(ChromeDriverLogLevel logLevel){
this.logLevel = Objects.requireNonNull(logLevel, "Log level must be set");
return this;
}

public ChromeDriverLogLevel getLogLevel(){
return logLevel;
}

}
12 changes: 10 additions & 2 deletions java/client/test/org/openqa/selenium/chrome/ChromeOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.openqa.selenium.chrome;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import org.junit.Test;
import static org.openqa.selenium.chrome.ChromeDriverLogLevel.OFF;
import static org.openqa.selenium.chrome.ChromeDriverLogLevel.SEVERE;

import java.util.List;
import java.util.Map;
import org.junit.Test;

public class ChromeOptionsTest {

Expand All @@ -45,5 +47,11 @@ public void optionsAsMapShouldBeImmutable() {
.isThrownBy(() -> args.add("-help"));
}

@Test
public void canBuildLogLevelFromStringRepresentation() {
assertThat(ChromeDriverLogLevel.fromString("off")).isEqualTo(OFF);
assertThat(ChromeDriverLogLevel.fromString("SEVERE")).isEqualTo(SEVERE);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverLogLevel;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DriverCommand;
Expand Down Expand Up @@ -48,10 +49,11 @@ private static ChromeDriverService getService() {
try {
Path logFile = Files.createTempFile("chromedriver", ".log");
ChromeDriverService service = new ChromeDriverService.Builder()
.withVerbose(true)
.withLogLevel(ChromeDriverLogLevel.ALL)
.withLogFile(logFile.toFile())
.build();
LOG.info("chromedriver will log to " + logFile);
LOG.info("chromedriver will use log level " + ChromeDriverLogLevel.ALL.toString().toUpperCase());
service.start();
// Fugly.
Runtime.getRuntime().addShutdownHook(new Thread(service::stop));
Expand Down