- 测试运行环境
- 运行环境: jdk 1.8
- 操作系统: centOS 7
- 执行脚本命令测试: java -jar JBrowserDriverExample-v0.1.jar
Example.java
package com.lemonsoft;
import com.machinepublishers.jbrowserdriver.JBrowserDriver;
import com.machinepublishers.jbrowserdriver.Settings;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import java.io.File;
import java.io.IOException;
/**
* Java 集成 无界面浏览器(JBrowserDriver) 在 无界面系统中 实现截图功能
*
* Created by mao_siyu on 2018/1/23.
*/
public class Example {
public static void main(String[] args) throws IOException {
Settings.Builder builder = Settings.builder();
// 这一行很关键,如果不加,在无界面系统中是不能运行的,
// 会提示 Error: JavaFX detected no fonts! Please refer to release notes for proper font configuration
builder.javaOptions("-Dprism.useFontConfig=false");
// builder.headless(true);
// builder.javascript(true);
// builder.quickRender(true);
// builder.timezone(Timezone.ASIA_CALCUTTA);
// builder.userAgent(UserAgent.CHROME);
JBrowserDriver driver = new JBrowserDriver(builder.build());
// You can optionally pass a Settings object here,
// constructed using Settings.Builder
// JBrowserDriver driver = new JBrowserDriver(Settings.builder().timezone(Timezone.AMERICA_NEWYORK).build());
// This will block for the page load and any
// associated AJAX requests
driver.get("https://www.baidu.com/");
// You can get status code unlike other Selenium drivers.
// It blocks for AJAX requests and page loads after clicks
// and keyboard events.
// System.out.println(driver.getStatusCode());
// Returns the page source in its current state, including
// any DOM updates that occurred after page load
// System.out.println(driver.getPageSource());
File screenShot = driver.getScreenshotAs(OutputType.FILE);
// 将图片写到 jar包执行时所在的目录
FileUtils.copyFile(screenShot, new File("." + File.separator + "screenShot.jpg"));
// Close the browser. Allows this thread to terminate.
driver.quit();
}
}
pom.xml 配置maven打jar包, 并执行main方法测试
4.0.0
headless
headless-artifact
1.0-SNAPSHOT
com.machinepublishers
jbrowserdriver
0.17.11
commons-io
commons-io
2.6
maven-assembly-plugin
false
jar-with-dependencies
com.lemonsoft.Example
JBrowserDriverExample-v0.1
make-assembly
package
assembly
下载地址