打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Selenium技巧-截图相关-收集未整


1Selenium技巧-截图加上时间戳

问题场景:

截图太多,如何给截图加上时间戳,防止重复命名的覆盖

解决办法:

driver.get_screenshot_as_file(u"D:/save_screenshot/%s.png" %datetime.now().strftime("%Y%m%d.%H%M%S.%f")[:-3])

这样截图文件的命名为“20120720.105146.343”(最后三位是千分秒,截图基本不太可能出现重名)

http://www.myexception.cn/internet/562906.html

 

2selenium webdriver学习(十二)-怎么利用selenium-webdriver截图

在自动化测试中常常会用到截图功能。最近用了一下selenium-webdriver的截图功能还算不错,可以截取页面全图,不管页面有多长。

 

下面的代码演示了如何使用webdriver进行截图:

import java.io.File;

import java.io.IOException;

 

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

importorg.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

importorg.openqa.selenium.firefox.FirefoxDriver;

public class ShotScreen {

 

         /**

          * @author gongjf

          * @throws IOException

          * @throws InterruptedException

          */

         publicstatic void main(String[] args) throws IOException, InterruptedException {

                

                 System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe"); 

                 WebDriverdr = new FirefoxDriver();

                 dr.get("http://www.51.com");

                                  

                 //这里等待页面加载完成

                 Thread.sleep(5000);

                 //下面代码是得到截图并保存在D盘下

                 FilescreenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);

                 FileUtils.copyFile(screenShotFile,new File("D:/test.png"));}}

 

 看了一下OutputType接口和TakesScreenshot接口,吐槽一下,貌似这两个接口不是同一个开发写的或者注释没有更新怎么的。在OutputType里面的注释说:

/**

 *Defines the output type for a screenshot. See org.openqa.selenium.Screenshotfor usage and

 *examples.

...

 

然后在那找了半天的org.openqa.selenium.Screenshot 接口,晕,后来想应该是org.openqa.selenium.TakesScreenshot

TakesScreenshot里有如下注释:

 /**

   *Capture the screenshot and store it in the specified location.

   *

   *<p>For WebDriver extending TakesScreenshot, this makes a best effort

   *depending on the browser to return the following in order of preference:

   *<ul>

  *   <li>Entirepage</li>

  *   <li>Currentwindow</li>

  *   <li>Visible portion ofthe current frame</li>

  *   <li>The screenshot ofthe entire display containing the browser</li>

   *</ul>

   *

   *<p>For WebElementextending TakesScreenshot, this makes a best effort

   *depending on the browser to return the following in order of preference:

  *   - The entire content of theHTML element

  *   - The visisble portion of theHTML element

   *

   *@param <X> Return type for getScreenshotAs.

   *@param target target type, @see OutputType

   *@return Object in which is stored information about the screenshot.

   *@throws WebDriverException on failure.

  */

 试了一下截取Webelement最终发现WebElement接口没有实现这个类。搞了半天也只是会了截取页面的全图。截取当前的frame也截取的页面全图。难道这个功能没有完善,好吧,这样说自我安慰一下。

selenium-webdriver 面向接口编程,找一个需要的功能还真是挺难的。

http://www.myexception.cn/web/423032.html

 3Selenium-webdriver系列课程(18)————万能的截图

Selenium-webdriver系列教程(18)————万能的截图
截图技能对于测试人员来说应该是较为重要的一个技能,就像踢假球是国内球员混迹中超的必备技能一般。

在自动化测试中,截图可以帮助我们直观的定位错误、记录测试步骤。

记得以前在给某跨国银行做自动化项目的时候,某银的PM要求我们自动化测试的每一步至少需要1个截图,以证明每个功能都被自动化测试给覆盖过,在这种情况下截图就成了证明自动化测试有效性的重要手段。

好的测试人员都会截得一手好图,就跟骨灰级宅男定会吟得一手好诗一般。

webdriver
的截图功能十分强悍。以前在截图的时候,最麻烦的问题莫过于页面太长而只能截到一屏,屏幕以外需要移动滚动条才能看到的区域一般是截不到的。现在webdriver解决了这个问题,无论页面有多长,webdriver都能比较完美的截到完整的页面。

下面的代码演示了如何使用webdriver进行截图:

require 'rubygems'

require 'selenium-webdriver'

dr = Selenium::WebDriver.for :ie

url = 'http://qq.com'

dr.get url

dr.save_screenshot '.\qq.png'

dr.close


save_screenshot
方法实现了截图功能,只需要传入保存截图的文件名就可以了,十分方便

 4selenium获取目前快照截图(web driver可用)

 

selenium获取当前快照截图(web driver可用)

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


经测试android可用,图片存储于temp目录中,可以写入到自定义目录。

 

 5selenium自动化测试,处置alert

 

selenium自动化测试,处理alert
//处理弹出框。

if(sel.isAlertPresent()){
   sel.getAlert();
}

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WebDriver拾级而上·之十二 截图selenium
《selenium2 python 自动化测试实战》(18)——webdriver错误截图
testng 失败自动截图
Selenium2+python自动化60
pytest文档8-html报告报错截图+失败重跑
python selenium浏览器截图
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服