前端测试工具-浏览器自动截图

4月16号参加了百度举办的技术沙龙,学了不少东西。去哪儿网分享的他们的前端测试工具,感觉很有意思。就是有一个程序运行他会自动启动你电脑的上的浏览器,感觉很好玩,也很实用,因为前端开发好多时间都花在了多浏览器兼容上了。

 

回来后我突然有了自己的想法。如下:就是我写一个程序,我给ta传一个url,他能自动启动我电脑上的浏览器,然后自动截图,然后分浏览器,按时间存储在我电脑的一个地方。写的过程我又想,这样截得图片会很多,要是有个程序,ta能自动去分析两张图片的差异,那这就太完美了。

 

启动我电脑上的浏览器,自动截图,自动保存制定目录的代码如下:

public class ScreenShot {
    //IE
    //Runtime.getRuntime().exec("explorer "+ url); 
	private static void start(String url, Browsers browsers) throws Exception {
		switch (browsers) {
		case Chrome:
			Runtime.getRuntime().exec("C:\\Documents and Settings\\Administrator\\Local Settings\\Application Data\\Google\\Chrome\\Application\\chrome.exe " + url);
			break;
		case TheWorld:
			Runtime.getRuntime().exec("D:\\usr\\local\\TheWorld\\TheWorld.exe " + url);
			break;
		case Firefox:
			Runtime.getRuntime().exec("D:\\usr\\local\\firefox\\firefox.exe " + url);
			break;
		case Maxthon:
			Runtime.getRuntime().exec("C:\\Program Files\\Maxthon2\\Maxthon.exe " + url);
			break;
		case Opera:
			Runtime.getRuntime().exec("C:\\Program Files\\Opera\\opera.exe " + url);
			break;
		case QQ:
			Runtime.getRuntime().exec("D:\\usr\\local\\QQBrowser\\Bin\\QQBrowser.exe " + url);
			break;
		case Souguo:
			Runtime.getRuntime().exec("D:\\usr\\local\\SogouExplorer\\sogouexplorer.exe " + url);
			break;
		default:
			Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
			break;
		}
	}
	
	private static void stop(String url, Browsers browsers) throws Exception {
		switch (browsers) {
		case Chrome:
			Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
			break;
		case TheWorld:
			Runtime.getRuntime().exec("taskkill /F /IM TheWorld.exe");
			break;
		case Firefox:
			Runtime.getRuntime().exec("taskkill /F /IM firefox.exe");
			break;
		case Maxthon:
			Runtime.getRuntime().exec("taskkill /F /IM Maxthon.exe");
			break;
		case Opera:
			Runtime.getRuntime().exec("taskkill /F /IM opera.exe ");
			break;
		case QQ:
			Runtime.getRuntime().exec("taskkill /F /IM QQBrowser.exe ");
			break;
		case Souguo:
			Runtime.getRuntime().exec("taskkill /F /IM sogouexplorer.exe ");
			break;
		default:
			Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
			break;
		}
	}
	
	private static void test(List<String> urls,Browsers browsers,String dir){
		String osName = System.getProperty("os.name");
		for (int i = 0; i < urls.size(); i++) {
			String url = urls.get(i);
			if(url!=null && url.length() > 0){
				try {
					if (osName.startsWith("Windows")) {
						Robot robot = new Robot();
						stop(url,browsers);
						robot.delay(3000);
						start(url,browsers);
						robot.delay(3000);
//						robot.mouseWheel(2000);
						Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
						int width = (int) d.getWidth();
						int height = (int) d.getHeight();
						// 最大化浏览器
						robot.keyRelease(KeyEvent.VK_F11);
						robot.delay(3000);
						Image image = robot.createScreenCapture(new Rectangle(0, 0, width,
								height));
						BufferedImage bi = new BufferedImage(width, height,
								BufferedImage.TYPE_INT_RGB);
						Graphics g = bi.createGraphics();
						g.drawImage(image, 0, 0, width, height, null);
						// 保存图片
						ImageIO.write(bi, "jpg", new File( dir+"/"+UtilDateTime.getDateTimeStamp().toString().replace(":", "-").replace(".", "-")+".jpg"));
						robot.delay(3000);
						stop(url,browsers);
					} 
				} catch (Exception ex) {
					 ex.printStackTrace();
				}
			}
		}
	}
	
	private void run (List<String> urls,String parentDir) throws Exception {
		//test chrome
		File chrome = new File(parentDir+"/"+Browsers.Chrome.toString());
		chrome.mkdirs();
		test(urls,Browsers.Chrome,parentDir+"/"+Browsers.Chrome.toString());
		
		//test firefox
		File firefox = new File(parentDir+"/"+Browsers.Firefox.toString());
		firefox.mkdirs();
		test(urls,Browsers.Firefox,parentDir+"/"+Browsers.Firefox.toString());
		
		//test theWorld
		File theWorld = new File(parentDir+"/"+Browsers.TheWorld.toString());
		theWorld.mkdirs();
		test(urls,Browsers.TheWorld,parentDir+"/"+Browsers.TheWorld.toString());
		
		//test Maxthon
		File maxthon = new File(parentDir+"/"+Browsers.Maxthon.toString());
		maxthon.mkdirs();
		test(urls,Browsers.Maxthon,parentDir+"/"+Browsers.Maxthon.toString());
		
		//test opera
		File opera = new File(parentDir+"/"+Browsers.Opera.toString());
		opera.mkdirs();
		test(urls,Browsers.Opera,parentDir+"/"+Browsers.Opera.toString());
		
		
		//test opera
		File qq = new File(parentDir+"/"+Browsers.QQ.toString());
		qq.mkdirs();
		test(urls,Browsers.QQ,parentDir+"/"+Browsers.QQ.toString());
		
		//test opera
		File souguo = new File(parentDir+"/"+Browsers.Souguo.toString());
		souguo.mkdirs();
		test(urls,Browsers.Souguo,parentDir+"/"+Browsers.Souguo.toString());
	}

	
	public static void main(String[] args) throws Exception{
		String currDate = UtilDateTime.getDateTimeStamp().toString().replace(":", "-").replace(".", "-");
		File directory = new File("d:/temp/"+currDate);
		if (!(directory.exists()) && !(directory.isDirectory())) {
            directory.mkdirs();
        }
		String parentDir = "d:/temp/"+currDate;
		List<String> urls = new ArrayList<String>();
//		urls.add("http://www.youku.com");
//		urls.add("http://www.sina.com.cn");
//		urls.add("http://www.sohu.com");
//		urls.add("http://www.qq.com");
//		urls.add("http://www.163.com");
//		urls.add("http://www.csdn.com");
		urls.add("http://www.iteye.com");
//		urls.add("http://192.168.1.33/index.jsp");
//		urls.add("http://192.168.1.33/about/info.jsp");
//		urls.add("http://192.168.1.33/news/typeList.jsp?typeid=1");
//		urls.add("http://192.168.1.33/news/typeList.jsp?typeid=2");
//		urls.add("http://192.168.1.33/news/typeList.jsp?typeid=3");
		ScreenShot screenShot = new ScreenShot();
		screenShot.run(urls,parentDir);
	}

 代码说明如下:程序运行在window系统上,启动浏览器的这部分代码是在网上copy一部分然后又改了一部分。

 

程序运行前,得先把浏览器设置好,如不要让他弹出提示框说设置为默认浏览器等等,妨碍截图。但是opera浏览器我结束进程,再启动时,老弹出一个框,还没有找到不让ta弹出来来。

 

程序运行的不错,感觉明天有变美好了,呵呵。

美中不足的是:这个程序只能截取第一屏的图,ta不能自动判断所给url的页面的长度,截取所有的图片。但也有一个不完美的办法是添加这个代码,让它去截最后一屏的图片(2000这个数字可以更大一些),这个基本能解决一般情况下的问题。

robot.mouseWheel(2000);

 

关于两个个图片的比较,或者说是相似度的比较,网上说了一些办法,如把图片转换为字符串,然后再比较字符串,试了试,直接把我的eclipse给搞死了。这算也是美中不足吧

我始终坚信:只有想不到,没有做不到的。加油,自己可以的,呵呵

 

 

 

 


 

 

源代码下载:http://code.google.com/p/catchwebhtmlcontent/downloads/detail?name=browser-shutScreen.zip&can=2&q=#makechanges

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值