Use Chrome Headless from Selenium / Java

Sumimasen late

This is the 7th day article of Chromium Browser Advent Calendar 2017.

background

As I reconfigured my E2E test, I was curious about how to use the latest Chrome Headless in a Java environment.

The environment is assumed to be Mac, but fortunately Linux should work fine.

The article uses the latest version based on 2017/12. The latest of today's base

is.

The bottom line is that the Java version of ChromeWebDriver is no longer supported, at least at this point, and with the appropriate Selenide wrapper for each language, you could skip the chromedriver download step.

1. Use Selenide (Selenium Java Wrapper).

Create a build.gradle file like the one below and bring the Selenide (selenide.org) library into a usable state.

build.gradle


apply plugin: 'idea'
apply plugin: 'java'

repositories {
    mavenCentral();
}

sourceCompatibility = targetCompatibility = 1.8

dependencies {
    compile 'com.codeborne:selenide:4.9'
    compile 'org.springframework.boot:spring-boot-starter-logging:1.5.9.RELEASE'
}

configurations.all {
    exclude group: 'log4j'
    exclude module: 'log4j-over-slf4j'
    exclude module: 'slf4j-log4j12'
    exclude module: 'slf4j-jdk14'
    exclude module: 'commons-logging'
}

2. Write the executable code

How to launch Chrome through Selenium, in this case, put it in a static variable before loading the page.

ChromeHeadlessApplication.java


package holdings.ozaki.matsudak;

import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.screenshot;

import org.slf4j.bridge.SLF4JBridgeHandler;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;


public class ChromeHeadlessApplication {
    static {
        //Stream the log towards Slf4j
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();

        //Use ChromeDriver in headless mode
        Configuration.browser = WebDriverRunner.CHROME;
        Configuration.headless = true;
        Configuration.reportsFolder = "build/reports";
        Configuration.browserSize = "1024x768";
    }

    public static void main (final String...args) {
        // google.Take a screenshot of com and finish
        open("https://google.com");
        screenshot("google.com");
    }
}

3. Run

First of all, it is a driver that becomes a bridge for executing chrome, but ʻio.github.bonigarcia.wdm.Downloader` will download it for the following.

log


2017-12-24 20:13:55 [main] INFO  i.g.bonigarcia.wdm.BrowserManager - Reading https://chromedriver.storage.googleapis.com/ to seek [chromedriver]
2017-12-24 20:13:55 [main] INFO  i.g.bonigarcia.wdm.BrowserManager - Latest version of [chromedriver] is 2.34
2017-12-24 20:13:55 [main] DEBUG io.github.bonigarcia.wdm.Downloader - Downloading https://chromedriver.storage.googleapis.com/2.34/chromedriver_mac64.zip to /Users/jp20217/.m2/repository/webdriver/chromedriver/mac64/2.34/chromedriver_mac64.zip
2017-12-24 20:13:56 [main] INFO  i.g.bonigarcia.wdm.BrowserManager - Exporting webdriver.chrome.driver as /Users/matsudak/.m2/repository/webdriver/chromedriver/mac64/2.34/chromedriver

If you do ps -A while executing the main statement, you can see that Chrome is started with --headless & --disable-gpu.

ps -A


50321 ??         0:00.30 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-gpu --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtri
als=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension=/var/folders/wn/s9lbdlj56rd4bl2b6k54v_z80000gp/T/.org.chromium.Chromium.hg9Dpt/internal --log-level=0 --metrics-recording-only --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=12986 --test-type=webdriver --use-mock-keychain --user-data-dir=/var/
folders/wn/s9lbdlj56rd4bl2b6k54v_z80000gp/T/.org.chromium.Chromium.76jzQ6 data:,

result

google.com.png

Summary

Since Chrome Headless cannot be used, PhantomJS, and so on, there are various restrictions, so I think that there are many environments where Chrome was launched via xvid.

From 2017, it seems like you should use Chrome Headless without xvid.

Other information that has been tested with Chrome Headless

Recommended Posts

Use Chrome Headless from Selenium / Java
Use PostgreSQL data type (jsonb) from Java
Use selenium (Chrome) in Ruby in WSL environment
Selenium x Java
[Java] Use Collectors.collectingAndThen
Use native libraries from Scala via Java CPP + Java
Capture and save from selenium installation in Java
Use Matplotlib from Java or Scala with Matplotlib4j
Call Java from JRuby
Access API.AI from Java
Use OpenCV in Java
From Java to Ruby !!
Use TensorFlow from JRuby
JAWJAW is convenient if you use WordNet from Java
Use PreparedStatement in Java
Use Lambda Layers with Java
[Java] How to use Map
Migration from Cobol to JAVA
[Java] How to use Map
Java starting from beginner, override
How to use java Optional
New features from Java7 to Java8
Use Face API from Ruby
How to use java class
[Java] How to use Optional ②
Connect from Java to PostgreSQL
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java Map
Java, instance starting from beginner
[Java] Use Big Decimal properly ~ 2018 ~
[java] Reasons to use static
Java starting from beginner, inheritance
Java life starting from scratch
How to use Java variables
Use Redis Stream in Java
CICS-Run Java applications-(5) Use JCICSX
Using Docker from Java Gradle
Use SpatiaLite with Java / JDBC
[Java] How to use Optional ①
From Ineffective Java to Effective Java
JavaScript as seen from Java
Selenium Sample Reservation Form (Java)
Execute non-Java instructions from Java