内容 Selenium专题—截屏功能
来源:互联网 发布:下载ppt的软件 编辑:程序博客网 时间:2024/05/17 08:22
在自动化测试运行过程中,我们可以截取图像记录我们的测试过程或验证功能,本文将会介绍两种截屏功能。
1、使用webdriver封装的API函数截图
程序清单如下:
//截屏功能,并以当前时间戳为文件名保存在指定目录下public void takeScreenShot(){String dir_name = "screenshot";//定义一个截图存放的目录名,此处为当前目录的screenshot目录下//判断目录是否存在if(!(new File(dir_name).isDirectory())){//如果不存在则新建目录new File(dir_name).mkdir();}SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");//格式化当前时间,例如20120406-165210String time = sdf.format(new Date()); File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); try{ //将截图存放到指定目录,并以当前时间戳作为文件名保存 FileUtils.copyFile(scrFile, new File(dir_name + File.separator + time + ".png")); }catch(IOException e){ e.printStackTrace(); } }
2、使用java提供的接口截图
//获取元素坐标并截图 public void elePosition(WebDriver driver,By location){ String js = "document.getElementById('su').style.border='2px solid red'";((JavascriptExecutor)driver).executeScript(js); WebElement ele = driver.findElement(location); Point p = ele.getLocation(); Dimension d = ele.getSize(); System.out.println("x:"+p.x+" y:"+p.y); System.out.println("height:"+d.height+" width:"+d.width); screenshot(p.x,p.y,d.width,d.height); } //截图 public void screenshot(int x,int y,int w,int h){ String dir_name = "screenshot";//定义一个截图存放的目录名,此处为当前目录的screenshot目录下//判断目录是否存在if(!(new File(dir_name).isDirectory())){//如果不存在则新建目录new File(dir_name).mkdir();}SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");//格式化当前时间,例如20131016-165210String time = sdf.format(new Date()); try { //屏幕的分辨率 int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(); //要截取的宽度 int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight(); //要截取的高度 //System.out.println(width+" "+height); Robot robot = new Robot(); BufferedImage image = robot.createScreenCapture(new Rectangle(width,height)); image = image.getSubimage(x, y, w+100, h+200); ImageIO.write (image, "png" , new File(dir_name + File.separator + time + ".png")); //保存到硬盘 }catch(AWTException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } }
以上两种方法都能成功的截取图片,区别在于:
第一种是截取整个浏览器页面
第二种是截取指定x轴、y轴距离以及图片大小
各位测试道友可根据自己实际需求选择方案。
+ 猜你喜欢...
===== 关于 DiggerPlus =====
DiggerPlus是国内新锐测试人垂直内容博客,专注于挖掘测试工具,普及测试技术,专注于测试人能力提升,博客上线后就受到广大测试人的热烈追捧;此外,DiggerPlus深度整合评测资源,揭秘科技数据和真相,让读者更懂科技,更好地把玩科技。 我们始终保持"独立,客观,严谨,优秀"的优良作风,努力为读者带来源源不断的优质内容。想访问更多酷毙的测试人网站?赶紧来DiggerPlus测试人网址导航看看吧!
===== DiggerPlus Team =====
DiggerPlus Team是DiggerPlus官方发起的团队,这是一群热爱科技,热爱测试,喜欢深度挖掘的热血测试人,他们是评测师,测试专家。 我们欢迎优秀的测试人加入DiggerPlus Team。 加入DiggerPlus,可以成为我们的认证Dper,认证Dper拥有DiggerPlus独一无二的专栏并得到个人展示。
===== DiggerPlus 官方读者群 =====
阅读全文
0 0
- 内容 Selenium专题—截屏功能
- 【专题】我们常用的功能自动化测试工具——Selenium篇
- Selenium专题—弹出框处理
- 使用selenium爬取微博内容
- 仿专题订阅功能
- 【Selenium专题】元素定位之CssSelector
- 转载:使用selenium爬取微博内容
- 使用selenium抓取网页内容
- python selenium查找网页内容
- 功能测试工具Selenium IDE
- selenium IDE录制的功能
- selenium IDE录制的功能
- Selenium Webdriver实现截图功能
- selenium WebDriver 获取disabled文本框内容
- selenium+python 向富文本输入内容
- selenium显示网页所有源码内容
- 20170807学习总结-selenium获取列表内容
- 20170808-selenium获取列表内容2
- c++宏定义详解
- double类型的计算
- 以二叉树的前序遍历为例,递归和非递归方式的实现
- Java多线程--ScheduledExecutorService的使用
- python基础之模块之os模块
- 内容 Selenium专题—截屏功能
- 启动服务器的时候后台没有报错,但是通过浏览器访问项目的时候报404
- MyEclipse2017 CI7 集成开发环境的破解与使用
- cas 单点登录 服务端搭建http协议与https协议
- easyui $.messager.alert失效问题
- sd卡在Linux下格式化等处理小代码编写。
- 希尔排序
- 第一篇
- 数据库连接