我已经了解了PhantomJS和CasperJS的强大功能,以获取网站截图.文章“Responsive Screenshots With Casper”对于了解这两种技术的基础知识以及屏幕截图功能非常有帮助.
接下来是如何使用此技术与Spring MVC应用程序集成.
到目前为止,我已经尝试了“Screen Capture using PhantomJS, GhostDriver, Selenium Hub”的代码并将其与“Remote PhantomJS driver in Junit”中提供的answer相结合.
发生的事情是,每次运行程序时,我都会遇到ClassNotFoundError,并且我一直提供缺少的JAR文件.我最终在Spring应用程序中下载并提供了九个新的JAR文件:
> phantomjsdriver-1.0.4.jar
> selenium-java-2.39.0.jar及其源文件
> asm-all-3.3.1.jar
> cglib-3.1.jar
> commons-exec-1.2.jar
> guava-16.0.1.jar
> httpclient-4.3.2.jar
> httpcore-4.3.2.jar
> json-20140107.jar
SEVERE: Servlet.service() for servlet [spring] in context with path [/my_spring_app] threw exception [Handler processing Failed; nested exception is java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class] with root cause
java.lang.IncompatibleClassChangeError: class net.sf.cglib.core.DebuggingClassWriter has interface org.objectweb.asm.ClassVisitor as super class
我应该继续尝试这条路径来获取我网站的截图吗?根据发生的事情,我觉得这是一场失败的战斗.我不熟悉PhantomJS,也无法在线找到任何合法的教程或指南.如果我不能做这项工作,我会回到iText和JFreeCharts.
下面是我在Spring应用程序中使用的代码:
package my_spring_app.controller;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MyController {
protected final static Log logger = LogFactory
.getLog(MyController.class);
@RequestMapping(value = "/screenshot", method = RequestMethod.GET)
public String showSupplementsPage(ModelMap model,
HttpServletRequest request, HttpServletResponse response) {
tryPhantom();
return "screenshot";
}
private URI uri;
private static PhantomJSDriverService service;
private WebDriver webDriver;
protected static DesiredCapabilities dCaps;
public void tryPhantom() {
service = new PhantomJSDriverService.Builder()
.usingPhantomJSExecutable(new File("classpath:phantomjs.exe"))
.usingAnyFreePort().build();
try {
service.start();
} catch (IOException e) {
e.printstacktrace();
}
try {
uri = new URI("http://localhost:8080/");
} catch (URISyntaxException e) {
e.printstacktrace();
}
dCaps = new DesiredCapabilities();
dCaps.setJavascriptEnabled(true);
dCaps.setCapability("takesScreenshot", true);
webDriver = new RemoteWebDriver(service.getUrl(), dCaps);
webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
long iStart = System.currentTimeMillis();
webDriver.get("http://localhost:8080/");
webDriver = new Augmenter().augment(webDriver);
File srcFile = ((TakesScreenshot) webDriver)
.getScreenshotAs(OutputType.FILE);
System.out.println("File:" + srcFile);
try {
FileUtils.copyFile(srcFile, new File("classpath:screenshots/pic.png"));
} catch (IOException e) {
e.printstacktrace();
}
System.out.println("Single Page Time:"
+ (System.currentTimeMillis() - iStart));
webDriver.quit();
service.stop();
}
}
这是我想到的图表:
解决方法:
我应该继续尝试这条路径来获取我网站的截图吗?根据发生的事情,我觉得这是一场失败的战斗.
你是对的,我认为需要重新考虑管理依赖关系的方法,否则将无法使网站运行.逐个手动装载罐子不太可能产生好的结果,因为不能保证库版本彼此兼容.
事实上,IncompatibleClassChangeError是下载的jar不兼容的标志.
你最好的选择是使用Maven自动卸载大部分罐子.通过仅声明顶级jar,Maven将自动下载所需的依赖jar(传递依赖),有助于减少并在许多情况下消除库不兼容问题.
一旦您启动并运行了该站点,就可以在Casper.js中编写一些脚本来访问本地服务器中的页面,单击某些按钮以生成PDF等,然后拍摄一些快照.
Spring MVC不需要特殊的集成,对于Spring MVC应用程序,如果PhantomJS或普通浏览器访问该站点,它是透明的.
原文地址:https://codeday.me/bug/20190703/1365768.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。