Skip to content

Commit

Permalink
Allow global configuration with method globalConfig() (issue #313)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Mar 13, 2019
1 parent 6ddcaa6 commit a841ecd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ public abstract class WebDriverManager {
protected String preferenceKey;
protected Properties versionsProperties;

public static Config globalConfig() {
Config global = new Config();
for (DriverManagerType type : DriverManagerType.values()) {
WebDriverManager.getInstance(type).setConfig(global);
}
return global;
}

public static void resetGlobalConfig() {
for (DriverManagerType type : DriverManagerType.values()) {
WebDriverManager.getInstance(type).reset();
}
}

public Config config() {
return config;
}
Expand Down Expand Up @@ -1263,4 +1277,8 @@ private void storeVersionToDownload(String version) {
}
}

private void setConfig(Config config) {
this.config = config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package io.github.bonigarcia.wdm.test;

import static io.github.bonigarcia.wdm.WebDriverManager.chromedriver;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.file.Files.createTempDirectory;
import static org.apache.commons.io.FileUtils.deleteDirectory;
Expand Down Expand Up @@ -49,20 +48,21 @@ public class CustomTargetTest {
@Before
public void setup() throws IOException {
tmpFolder = createTempDirectory("").toRealPath();
chromedriver().config().setTargetPath(tmpFolder.toString());
WebDriverManager.globalConfig().setTargetPath(tmpFolder.toString());
log.info("Using temporal folder {} as cache", tmpFolder);
}

@Test
public void testTargetPath() {
chromedriver().setup();
WebDriverManager.chromedriver().setup();
String binaryPath = WebDriverManager.chromedriver().getBinaryPath();
log.info("Binary path {}", binaryPath);
assertThat(binaryPath, startsWith(tmpFolder.toString()));
}

@After
public void teardown() throws IOException {
WebDriverManager.resetGlobalConfig();
log.info("Deleting temporal folder {}", tmpFolder);
deleteDirectory(tmpFolder.toFile());
}
Expand Down

1 comment on commit a841ecd

@zhale7
Copy link

@zhale7 zhale7 commented on a841ecd Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, Sir. While I was trying to find my error's reason I came across this commit. In my case, I have IDriver interface, and inside it there is:
WebDriver newDriver() throws DriverNotSupportedException;

default void downloadLocalWebDriver(DriverManagerType driverType) throws DriverNotSupportedException {
    Config wdmConfig = WebDriverManager.globalConfig();
    wdmConfig.setAvoidBrowserDetection(true);

    String browserVersion = System.getProperty("browser.version", "");

    if (!browserVersion.isEmpty()) {
        switch (driverType) {
            case CHROME:
                wdmConfig.setChromeDriverVersion(browserVersion);
                break;
            case EDGE:
                wdmConfig.setEdgeDriverVersion(browserVersion);
            case FIREFOX:
                wdmConfig.setFirefoxVersion(browserVersion);
            default:
                throw new DriverNotSupportedException(driverType.name());
        }
    }

    WebDriverManager.getInstance(driverType).setup();
}

}

But here WebDriverManager.globalConfig() method does not work. What should I do? Can you help please?

Please sign in to comment.