zoukankan      html  css  js  c++  java
  • testng 异常 截图

    testNG里有一个异常监听类,失败时会执行类里的相关方法

    DriverBase  截图类
    TestngListenerScreen  异常监听类
    Test1 测试类

    1.DriverBase类
    package com.cmall.screenshot;
    
    import com.cmall.appium.DriverFactory;
    import com.cmall.appium.Helper;
    import com.cmall.jdjr.pages.Modules.Integration.HomePage;
    import com.cmall.utils.LogUtil;
    import com.cmall.utils.PropertyUtil;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.pagefactory.AppiumFieldDecorator;
    import org.apache.commons.io.FileUtils;
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.TakesScreenshot;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;
    
    import java.io.File;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.concurrent.TimeUnit;
    
    public class DriverBase
    {
    
        private static AndroidDriver<MobileElement> driver = null;
        private Helper helper;
        private LogUtil log = new LogUtil(DriverBase.class);
        /**
         * 获取driver
         * */
        public AndroidDriver<MobileElement> getDriver() {
            return Test1.mdriver;
        }
    
        /**
         * 自动截图
         * */
        public void takeScreenShot(String methodName) {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
            Calendar cal = Calendar.getInstance();
            Date date = cal.getTime();
            String dateStr = sf.format(date);//上面几行代码的意思都是获取时间,并且格式化,用来作为图片的名称
            String path = this.getClass().getSimpleName() + "_" + methodName + "_" + dateStr + ".png";
            //因为我们截图是需要用到driver的,所以这里需要获取driver,这个driver是获取的当前对象的driver
            takeScreenShot((TakesScreenshot) this.getDriver(), path);
    
        }
    
        /**
         * 传入参数截图
         * */
        public void takeScreenShot(TakesScreenshot drivername, String path) {
            String currentPath = PropertyUtil.getString("screenPic_dir");
            File scrFile = drivername.getScreenshotAs(OutputType.FILE);
            try {
                FileUtils.copyFile(scrFile, new File(currentPath + "\" + path));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.error("<a href=" + currentPath + " target=_blank>Failed Screen Shot</a>");
                System.out.println("截图成功");
            }
        }
        public void setDriver(AndroidDriver<MobileElement> driver){
            this.driver = driver;
        }
    }

    2.TestngListenerScreen类

    package com.cmall.screenshot;
    
    import org.testng.ITestContext;
    import org.testng.ITestResult;
    import org.testng.TestListenerAdapter;
    
    public class TestngListenerScreen extends TestListenerAdapter
    {
        @Override
        public void onTestSuccess(ITestResult tr)
        {
            super.onTestSuccess(tr);
        }
    
        // 主要是用到这个方法了,当你报错时他会监听到,然后就会执行截图操作
        @Override
        public void onTestFailure(ITestResult tr)
        {
            super.onTestFailure(tr);
            System.out.println("####################################################");
            System.out.println(tr);
            System.out.println("####################################################");
            takeScreenShot(tr,tr.getMethod().getMethodName());//第二个参数表示哪个类产生的异常
        }
        @Override
        public void onTestSkipped(ITestResult tr) {
            super.onTestSkipped(tr);
        }
    
        @Override
        public void onTestStart(ITestResult result) {
            super.onTestStart(result);
        }
    
        @Override
        public void onStart(ITestContext testContext) {
            super.onStart(testContext);
        }
    
        @Override
        public void onFinish(ITestContext testContext) {
            super.onFinish(testContext);
        }
    
        private void takeScreenShot(ITestResult tr,String methodName) {
            DriverBase driverBase = (DriverBase) tr.getInstance();
            driverBase.takeScreenShot(methodName);
    
        }
    }

    3.Test1测试类

    package com.cmall.screenshot;
    
    import com.cmall.appium.DriverFactory;
    import com.cmall.appium.Helper;
    import com.cmall.appium.MultideviceManage;
    import com.cmall.http.LogUtil;
    import com.cmall.jdjr.pages.Modules.Integration.HomePage;
    import io.appium.java_client.MobileElement;
    import io.appium.java_client.android.AndroidDriver;
    import io.appium.java_client.pagefactory.AppiumFieldDecorator;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Listeners;
    
    import java.util.concurrent.TimeUnit;
    import org.testng.annotations.Test;
    
    @Listeners({ TestngListenerScreen.class })
    public class Test1 extends DriverBase
    {
        static AndroidDriver<MobileElement> mdriver = null;
        private LogUtil log = new LogUtil(Test.class);
        MultideviceManage m = new MultideviceManage();
        Helper helper;
        public Test1(){
            log.info("---------屏幕截图测试类---------------");
        }
        @Test
        public void test(){
            mdriver = DriverFactory.initDriver(4723,"TWGDU16B26001079");
            HomePage homePage = new HomePage();
            PageFactory.initElements(new AppiumFieldDecorator(mdriver, 20 , TimeUnit.SECONDS), homePage);
            // int a=1/0;//没预测到的--会截图
            try {
                Thread.sleep(2000);
                helper = new Helper(mdriver);
                helper.clickonElement(homePage.精选);
               // Assert.assertEquals(1,2);//会截图
                //throw new IllegalArgumentException("参数长度不是7位"); //不会截图
                int b=1/0;//能预测到被catch到的 不会截图
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

     4.配置xml文件

    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
    <suite name="TestngListenerScreen" verbose="1" >
    <listeners>
        <listener class-name="com.cmall.screenshot.TestngListenerScreen"></listener>
    </listeners>
    <test name = "Test" >
        <classes>
            <class name="com.cmall.screenshot.ScreenTest"/>
        </classes>
    </test>
    </suite>

    5.把xml文件配置在pom.xml里

     
  • 相关阅读:
    经典音乐插曲推荐![附地址]
    广播电台常用51首背景音乐——绝对经典
    酒吧..夜店常用歌曲~潮人必备音乐噢~【附下载地址】
    ASP与ASP.NET互通COOKIES的一点经验
    linux进程状态浅析
    常用的酒吧经典乐曲106首
    呼和浩特电视台媒资管理系统的设计与分析
    不再为DataGrid生成的表格的单无格中的内容过长、自动折行、表格撑开等问题而烦恼
    Linux shell脚本全面学习
    ASP.NET文件管理显示信息
  • 原文地址:https://www.cnblogs.com/liuwanqiu/p/7840284.html
Copyright © 2011-2022 走看看