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

1.首先我们得用例写好之后放入文档中,把不用的案例类型、前置条件去掉之后,如图:
java自动化将用例和截图一起执行测试放入world中直接生成测试报告【搬代码】_第1张图片
放到桌面后,先看执行结果:
java自动化将用例和截图一起执行测试放入world中直接生成测试报告【搬代码】_第2张图片

直接上代码

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");//设置标题2
            p.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");//设置标题2
                paragraph1.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;
    }
}


你可能感兴趣的:(java,自动化,开发语言)