zoukankan      html  css  js  c++  java
  • Selenium生成Report的利器- ExtentReports

    生成的报告最后的样子是:

    里面加了截图, 将每一步骤的截图,(所以方法里多加了一个截屏方法)。 加入到报告中,这样更清晰明了。

    首先 pom文件中需引用:
    <!-- 报告输出 -->
    <dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.1</version>
    </dependency>



    public  void runtest() throws InterruptedException {

    //生成报告
    ExtentReports extent=new ExtentReports("./demo.html", NetworkMode.OFFLINE);
    ExtentTest test = extent.startTest("智联卓聘测试报告,(*^__^*) ");
    try {
    driver=new ChromeDriver();
    String strUrl="http://c.highpin.cn/";
    Thread.sleep(2000);
    driver.get(strUrl);
    driver.manage().window().maximize();
    Thread.sleep(2000);
    driver.findElement(By.name("Logon_UserEmail")).clear();
    driver.findElement(By.name("Logon_UserEmail")).sendKeys("testzp@qq.com");
    test.log(LogStatus.PASS,"输入用户名"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"input_username.png")));
    driver.findElement(By.name("Logon_Password")).clear();
    driver.findElement(By.name("Logon_Password")).sendKeys("wxl1234567");
    test.log(LogStatus.PASS,"输入密码"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"input_password.png")));
    driver.findElement(By.id("Logon_PostCode")).clear();
    driver.findElement(By.id("Logon_PostCode")).sendKeys("1234");
    driver.findElement(By.cssSelector(".CLoginBtn")).click();
    test.log(LogStatus.PASS,"点击登录按钮"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"login_click.png")));
    Thread.sleep(5000);
    String imgPath=snapshot((TakesScreenshot)driver,"LoginPass.png");
    test.log(LogStatus.PASS,"登录成功"+"截图 -- " + test.addScreenCapture(imgPath)); //成功之后打印报告
    }catch (java.lang.Exception e)
    {
    e.printStackTrace();
    // 记录错误信息
    String imgPath=snapshot((TakesScreenshot)driver,"LoginFail.png");
    test.log(LogStatus.FAIL, "截图 -- " + test.addScreenCapture(imgPath));
    test.log(com.relevantcodes.extentreports.LogStatus.INFO, "截图 -- " + test.addScreenCapture(imgPath));
    }
    extent.endTest(test);
    extent.flush();
    extent.close();
    }

    //selenium 截屏方法
    public String snapshot(TakesScreenshot drivername, String filename)
    {
    // this method will take screen shot ,require two parameters ,one is driver name, another is file name
    String Screenshot=null;
    File scrFile=drivername.getScreenshotAs(OutputType.FILE);
    try{
    Screenshot=filename;
    FileUtils.copyFile(scrFile,new File(filename));
    }catch(IOException e){
    // TODO Auto-generated catch block
    System.out.println("Can't save screenshot");
    e.printStackTrace();
    }
    finally
    {
    return Screenshot;
    }
    }
    另一个截屏方法,写的比较好,我没有用这个方法,这个方法传的是三个参数 哈哈

    /**
    * @param driver -- 浏览器对象
    * @param screenShotName -- 截图的文件名
    * @return destImagePath -- 截图的存放路径
    * @Description: 屏幕截图方法(动态注入到测试类的方法)
    */
    public static String captureScreenShot(WebDriver driver, String reportDir, String screenShotName) {
    TakesScreenshot ts = (TakesScreenshot) driver;
    File sourceImage = ts.getScreenshotAs(OutputType.FILE);
    String destImagePath = reportDir + "/" + screenShotName + ".png";
    File destImage = new File(destImagePath);
    try {
    FileUtils.copyFile(sourceImage, destImage);
    } catch (IOException e) {
    e.printStackTrace();
    }
    destImagePath = screenShotName + ".png";
    return destImagePath;
    }

  • 相关阅读:
    centos 7安装libreoffice
    python3-xlwt-Excel设置(字体大小、颜色、对齐方式、换行、合并单元格、边框、背景、下划线、斜体、加粗)
    PHP导出身份证号科学计数法
    PHP接收json格式的POST数据
    微信小程序知识
    搭建Vue开发环境的步骤
    公众号认证?小程序认证?小程序复用公众号资质进行认证?
    七牛云——批量将本地图片上传到七牛云
    身份认证接口
    php二维数组去重
  • 原文地址:https://www.cnblogs.com/wxll/p/5949418.html
Copyright © 2011-2022 走看看