ScreenCaptureHtmlUnitDriver.java
https://github.com/apache/incubator-zeppelin/blob/master/zeppelin-server/src/test/java/com/webautomation/ScreenCaptureHtmlUnitDriver.java
http://stackoverflow.com/questions/36254656/not-able-to-take-screenshot-using-htmlunitdriver-selenium-webdriver-java
| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package com.webautomation; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.io.IOException; | |
| import java.net.URL; | |
| import java.util.Collections; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| import java.util.Map; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| import java.util.zip.ZipEntry; | |
| import java.util.zip.ZipOutputStream; | |
| import org.apache.commons.io.FilenameUtils; | |
| import org.apache.commons.io.IOUtils; | |
| import org.openqa.selenium.Capabilities; | |
| import org.openqa.selenium.OutputType; | |
| import org.openqa.selenium.TakesScreenshot; | |
| import org.openqa.selenium.WebDriverException; | |
| import org.openqa.selenium.htmlunit.HtmlUnitDriver; | |
| import org.openqa.selenium.internal.Base64Encoder; | |
| import org.openqa.selenium.remote.CapabilityType; | |
| import org.openqa.selenium.remote.DesiredCapabilities; | |
| import com.gargoylesoftware.htmlunit.BrowserVersion; | |
| import com.gargoylesoftware.htmlunit.WebClient; | |
| import com.gargoylesoftware.htmlunit.WebRequest; | |
| import com.gargoylesoftware.htmlunit.WebWindow; | |
| import com.gargoylesoftware.htmlunit.html.HtmlElement; | |
| import com.gargoylesoftware.htmlunit.html.HtmlPage; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| /** | |
| * from https://code.google.com/p/selenium/issues/detail?id=1361 | |
| */ | |
| public class ScreenCaptureHtmlUnitDriver extends HtmlUnitDriver implements TakesScreenshot { | |
| private static Map<String, byte[]> imagesCache = Collections.synchronizedMap(new HashMap<String, byte[]>()); | |
| private static Map<String, String> cssjsCache = Collections.synchronizedMap(new HashMap<String, String>()); | |
| // http://stackoverflow.com/questions/4652777/java-regex-to-get-the-urls-from-css | |
| private final static Pattern cssUrlPattern = Pattern.compile("background(-image)?[\\s]*:[^url]*url[\\s]*\\([\\s]*([^\\)]*)[\\s]*\\)[\\s]*");// ?<url> | |
| static Logger LOGGER = LoggerFactory.getLogger(ScreenCaptureHtmlUnitDriver.class); | |
| public ScreenCaptureHtmlUnitDriver() { | |
| super(); | |
| } | |
| public ScreenCaptureHtmlUnitDriver(boolean enableJavascript) { | |
| super(enableJavascript); | |
| } | |
| public ScreenCaptureHtmlUnitDriver(Capabilities capabilities) { | |
| super(capabilities); | |
| } | |
| public ScreenCaptureHtmlUnitDriver(BrowserVersion version) { | |
| super(version); | |
| DesiredCapabilities var = ((DesiredCapabilities) getCapabilities()); | |
| var.setCapability(CapabilityType.TAKES_SCREENSHOT, true); | |
| } | |
| @Override | |
| @SuppressWarnings("unchecked") | |
| public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException { | |
| byte[] archive = new byte[0]; | |
| try { | |
| archive = downloadCssAndImages(getWebClient(), (HtmlPage) getCurrentWindow().getEnclosedPage()); | |
| } catch (Exception e) { | |
| LOGGER.error("Exception in ScreenCaptureHtmlUnitDriver while getScreenshotAs ", e); | |
| } | |
| if(target.equals(OutputType.BASE64)){ | |
| return target.convertFromBase64Png(new Base64Encoder().encode(archive)); | |
| } | |
| if(target.equals(OutputType.FILE)){ | |
| File f = new File("screen.tmp"); | |
| try { | |
| FileOutputStream scr = new FileOutputStream(f); | |
| scr.write(archive); | |
| scr.close(); | |
| } catch (IOException e) { | |
| throw new WebDriverException(e); | |
| } | |
| return (X) f; | |
| } | |
| return (X) archive; | |
| } | |
| // http://stackoverflow.com/questions/2244272/how-can-i-tell-htmlunits-webclient-to-download-images-and-css | |
| protected byte[] downloadCssAndImages(WebClient webClient, HtmlPage page) throws Exception { | |
| WebWindow currentWindow = webClient.getCurrentWindow(); | |
| Map<String, String> urlMapping = new HashMap<String, String>(); | |
| Map<String, byte[]> files = new HashMap<String, byte[]>(); | |
| WebWindow window = null; | |
| try { | |
| window = webClient.getWebWindowByName(page.getUrl().toString()+"_screenshot"); | |
| webClient.getPage(window, new WebRequest(page.getUrl())); | |
| } catch (Exception e) { | |
| LOGGER.error("Exception in ScreenCaptureHtmlUnitDriver while downloadCssAndImages ", e); | |
| window = webClient.openWindow(page.getUrl(), page.getUrl().toString()+"_screenshot"); | |
| } | |
| String xPathExpression = "//*[name() = 'img' or name() = 'link' and (@type = 'text/css' or @type = 'image/x-icon') or @type = 'text/javascript']"; | |
| List<?> resultList = page.getByXPath(xPathExpression); | |
| Iterator<?> i = resultList.iterator(); | |
| while (i.hasNext()) { | |
| try { | |
| HtmlElement el = (HtmlElement) i.next(); | |
| String resourceSourcePath = el.getAttribute("src").equals("") ? el.getAttribute("href") : el | |
| .getAttribute("src"); | |
| if (resourceSourcePath == null || resourceSourcePath.equals("")) | |
| continue; | |
| URL resourceRemoteLink = page.getFullyQualifiedUrl(resourceSourcePath); | |
| String resourceLocalPath = mapLocalUrl(page, resourceRemoteLink, resourceSourcePath, urlMapping); | |
| urlMapping.put(resourceSourcePath, resourceLocalPath); | |
| if (!resourceRemoteLink.toString().endsWith(".css")) { | |
| byte[] image = downloadImage(webClient, window, resourceRemoteLink); | |
| files.put(resourceLocalPath, image); | |
| } else { | |
| String css = downloadCss(webClient, window, resourceRemoteLink); | |
| for (String cssImagePath : getLinksFromCss(css)) { | |
| URL cssImagelink = page.getFullyQualifiedUrl(cssImagePath.replace("\"", "").replace("\'", "") | |
| .replace(" ", "")); | |
| String cssImageLocalPath = mapLocalUrl(page, cssImagelink, cssImagePath, urlMapping); | |
| files.put(cssImageLocalPath, downloadImage(webClient, window, cssImagelink)); | |
| } | |
| files.put(resourceLocalPath, replaceRemoteUrlsWithLocal(css, urlMapping) | |
| .replace("resources/", "./").getBytes()); | |
| } | |
| } catch (Exception e) { | |
| LOGGER.error("Exception in ScreenCaptureHtmlUnitDriver while resultList.iterator ", e); | |
| } | |
| } | |
| String pagesrc = replaceRemoteUrlsWithLocal(page.getWebResponse().getContentAsString(), urlMapping); | |
| files.put("page.html", pagesrc.getBytes()); | |
| webClient.setCurrentWindow(currentWindow); | |
| return createZip(files); | |
| } | |
| String downloadCss(WebClient webClient, WebWindow window, URL resourceUrl) throws Exception { | |
| if (cssjsCache.get(resourceUrl.toString()) == null) { | |
| cssjsCache.put(resourceUrl.toString(), webClient.getPage(window, new WebRequest(resourceUrl)) | |
| .getWebResponse().getContentAsString()); | |
| } | |
| return cssjsCache.get(resourceUrl.toString()); | |
| } | |
| byte[] downloadImage(WebClient webClient, WebWindow window, URL resourceUrl) throws Exception { | |
| if (imagesCache.get(resourceUrl.toString()) == null) { | |
| imagesCache.put( | |
| resourceUrl.toString(), | |
| IOUtils.toByteArray(webClient.getPage(window, new WebRequest(resourceUrl)).getWebResponse() | |
| .getContentAsStream())); | |
| } | |
| return imagesCache.get(resourceUrl.toString()); | |
| } | |
| public static byte[] createZip(Map<String, byte[]> files) throws IOException { | |
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
| ZipOutputStream zipfile = new ZipOutputStream(bos); | |
| Iterator<String> i = files.keySet().iterator(); | |
| String fileName = null; | |
| ZipEntry zipentry = null; | |
| while (i.hasNext()) { | |
| fileName = i.next(); | |
| zipentry = new ZipEntry(fileName); | |
| zipfile.putNextEntry(zipentry); | |
| zipfile.write(files.get(fileName)); | |
| } | |
| zipfile.close(); | |
| return bos.toByteArray(); | |
| } | |
| List<String> getLinksFromCss(String css) { | |
| List<String> result = new LinkedList<String>(); | |
| Matcher m = cssUrlPattern.matcher(css); | |
| while (m.find()) { // find next match | |
| result.add( m.group(2)); | |
| } | |
| return result; | |
| } | |
| String replaceRemoteUrlsWithLocal(String source, Map<String, String> replacement) { | |
| for (String object : replacement.keySet()) { | |
| // background:url(http://org.com/images/image.gif) | |
| source = source.replace(object, replacement.get(object)); | |
| } | |
| return source; | |
| } | |
| String mapLocalUrl(HtmlPage page, URL link, String path, Map<String, String> replacementToAdd) throws Exception { | |
| String resultingFileName = "resources/" + FilenameUtils.getName(link.getFile()); | |
| replacementToAdd.put(path, resultingFileName); | |
| return resultingFileName; | |
| } | |
| } |
ScreenCaptureHtmlUnitDriver.java的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
随机推荐
- hdu_5274_Dylans loves tree(树剖)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5274 题意:给一棵树和叶子的值,然后有单点修改操作和询问区间操作,询问的是每一个值出现的奇偶次数,如果 ...
- LeetCode OJ 118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- TeX括号。。。
#include <stdio.h> #include <stdlib.h> int main() { ; ) { if(c=='"') { printf(" ...
- UIView animateWithDuration 使用详解
在ios4.0及以后鼓励使用animateWithDuration方法来实现动画效果.当然,以往的begin/commit的方法依然使用,下面详细解释一下animateWithDuration的使用方 ...
- php 获取地址栏参数
javascript实现: top.location.href 顶级窗口的地址this.location.href 当前窗口的地址 PHP实现: //获取域名或主机地址 echo $_SERVER[' ...
- Entity Framework 学习高级篇1—改善EF代码的方法(上)
本节,我们将介绍一些改善EF代码的相关方法,如NoTracking,GetObjectByKey, Include等. l MergeOption.NoTracking 当我们只需要读 ...
- screen 链接远程桌面
screen 开一个新的screen窗口 screen -ls 查看已经存在的所有screen窗口 screen -r 208111 进入这个窗口 ctrl+a+d 退出screen,回 ...
- PHP set_error_handler()函数的使用【转载】
我们写程序,难免会有问题(是经常会遇到问题 ),而PHP遇到错误时,就会给出出错脚本的位置.行数和原因.有很多人说,这并没有什么大不了.确实,在调试程序阶段,这确实是没啥的,而且我认为给出错误路径是必 ...
- Qt学习之系列[9] – QCoreApplication:processEvents()可能会引起递归,导致栈溢出崩溃
api含义:QCoreApplication::processEvents() 将处理所有事件队列中的事件并返回给调用者. 问题描述: 当主线程在某个槽函数里正在执行processEvents时, 刚 ...
- Mysql找回root密码
1.停止数据库服务 service mysql stop 2.修改my.cnf 添加skip-grant-tables 3.重启数据库 service mysql restart 4.修改root密码 ...