内容 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轴距离以及图片大小
各位测试道友可根据自己实际需求选择方案。

+ 猜你喜欢...

  • 使用WebDriver遇到的那些坑
  • Nightwatch.js – 基于Node.js的验收测试框架
  • Sahi-基于业务,成熟的Web自动化测试工具
  • Selenium专题—定位页面表格和单元格
  • Selenium-XPath,CSS,DOM对照表
  • Selenium专题—JQuery选择器(一)
  • Selenium专题—日志记录
  • Selenium专题—报告生成
  • Selenium专题—数据驱动实现
  • Selenium专题—弹出框处理

===== 关于 DiggerPlus =====
DiggerPlus是国内新锐测试人垂直内容博客,专注于挖掘测试工具,普及测试技术,专注于测试人能力提升,博客上线后就受到广大测试人的热烈追捧;此外,DiggerPlus深度整合评测资源,揭秘科技数据和真相,让读者更懂科技,更好地把玩科技。 我们始终保持"独立,客观,严谨,优秀"的优良作风,努力为读者带来源源不断的优质内容。想访问更多酷毙的测试人网站?赶紧来DiggerPlus测试人网址导航看看吧!

===== DiggerPlus Team =====
DiggerPlus Team是DiggerPlus官方发起的团队,这是一群热爱科技,热爱测试,喜欢深度挖掘的热血测试人,他们是评测师,测试专家。 我们欢迎优秀的测试人加入DiggerPlus Team。 加入DiggerPlus,可以成为我们的认证Dper,认证Dper拥有DiggerPlus独一无二的专栏并得到个人展示。

===== DiggerPlus 官方读者群 =====

原创粉丝点击