Sumimasen late
This is the 7th day article of Chromium Browser Advent Calendar 2017.
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.
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'
}
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");
}
}
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:,
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.
Recommended Posts