Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 594|回复: 0

selenium遇到异常自动截图

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-7-17 05:38:22 | 显示全部楼层 |阅读模式

    最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理。今天坐下总结。

    一、第一种方式,重写onException方法

    只针对webdriver的异常截图,该方法由于只针对webdriver抛的异常时才能截图,有一定的限制

    a.继承AbstractWebDriverEventListener类,重写onException方法,

        public void onException(java.lang.Throwable throwable,
                WebDriver driver){
    
            Throwable cause = throwable.getCause();
        /*
        String cause = throwable.getClass().getName();
        ScreenshotException ec = new ScreenshotException(cause);
        */ System.out.println(throwable.getClass().getName()); System.out.println(
    "onException=" + cause); Date currentTime = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String dateString = formatter.format(currentTime); File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); try { File screenshot = new File("D:/ddd/" + dateString + ".png"); FileUtils.copyFile(scrFile,screenshot); } catch (IOException e) { e.printStackTrace(); } }

    b.测试类,要用EventFiringWebDriver ,并注册MyListen 

        public static void main(String args[]) {
            
            String key = "webdriver.chrome.driver";
            String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
            System.setProperty(key, value);
            WebDriver dr = new ChromeDriver();
            
            EventFiringWebDriver event = new EventFiringWebDriver(dr);
            MyListen ml = new MyListen();
            event.register(ml);
            dr = event;
            dr.get("http://www.baidu.com");
            dr.findElement(By.id("kw1")).sendKeys("test");
            //System.out.println(5/0);
            
            Assert.assertEquals("haha", event.findElement(By.id("su")).getAttribute("value"));
            event.quit();
            
        }

    二、第二种方式:使用testNG的TestListenerAdapter

    a.先建一个类继承TestListenerAdapter,并重写onTestFailure方法

    package com.screenshot.exception;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.velocity.runtime.log.LogManager;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.WebDriver;
    import org.testng.ITestResult;
    import org.testng.TestListenerAdapter;
    
    import com.screenshot.singleton.TestBase;
    
    /** 
     * @author QiaoJiaofei 
     * @version 创建时间:2015年8月24日 下午6:33:44 
     * 类说明 
     */
    public class UseTestNg extends TestListenerAdapter{
    
               @Override  
                public synchronized void onTestFailure(ITestResult result) {  
                     Object currentClass = result.getInstance();  
                      WebDriver webDriver = ((TestUsNg) currentClass).getDriver();  
                    
                            if (webDriver != null)  
                             {  
                                Date currentTime = new Date();
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
                                String dateString = formatter.format(currentTime);
                                File scrFile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
                                try {
                                    
                                    File screenshot = new File("D:/ddd/" 
                                    + dateString  + ".png");
                                    FileUtils.copyFile(scrFile,screenshot);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                               
                          } 
               }  
    }

    b.创建测试类,注意需要在测试类中写getDriver()方法

    package com.screenshot.exception;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;
    
    /** 
     * @author QiaoJiaofei 
     * @version 创建时间:2015年8月24日 下午6:43:44 
     * 类说明 
     */
    public class TestUsNg {
        private WebDriver dr;   
        public WebDriver getDriver() {  
            return dr;  
        } 
    
        @Test
        public void f() {
            String key = "webdriver.chrome.driver";
            String value = "D:/BaiduYunDownload/selenium/chromedriver.exe";
            System.setProperty(key, value);
            dr = new ChromeDriver(); 
            System.out.println(5/0);
        }
    }

    C.在testng的xml文件中添加监听

    <listeners>

    <listener class-name="com.screenshot.exception.UseTestNg" />

    </listeners>

    D.测试类继承该类,每个子类前面要加:@Listeners({com.gm.base.MyListener.class})

    三、如何将生成的图片连接到reportNG中,将下面的代码放入上面相应重写的方法中,图片路径与上述代码中生成的图片结合一起。

    String imgName = "";//图片路径
    Reporter.log("<a href=./img/" + imgName + " target=_blank>Failed Screen Shot</a>", true);  

     题外:使用Robot主动截图,这种可以在自己想截图的时候调用该方法即可截当前界面

    package com.screenshot.book;
    
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    
    import org.testng.annotations.Test;
    
    /** 
     * @author QiaoJiaofei 
     * @version 创建时间:2015年8月26日 下午7:40:34 
     * 类说明 
     */
    public class TestRobot {
        @Test
        public void takeScreenShotMethod(){
            try{
                Thread.sleep(3000);
                BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                ImageIO.write(image, "jpg", new File("D:/ddd/screenshot.jpg"));
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
    }

     

    备注:

    使用junit自动截图,可以使用Rule,由于我用的是testNG,所以没有调试junit的方法。详细参考:http://stackoverflow.com/questions/20995722/when-does-onexception-get-triggered-in-webdrivereventlistener

    @Rule
    public TestRule testWatcher = new TestWatcher() {
    
        @Override
        public void succeeded(Description test){
            for (LogEntry log : driver.manage().logs().get(LogType.DRIVER).getAll()) {
                System.out.println("Level:" + log.getLevel().getName());
                System.out.println("Message:" + log.getMessage());
                System.out.println("Time:" + log.getTimestamp());
                System.out.println("-----------------------------------------------");
            }
            System.out.println();
    
        @Override
        public void failed(Throwable t, Description test) {
    
            String testName = test.getClassName();
            String subTestName = test.getMethodName();
            String screenShotName = String.format("%s\\%s", testName, screenShotName);
            if (driver instanceof TakesScreenshot) {
              File tempFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
              try {
                  System.out.println(">>>>>>>>>>LOGS: " + yourDirForImages + "\\" + screenShotName + ".png");
                  FileUtils.copyFile(tempFile, new File(String.format("%s.png", screenShotName)));
              } catch (IOException e) {
                e.printStackTrace();
              }
        }

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 10:53 , Processed in 0.068269 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表