java自动化将用例和截图一起执行测试放入world中直接生成测试报告【搬代码】

news/2024/7/27 8:14:56/文章来源:https://blog.csdn.net/weixin_52001174/article/details/135572355

1.首先我们得用例写好之后放入文档中,把不用的案例类型、前置条件去掉之后,如图:
1
放到桌面后,先看执行结果:
在这里插入图片描述

首先,我们先创建一个时间,这个时间主要是给图片创建名称,并且要在插入world中使用该时间去查找对应的图片名称,且该图片名称是唯一值

  //创建时间public static String times(){Date date = new Date();String format = String.format("%tF", date);String hour = String.format("%tH", date);String mintus = String.format("%tM", date);String secound = String.format("%tS", date);String t=format+hour+mintus+secound;return t;}

其次,我们就要创建一个截图的方法,截图主要用于我们对结果的记录,并且保存到对应的文件夹中,方便时间值来查找到并使用它

   //截图放起来,并且返回一个截图时间public static String  Screen(ChromeDriver driver) throws IOException {File file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);//直接添加E:/idear/idearxm/jiekou,前面没有。。String times = times();FileUtils.copyFile(file,new File("E:/idear/idearxm/jiekou/test-output/images/"+times+".png"));driver.quit();return times;//把截图时间也返回去}

然后我们就要创建测试用例的代码,执行测试用例,当然我们在这里不是讲解selenium的断言等,主要是使用截图,来创建自动化测试文档给其中放置图片使用。
其实我们在编写selenium的代码时,完全可以将每一个步骤编写成对应的方法,写在一个文件内,等待调用。因为这样我们可以避免代码重复堆叠,最后形成庞大的屎山代码,但是屎山代码完全可以让我们在这个公司越来越稳,后面准备写一篇如何正确使用屎山代码,并且保留bug的文章,和httpclient的自动化测试报告

//这里是编写测试用例执行代码地方public static void test002() throws IOException, InvalidFormatException, InterruptedException {ChromeDriver driver = new ChromeDriver();System.setProperty("webdriver.chrome.bin","C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");System.setProperty("webdriver.chrome.driver","C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");driver.get("http://www.baidu.com");Thread.sleep(5000);String screen = Screen(driver);//调用方法,将截图的时间获取到,放入插入图片那里//        创建一个段落对象。XWPFParagraph paragraph=document.createParagraph();
//        创建一个run。run具体是什么,我也不知道。但是run是这里面的最小单元了。XWPFRun run=paragraph.createRun();
//        插入图片,将screen时间截图的时间获取到,放入插入图片这里run.addPicture(new FileInputStream("E:/idear/idearxm/jiekou/test-output/images/"+screen+".png"),XWPFDocument.PICTURE_TYPE_PNG,screen+".png",Units.toEMU(400),Units.toEMU(200));}

最后呢,我们创建表并且将上述的代码带入其中,在其中放置图片使用

public static final XWPFDocument document = new XWPFDocument();public static void main(String[] args) {try{// 添加段落内容,也就是标题XWPFParagraph p = document.createParagraph();p.setStyle("标题4");//设置标题2p.setFontAlignment(2);//字体对齐方式:1左对齐 2居中3右对齐XWPFRun run = p.createRun();run.setText("xxx测试报告");//添加标题//urlexcel,这个地址是你用例的地址String path="D:\\桌面\\Eworld1.xls";//解析路径Workbook workbook=Workbook.getWorkbook(new File(path));//获取第一张表Sheet sheet = workbook.getSheet(0);//循环获取第一行数据,因为默认第一行为标题行,我们可以从1开始循环,如果需要读取标题行,从0开始for (int i = 1; i <sheet.getRows() ; i++) {//获取第一行的第i行信息 sheet.getcell(列,行);下标从0开始String name = sheet.getCell(0, i).getContents();System.out.println("标题::"+name);//获取第二行的第i行信息String miaoshu = sheet.getCell(1,i).getContents();System.out.println("描述::"+miaoshu);//获取第三行的第i行信息String buzhou = sheet.getCell(2,i).getContents();System.out.println("步骤:"+buzhou);//获取第四行的第i行信息String yuqi = sheet.getCell(3,i).getContents();System.out.println("预期结果:"+yuqi);System.out.println("===========================");//将获取到每行的内容放到数组中String data[]={name,miaoshu,buzhou,yuqi};// 添加段落内容,也就是标题XWPFParagraph paragraph1 = document.createParagraph();paragraph1.setStyle("标题4");//设置标题2paragraph1.setFontAlignment(1);//字体对齐方式:1左对齐 2居中3右对齐XWPFRun run1 = paragraph1.createRun();run1.addBreak();//换行run1.setText(data[0]);//添加第一行段落文本// 添加表格 1行3列XWPFTable table = document.createTable(1,3);// 设置表格的行高和列宽table.setWidth(9000);// 设置列宽table.getRow(0).getCell(0).setWidth("3000");//设置获取第一行1列设置宽度table.getRow(0).getCell(1).setWidth("3000");//设置获取第一行2列设置宽度table.getRow(0).getCell(2).setWidth("3000");//设置获取第一行3列设置宽度int rowCount = 1;int colCount = 3;for (int a = 0; a < rowCount; a++) {XWPFTableRow row = table.getRow(a);if (row == null) {row = table.createRow();}for (int j = 0; j < colCount; j++) {XWPFTableCell cell = row.getCell(j);if (cell == null) {cell = row.addNewTableCell();}if (j==0){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[1]);}else if (j==1){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[2]);}else if (j==2){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[3]);}}}
//放测试截图//放测试用例代码,一张完美的自动化测试报告就产生了if (data[0].equals("修改商户费率-001")){//TimesUntil.Screen();test002();}else if (data[0].equals("修改商户费率-002")){test002();}}// 保存为Word文件FileOutputStream outStream = new FileOutputStream("output.docx");document.write(outStream);outStream.close();System.out.println("成功生成Word文件!");}catch (Exception e){e.printStackTrace();}}

直接上全部代码

package com.znzdh.qitagongju;import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;public class Eworld3 {public static Logger log= Logger.getAnonymousLogger();//目前在其中并没有使用打印日志// 创建新的Word文档对象public static final XWPFDocument document = new XWPFDocument();public static void main(String[] args) {try{// 添加段落内容,也就是标题XWPFParagraph p = document.createParagraph();p.setStyle("标题4");//设置标题2p.setFontAlignment(2);//字体对齐方式:1左对齐 2居中3右对齐XWPFRun run = p.createRun();run.setText("xxx测试报告");//添加标题//urlexcel,这个地址是你用例的地址String path="D:\\桌面\\Eworld1.xls";//解析路径Workbook workbook=Workbook.getWorkbook(new File(path));//获取第一张表Sheet sheet = workbook.getSheet(0);//循环获取第一行数据,因为默认第一行为标题行,我们可以从1开始循环,如果需要读取标题行,从0开始for (int i = 1; i <sheet.getRows() ; i++) {//获取第一行的第i行信息 sheet.getcell(列,行);下标从0开始String name = sheet.getCell(0, i).getContents();System.out.println("标题::"+name);//获取第二行的第i行信息String miaoshu = sheet.getCell(1,i).getContents();System.out.println("描述::"+miaoshu);//获取第三行的第i行信息String buzhou = sheet.getCell(2,i).getContents();System.out.println("步骤:"+buzhou);//获取第四行的第i行信息String yuqi = sheet.getCell(3,i).getContents();System.out.println("预期结果:"+yuqi);System.out.println("===========================");//将获取到每行的内容放到数组中String data[]={name,miaoshu,buzhou,yuqi};// 添加段落内容,也就是标题XWPFParagraph paragraph1 = document.createParagraph();paragraph1.setStyle("标题4");//设置标题2paragraph1.setFontAlignment(1);//字体对齐方式:1左对齐 2居中3右对齐XWPFRun run1 = paragraph1.createRun();run1.addBreak();//换行run1.setText(data[0]);//添加第一行段落文本// 添加表格 1行3列XWPFTable table = document.createTable(1,3);// 设置表格的行高和列宽table.setWidth(9000);// 设置列宽table.getRow(0).getCell(0).setWidth("3000");//设置获取第一行1列设置宽度table.getRow(0).getCell(1).setWidth("3000");//设置获取第一行2列设置宽度table.getRow(0).getCell(2).setWidth("3000");//设置获取第一行3列设置宽度int rowCount = 1;int colCount = 3;for (int a = 0; a < rowCount; a++) {XWPFTableRow row = table.getRow(a);if (row == null) {row = table.createRow();}for (int j = 0; j < colCount; j++) {XWPFTableCell cell = row.getCell(j);if (cell == null) {cell = row.addNewTableCell();}if (j==0){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[1]);}else if (j==1){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[2]);}else if (j==2){XWPFParagraph cellPara = cell.getParagraphArray(0);XWPFRun cellRun = cellPara.createRun();cellRun.setText(data[3]);}}}
//放测试截图//放测试用例代码,一张完美的自动化测试报告就产生了if (data[0].equals("修改商户费率-001")){//TimesUntil.Screen();test002();}else if (data[0].equals("修改商户费率-002")){test002();}}// 保存为Word文件FileOutputStream outStream = new FileOutputStream("output.docx");document.write(outStream);outStream.close();System.out.println("成功生成Word文件!");}catch (Exception e){e.printStackTrace();}}//这里是编写测试用例执行代码地方public static void test002() throws IOException, InvalidFormatException, InterruptedException {ChromeDriver driver = new ChromeDriver();System.setProperty("webdriver.chrome.bin","C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");System.setProperty("webdriver.chrome.driver","C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");driver.get("http://www.baidu.com");Thread.sleep(5000);String screen = Screen(driver);//调用方法,将截图的时间获取到,放入插入图片那里//        创建一个段落对象。XWPFParagraph paragraph=document.createParagraph();
//        创建一个run。run具体是什么,我也不知道。但是run是这里面的最小单元了。XWPFRun run=paragraph.createRun();
//        插入图片,将screen时间截图的时间获取到,放入插入图片这里run.addPicture(new FileInputStream("E:/idear/idearxm/jiekou/test-output/images/"+screen+".png"),XWPFDocument.PICTURE_TYPE_PNG,screen+".png",Units.toEMU(400),Units.toEMU(200));}//截图放起来,并且返回一个截图时间public static String  Screen(ChromeDriver driver) throws IOException {File file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);//直接添加E:/idear/idearxm/jiekou,前面没有。。String times = times();FileUtils.copyFile(file,new File("E:/idear/idearxm/jiekou/test-output/images/"+times+".png"));driver.quit();return times;//把截图时间也返回去}//创建时间public static String times(){Date date = new Date();String format = String.format("%tF", date);String hour = String.format("%tH", date);String mintus = String.format("%tM", date);String secound = String.format("%tS", date);String t=format+hour+mintus+secound;return t;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_926070.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【LabVIEW FPGA入门】没有CompactRIO时进行编程测试

1.新建一个空白项目。 2.新建cRIO终端。 要添加仿真的远程实时目标&#xff0c;请选择项目名称&#xff0c;右击并选择新建>>目标和设备(Targets and Devices)。 3.新建终端和设备&#xff0c;选一个cRIO型号 接下来&#xff0c;当添加目标和设备窗口出现时&#xff0c;请…

【数据结构】常见八大排序算法总结

目录 前言 1.直接插入排序 2.希尔排序 3.选择排序 4.堆排序 5.冒泡排序 6.快速排序 6.1Hoare版本 6.2挖坑法 6.3前后指针法 6.4快速排序的递归实现 6.5快速排序的非递归实现 7.归并排序 8.计数排序&#xff08;非比较排序&#xff09; 9.补充:基数排序 10.总结…

Jmeter 性能-监控服务器

Jmeter监控Linux需要三个文件 JMeterPlugins-Extras.jar (包&#xff1a;JMeterPlugins-Extras-1.4.0.zip) JMeterPlugins-Standard.jar (包&#xff1a;JMeterPlugins-Standard-1.4.0.zip) ServerAgent-2.2.3.zip 1、Jemter 安装插件 在插件管理中心的搜索Servers Perform…

散列函数,哈希表hash table

附上一句话&#xff1a;我知道大家可能曾经了解过这个散列表了&#xff0c;我发现&#xff0c;如果多看几个相关的视频&#xff0c;从不同的表述方式和不同的理解角度来理解这个问题&#xff0c;我会明白的更透彻&#xff0c;也有更多新的收获&#xff0c;尤其是对这个算法的应…

宁夏银行关键系统基于OceanBase的创新实践

宁夏银行成立于 1998 年&#xff0c;是宁夏第一家“宁”字号地方商业银行&#xff0c;西部地区第一家以省级行政区命名的地方商业银行。2016 年&#xff0c;被中国人民银行评为宁夏地区系统性重要银行。目前&#xff0c;全行设分支机构 97 家&#xff0c;其中总行营业部 1 家&a…

制造工厂ERP系统:从数字销售-生产到财务管理,掌握企业数字化十大核心!

在快速发展的数字化时代&#xff0c;企业&#xff08;尤其是传统生产制造行业&#xff09;面临着诸多挑战与机遇。无论是客户体验、供应链管理还是内部流程优化&#xff0c;数字化都在发挥着关键作用。为了更好地应对数字化带来的挑战和机遇为了更好地应对市场变化和提高竞争力…

打造高品质家具的必选!数控开料机为何备受推崇?

随着科技的不断进步&#xff0c;数控开料机已经成为了木材加工行业中的首选设备。 一、数控开料机在木材加工行业中的优势 高效、精准的加工效果 数控开料机采用高精度的数控技术和高功率的机械传动系统&#xff0c;可以实现对木材的精确开料和高效加工。与传统的手工操作相…

【RabbitMQ】RabbitMQ高级:死信队列和延迟队列

目录 设置TTL&#xff08;过期时间&#xff09;概述RabbitMQ使用TTL原生API案例springboot案例 死信队列概述原生API案例springboot案例 延迟队列概述插件实现延迟队列安装插件代码 TTL实现延迟队列实现延迟队列优化 设置TTL&#xff08;过期时间&#xff09; 概述 在电商平台…

51单片机-电子密码锁

实物演示效果&#xff1a; https://www.bilibili.com/video/BV1xh4y1K7uV/?vd_source6ff7cd03af95cd504b60511ef9373a1d 电子密码锁的主要功能 1.按键设置6位密码&#xff0c;输入密码若密码正确&#xff0c;则锁打开。显示open&#xff01; 2.密码可以自己修改&#xff0…

Ubuntu 22.04安装使用easyconnect

EasyConnect 百度百科&#xff0c;EasyConnect能够帮助您在办公室之外使用公司内网的所有系统及应用。在您的公司部署深信服远程应用发布解决方案后&#xff0c;您的公司所有业务系统及应用都可以轻松迁移至移动互联网上。您可以通过手机、PAD等智能移动终端随时随地开展您的业…

【Leetcode】82. 删除排序链表中的重复元素 II

文章目录 题目思路代码 题目 82. 删除排序链表中的重复元素 II 题目&#xff1a;给定一个已排序的链表的头 head &#xff0c; 删除原始链表中所有重复数字的节点&#xff0c;只留下不同的数字 。返回 已排序的链表 。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,3,4,…

带你解析git的基础功能(二)

文章目录 一.前言二.什么是git的分支管理三.git的分支管理的相关操作3.1 创建分支3.2 切换分支3.3 合并分支 和合并冲突3.4 删除分支 四.分支管理策略第一种分支策略第二种分支策略 总结 一.前言 掌握 Git 分⽀管理&#xff0c;从分⽀创建&#xff0c;切换&#xff0c;合并&am…

Visual Studio 2019 ctrl+f 呼出查找和替换窗口

有时候 ctrlshiftf 呼出查找和替换窗口不起作用&#xff0c;可能和其它程序的快捷键冲突&#xff0c;解决方案&#xff1a; ------------英文版本------------ 依次点击VS菜单栏中的 Tools - Options - Environment - Keyboard: 1. 在右侧的 Show commands containing: 文本框输…

【深度学习】RTX2060 2080如何安装CUDA,如何使用onnx runtime

文章目录 如何在Python环境下配置RTX 2060与CUDA 101. 安装最新的NVIDIA显卡驱动2. 使用conda安装CUDA Toolkit3. 验证onnxruntime与CUDA版本4. 验证ONNX需求版本5. 安装ONNX与onnxruntime6. 编写ONNX推理代码 如何在Python环境下配置RTX 2060与CUDA 10 RTX 2060虽然是一款较早…

Android PendingIntent 闪退

先来给大家推荐一个我日常会使用到的图片高清处理在线工具&#xff0c;主要是免费&#xff0c;直接白嫖 。 有时候我看到一张图片感觉很不错&#xff0c;但是图片清晰度不合我意&#xff0c;就想有没有什么工具可以处理让其更清晰&#xff0c; 网上随便搜下就能找到&#xff…

activiti流程图+动态表单

使用技术 jeecg-bootactivitivue3form-create 简单效果展示 流程图绘制 审批人配置 动态表单配置 流程审批 流程审批记录 填写表单信息 源码地址 后台&#xff1a;https://gitee.com/houshixin/jmg-boot前端&#xff1a;https://gitee.com/houshixin/jmg-ui

Pandas加载大数据集

Scaling to large datasets — pandas 2.1.4 documentationhttps://pandas.pydata.org/docs/user_guide/scale.html#use-efficient-datatypes官方文档提供了4种方法&#xff1a;只加载需要的列、转化数据类型、使用chunking&#xff08;转化文件存储格式&#xff09;、使用Dask…

计算机导论06-人机交互

文章目录 人机交互基础人机交互概述人机交互及其发展人机交互方式人机界面 新型人机交互技术显示屏技术跟踪与识别&#xff08;技术&#xff09;脑-机接口 多媒体技术多媒体技术基础多媒体的概念多媒体技术及其特性多媒体技术的应用多媒体技术发展趋势 多媒体应用技术文字&…

DataXCloud部署与配置[智数通]

静态IP设置 # 修改网卡配置文件 vim /etc/sysconfig/network-scripts/ifcfg-ens33# 修改文件内容 TYPEEthernet PROXY_METHODnone BROWSER_ONLYno BOOTPROTOstatic IPADDR192.168.18.130 NETMASK255.255.255.0 GATEWAY192.168.18.2 DEFROUTEyes IPV4_FAILURE_FATALno IPV6INIT…

bash shell基础命令(一)

1.shell启动 shell提供了对Linux系统的交互式访问&#xff0c;通常在用户登录终端时启动。系统启动的shell程序取决于用户账户的配置。 /etc/passwd/文件包含了所有用户的基本信息配置&#xff0c; $ cat /etc/passwd root:x:0:0:root:/root:/bin/bash ...例如上述root账户信…