1、使用maven配置依赖包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>ceshi</groupId> 8 <artifactId>puhui</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <build> 11 <plugins> 12 <plugin> 13 <groupId>org.apache.maven.plugins</groupId> 14 <artifactId>maven-compiler-plugin</artifactId> 15 <configuration> 16 <source>1.6</source> 17 <target>1.6</target> 18 </configuration> 19 </plugin> 20 </plugins> 21 </build> 22 23 <dependencies> 24 25 <!-- selenium-java --> 26 <dependency> 27 <groupId>org.seleniumhq.selenium</groupId> 28 <artifactId>selenium-java</artifactId> 29 <version>3.4.0</version> 30 </dependency> 31 32 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> 33 <dependency> 34 <groupId>org.apache.poi</groupId> 35 <artifactId>poi</artifactId> 36 <version>3.9</version> 37 </dependency> 38 39 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> 40 <dependency> 41 <groupId>org.apache.poi</groupId> 42 <artifactId>poi-ooxml</artifactId> 43 <version>3.9</version> 44 </dependency> 45 </dependencies> 46 47 48 </project>
2、配置当前项目配置文件config.properties
3、代码分析
package com.hsjry.test; /** *自动化测试脚本 * *@create create on 2019/9/6 17:30 */ import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebElement; import java.io.*; import java.util.List; import java.util.Properties; import java.util.concurrent.TimeUnit; public class zidonghua { public static String tel; public static void main(String[] args) throws InterruptedException, IOException { //等待1秒 Thread.sleep(1000); WebDriver driver =new ChromeDriver(); //最大化窗口 driver.manage().window().maximize(); // 与浏览器同步非常重要,必须等待浏览器加载完毕 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //下面开始元素操作,打开网址 driver.get("http://10.53.XXX.XXX:8082/RiskEngine/logon"); //输入账号和密码,点击登陆 driver.findElement(By.xpath("/html/body/div[1]/div/div/form/input[1]")).sendKeys("admin"); //输入用户名 driver.findElement(By.xpath("/html/body/div[1]/div/div/form/input[2]")).sendKeys("admin"); //输入密码 driver.findElement(By.id("login-button")).click(); //打开决策控制台 driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/ul/li[2]/a/cite")).click(); driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/ul/li[2]/dl/dd[1]/a/cite")).click(); driver.switchTo().frame(driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/iframe"))); //driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/div/div[2]/iframe"); //读取本地文件内的手机号,获取当前工程下的properties文件 Properties p = new Properties(); p.load(zidonghua.class.getClassLoader().getResourceAsStream("config" + ".properties")); tel=p.getProperty("Tel"); driver.findElement(By.xpath("//*[@id="mobile1"]")).sendKeys(tel); driver.findElement(By.id("test1")).click(); //切换表单到输出表 driver.switchTo().frame(driver.findElement(By.xpath("//*[@id="layui-layer-iframe100001"]"))); Thread.sleep(1000); //查看变量 driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[6]/a[1]")).click(); //获取事件ID和事件类型 String text1=driver.findElement(By.xpath ("/html/body/table/tbody/tr[1]/td[1]/a[1]")).getText();//事件ID String text2=driver.findElement(By.xpath ("/html/body/table/tbody/tr[1]/td[3]")).getText();//事件类型 //Thread.sleep(1000); driver.switchTo().parentFrame();//返回上一层iframe driver.switchTo().frame(driver.findElement(By.xpath ("//*[@id="layui-layer-iframe100002"]")));//切换到当前iframe WebElement table= driver.findElement(By.xpath ("//*[@id="wrap"]/div[1]/table"));//获取当前table对象 WebElement rows1=table.findElement(By.tagName("tr")); WebElement cols1=rows1.findElement(By.tagName("td")); //获取表格元素的行数,查找表格元素有几个tr元素,有几个tr元素,就可以知道表格有几行,tr数量和表格行数相一致 List<WebElement> rows = table.findElements(By.tagName("tr")); FileInputStream fis = new FileInputStream("D:\test.xlsx");//创建输入流,获取本地文件 XSSFWorkbook workbook=new XSSFWorkbook(fis); //创建工作簿,将数据读入到workbook中 XSSFSheet sheet1 = workbook.getSheet("Sheet1"); XSSFSheet sheet2 = workbook.getSheet("Sheet2"); //读取第一个sheet页,页名为Sheet0,也可以用getSheetAt(0),第一个sheet页 int rowNum=sheet1.getLastRowNum();//获取表格总行数,并赋值给a System.out.println(rowNum); System.out.println(rows.size()); long beginTime=System.currentTimeMillis(); //获取开始时间 //获取每一行是数据并写进表格 for(int h=0;h<=rows.size();h++){ String row=rows.get(h).getText(); sheet2.createRow(h).createCell(0).setCellValue(row); } //获取sheet1页每行第一列和第二列的值 for(int i=0;i<rowNum;i++){ String AS = sheet1.getRow(i).getCell(0).getStringCellValue(); String BS =sheet1.getRow(i).getCell(1).getStringCellValue(); //获取sheet2页每行第一列的值,如果sheet2页内容包含sheet1页内容,则写进sheet1页 for (int j=0;j<=rows.size();j++){ String row1=sheet2.getRow(j).getCell(0).getStringCellValue(); if(row1.contains(AS)&&row1.contains(BS)){ sheet1.getRow(i).createCell(2).setCellValue(row1); } } } /** for(int i=0;i<=rowNum;i++) { String AS = sheet.getRow(i).getCell(0).getStringCellValue(); if (html.contains(AS)) { //System.out.println(aaa); sheet.getRow(i).createCell(2).setCellValue(cols.get(2).getText().toString());//创建第三列,赋值变量中文名 sheet.getRow(i).createCell(3).setCellValue(cols.get(3).getText().toString());//创建第四列,赋值变量值 break; } sheet.getRow(0).getCell(0).setCellValue("会话ID"+text1);//获取会话ID sheet.getRow(0).getCell(2).setCellValue("交互类型"+text2);//获取交互类型 } */ long endTime=System.currentTimeMillis();//获取结束时间 System.out.println("当前耗时:"+(endTime-beginTime)/1000+"s"); String name =text2+text1; //作为新excel文件的名字 FileOutputStream os = new FileOutputStream("D:\"+name+".xlsx");//创建一个向指定位置写入文件的输出流 workbook.write(os);//向指定的文件写入excel os.close();//关闭流 driver.close();//关闭浏览器 } }