Skip to content

Open browser in headless mode

Ha Do edited this page Aug 22, 2020 · 1 revision
  public static WebDriver openHeadlessBrowser(String name) {
        switch (name.toLowerCase()) {
            case "chrome":
                ChromeOptions chromeOptions = new ChromeOptions();
                chromeOptions.setHeadless(true);
                return new ChromeDriver(chromeOptions);
            case "firefox":
                FirefoxBinary firefoxBinary = new FirefoxBinary();
                firefoxBinary.addCommandLineOptions("--headless");
                FirefoxOptions firefoxOptions = new FirefoxOptions();
                firefoxOptions.setBinary(firefoxBinary);
                return new FirefoxDriver(firefoxOptions);

            default:
                throw new IllegalArgumentException("The browser " + name + " dose not support!!!");
        }
    }