码迷,mamicode.com
首页 > Web开发 > 详细

Selenium2学习-033-WebUI自动化实战实例-031-页面快照截图应用之二 -- 区域截图

时间:2015-08-08 13:24:42      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

我在之前的文章中曾给出浏览器显示区域截图的方法,具体请参阅 。或许,有些小主已经想到了,每次都获取整个显示区域的截图存储,那么经过一段时间后,所使用的图片服务器的容量将会受到极大的挑战,尤其是在产品需要获取页面样式截图或断言失败截图比较多的情况下。解决此问题有两种途径,一是定期清理过期的样式截图;二是不需要获取整个显示区域的样式截图(即指定区域范围截图)。此文给出的方法即是区域范围截图,敬请各位小主参阅。若有不足之处,敬请指正,不胜感激!

不唠叨了,直接上码了。。。

 1     /**
 2      * Get basic snapshot for expected area of display screen area
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoSeleniumDemo main.aaron.sele.core SeleniumCore.java snapshotPartial, 2015-7-28 01:41:12 Exp $
 6      * 
 7      * @param filename : store png file name
 8      * @param left     : left distance
 9      * @param top      : top distance
10      * @param width    : width distance
11      * @param height   : height distance
12      *  
13      * @return boolean
14      */
15     public boolean snapshotPartial(String filename, int left, int top, int width, int height){
16         boolean success = false;
17         
18         try {
19             // Get byte data of full screen capture
20             byte[] byte_screen_capture = ((TakesScreenshot) this.webdriver).getScreenshotAs(OutputType.BYTES);
21             
22             // create full screen capture
23             BufferedImage img_screen_catpture = ImageIO.read(new ByteArrayInputStream(byte_screen_capture));
24             
25             // get partial image by location and size
26             BufferedImage partial_screen_capture = img_screen_catpture.getSubimage(left, top, width, height);
27             
28             File f = new File(filename);
29             
30             if (f.isFile() && f.exists()) {
31                 f.delete();
32             }
33             
34             // store partial image
35             ImageIO.write(partial_screen_capture, "png", f);
36             
37             success = true;
38         } catch (IOException ioe_sci) {
39             ioe_sci.printStackTrace();
40         } catch (RasterFormatException rfe) {
41             rfe.printStackTrace();
42         }
43         
44         return success;
45     }

 

至此,WebUI 自动化功能测试脚本第 031-页面快照截图应用之二 -- 区域截图 顺利完结,希望此文能够给初学 Selenium 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

Selenium2学习-033-WebUI自动化实战实例-031-页面快照截图应用之二 -- 区域截图

标签:

原文地址:http://www.cnblogs.com/fengpingfan/p/4712962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!