Skip to content

Commit 72e3977

Browse files
committed
Implementing enum for FirefoxDriver (geckodriver) log levels, it's not wise to use jul levels because geckodriver has its own set of levels.
1 parent 2d3a147 commit 72e3977

File tree

3 files changed

+83
-35
lines changed

3 files changed

+83
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.firefox;
19+
20+
public enum FirefoxDriverLogLevel {
21+
TRACE,
22+
DEBUG ,
23+
CONFIG,
24+
INFO,
25+
WARN,
26+
ERROR,
27+
FATAL;
28+
29+
@Override
30+
public String toString() {
31+
return super.toString().toLowerCase();
32+
}
33+
34+
public static FirefoxDriverLogLevel fromString(String text) {
35+
if (text != null) {
36+
for (FirefoxDriverLogLevel b : FirefoxDriverLogLevel.values()) {
37+
if (text.equalsIgnoreCase(b.toString())) {
38+
return b;
39+
}
40+
}
41+
}
42+
return null;
43+
}
44+
45+
}

java/client/src/org/openqa/selenium/firefox/FirefoxOptions.java

+26-33
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.openqa.selenium.WebDriverException;
4545
import org.openqa.selenium.firefox.internal.ProfilesIni;
4646
import org.openqa.selenium.logging.LoggingPreferences;
47-
import org.openqa.selenium.remote.BrowserType;
4847
import org.openqa.selenium.remote.DesiredCapabilities;
4948

5049
import java.io.File;
@@ -88,10 +87,9 @@ public class FirefoxOptions {
8887
private Map<String, Boolean> booleanPrefs = new HashMap<>();
8988
private Map<String, Integer> intPrefs = new HashMap<>();
9089
private Map<String, String> stringPrefs = new HashMap<>();
91-
private Level logLevel = null;
90+
private FirefoxDriverLogLevel logLevel = null;
9291
private boolean legacy;
9392
private DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
94-
private boolean headless;
9593

9694
private void amend(Map<String, Object> map) throws IOException {
9795
if (map.containsKey("binary")) {
@@ -400,7 +398,31 @@ public FirefoxOptions addPreference(String key, String value) {
400398
return this;
401399
}
402400

401+
private Map<Level, FirefoxDriverLogLevel> logLevelToGeckoLevelMap
402+
= new ImmutableMap.Builder<Level, FirefoxDriverLogLevel>()
403+
.put(Level.ALL, FirefoxDriverLogLevel.TRACE)
404+
.put(Level.FINEST, FirefoxDriverLogLevel.TRACE)
405+
.put(Level.FINER, FirefoxDriverLogLevel.TRACE)
406+
.put(Level.FINE, FirefoxDriverLogLevel.DEBUG)
407+
.put(Level.CONFIG, FirefoxDriverLogLevel.CONFIG)
408+
.put(Level.INFO, FirefoxDriverLogLevel.INFO)
409+
.put(Level.WARNING, FirefoxDriverLogLevel.WARN)
410+
.put(Level.SEVERE, FirefoxDriverLogLevel.ERROR)
411+
.put(Level.OFF, FirefoxDriverLogLevel.FATAL)
412+
.build();
413+
414+
/**
415+
* @deprecated Use {@link #setLogLevel(FirefoxDriverLogLevel)}
416+
*/
417+
@Deprecated
403418
public FirefoxOptions setLogLevel(Level logLevel) {
419+
// levels defined by GeckoDriver
420+
// https://github.com/mozilla/geckodriver#log-object
421+
this.logLevel = logLevelToGeckoLevelMap.getOrDefault(logLevel, FirefoxDriverLogLevel.DEBUG);
422+
return this;
423+
}
424+
425+
public FirefoxOptions setLogLevel(FirefoxDriverLogLevel logLevel) {
404426
this.logLevel = logLevel;
405427
return this;
406428
}
@@ -605,43 +627,14 @@ public Map<String, Object> toJson() throws IOException {
605627
}
606628

607629
if (logLevel != null) {
608-
options.put("log", ImmutableMap.of("level", logLevelToGeckoLevel()));
630+
options.put("log", ImmutableMap.of("level", logLevel));
609631
}
610632

611633
options.put("args", args);
612634

613635
return options.build();
614636
}
615637

616-
private String logLevelToGeckoLevel() {
617-
// levels defined by GeckoDriver
618-
// https://github.com/mozilla/geckodriver#log-object
619-
if (logLevel.intValue() < Level.FINE.intValue()) {
620-
return "trace";
621-
}
622-
if (logLevel == Level.FINE) {
623-
return "debug";
624-
}
625-
if (logLevel == Level.CONFIG) {
626-
return "config";
627-
}
628-
if (logLevel == Level.INFO) {
629-
return "info";
630-
}
631-
if (logLevel == Level.WARNING) {
632-
return "warn";
633-
}
634-
if (logLevel == Level.SEVERE) {
635-
return "error";
636-
}
637-
if (logLevel == Level.OFF) {
638-
return "fatal";
639-
}
640-
641-
// something else? ¯\_(ツ)_/¯
642-
return "debug";
643-
}
644-
645638
@Override
646639
public String toString() {
647640
return "{" +

java/client/test/org/openqa/selenium/firefox/FirefoxOptionsTest.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323
import static org.junit.Assert.assertEquals;
2424
import static org.junit.Assert.assertFalse;
2525
import static org.junit.Assert.assertSame;
26-
import static org.junit.Assert.assertThat;
2726
import static org.junit.Assert.assertTrue;
2827
import static org.junit.Assert.fail;
2928
import static org.junit.Assume.assumeNotNull;
3029
import static org.junit.Assume.assumeThat;
3130
import static org.openqa.selenium.firefox.FirefoxDriver.MARIONETTE;
32-
import static org.openqa.selenium.firefox.FirefoxDriver.PROFILE;
3331
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_BINARY;
3432
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.BROWSER_PROFILE;
3533
import static org.openqa.selenium.firefox.FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE;
@@ -282,4 +280,16 @@ public void callingToStringWhenTheBinaryDoesNotExistShouldNotCauseAnException()
282280
fail(Throwables.getStackTraceAsString(e));
283281
}
284282
}
283+
284+
@Test
285+
public void logLevelStringRepresentationIsLowercase() {
286+
assertEquals(FirefoxDriverLogLevel.DEBUG.toString(), "debug");
287+
}
288+
289+
@Test
290+
public void canBuildLogLevelFromStringRepresentation() {
291+
assertEquals(FirefoxDriverLogLevel.fromString("warn"), FirefoxDriverLogLevel.WARN);
292+
assertEquals(FirefoxDriverLogLevel.fromString("ERROR"), FirefoxDriverLogLevel.ERROR);
293+
}
294+
285295
}

0 commit comments

Comments
 (0)