Java的selenium代码随笔(1)
package ShareClass;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
/*
* 此类主要用于编写操作Excel的方法
*/
public class ExcelUtil {
private static Sheet excelSheet;
private static Workbook excelWorkBook;
private static Cell cell;
private static Row row;
private static FileInputStream excelFile;
private static String filePath;
public ExcelUtil(String filePath) throws NoSuchFileException {
filePath = this.filePath;
}
/**
* 判断是否存在“数据读取配置”
*
* @return
* @throws IOException
*/
public List<String[]> SetExcelReadConfig() throws IOException {
List<String[]> excelReadConfigList = new ArrayList<String[]>();
try {
// 实例化Excel文件的FileInputStream对象
excelFile = new FileInputStream(filePath);
// 实例化Excel文件的Workbook对象
String FileExtensionName = filePath.substring(filePath.indexOf("."));
// 判断文件类型如果是.xlsx,则使用XSSFWorkBook对象进行实例化
// 判断文件类型如果是.xls,则使用SSFWorkBook对象进行实力化
if (FileExtensionName.equals(".xlsx")) {
excelWorkBook = new XSSFWorkbook(excelFile);
} else if (FileExtensionName.equals(".xls")) {
excelWorkBook = new HSSFWorkbook(excelFile);
}
for (int i = 0; i < excelWorkBook.getNumberOfSheets(); i++) {
if (excelWorkBook.getSheetAt(i).equals("数据读取配置")) {
excelSheet = excelWorkBook.getSheet("数据读取配置");
int rowNum = excelSheet.getLastRowNum() - excelSheet.getFirstRowNum();
for (int j = 1; j < rowNum; j++) {
String[] configList = new String[6];
row = excelSheet.getRow(j);
for (int k = 0; k < 6; k++) {
if (row.getCell(k).equals(null)) {
row.getCell(k).setCellValue("0");
} else {
configList[k] = row.getCell(k).getStringCellValue();
}
}
excelReadConfigList.add(configList);
}
return excelReadConfigList;
} else {
continue;
}
}
return null;
} catch (NoSuchFileException e) {
// TODO: handle exception
throw (e);
}
}
/*
* 此方法主要用于读取Excel中的数据
*/
public static String[][] excelRead(String sheetName, int startRowNum, int endRowNum, int startCellNum,
int endCellNum) throws IOException {
// 实例化Excel文件的FileInputStream对象
excelFile = new FileInputStream(filePath);
// 实例化Excel文件的Workbook对象
String FileExtensionName = filePath.substring(filePath.indexOf("."));
// 判断文件类型如果是.xlsx,则使用XSSFWorkBook对象进行实例化
// 判断文件类型如果是.xls,则使用SSFWorkBook对象进行实力化
if (FileExtensionName.equals(".xlsx")) {
excelWorkBook = new XSSFWorkbook(excelFile);
} else if (FileExtensionName.equals(".xls")) {
excelWorkBook = new HSSFWorkbook(excelFile);
}
excelSheet = excelWorkBook.getSheet(sheetName);
// 获取Excel数据文件Sheet中的数据行号
// getLastRowNum方法获取数据的的最后行号
// getFirstRowNum方法获取数据的第一行行号
// 相减后算出数据的行号
// 注意:Excel文件的行号和列号都是从0开始的
int rowCount = endRowNum - startRowNum + 1;
// 获取Excel数据文件中的列号
int cellCount = endCellNum - startCellNum + 1;
// 定义各一个二维数组去接收这个数据
String[][] excelDate = new String[rowCount][cellCount];
// 循环遍历获取数据
for (int i = 0; i < rowCount; i++) {
row = excelSheet.getRow(startRowNum);
for (int j = 0; j < cellCount; j++) {
excelDate[i][j] = (String) (row.getCell(startCellNum).getCellTypeEnum() == CellType.STRING
? row.getCell(startCellNum).getStringCellValue()
: "" + row.getCell(startCellNum).getNumericCellValue());
}
startRowNum++;
}
return excelDate;
}
/*
* 此方法主要用于将测试结果写入到Excel中
*/
public static void excelWrite(int rowNum, int cellNum, String result) throws Exception {
try {
// 获取Excel文件中的行对象
int rowNumChange = rowNum + 2;
row = excelSheet.getRow(rowNumChange);
// 如果单元格为空
if (cell == null) {
// 单元格对象是Null的时候,创建单元格
// 如果单元格为空,无法直接调用单元格对象的setCellValue方法设定的单元格的值
cell.setCellValue(result);
} else {
// 单元格中有内容,则可以直接调用单元格对象的setCellValue方法设定单元格的值
cell.setCellValue(result);
}
// 实例化写入Excel文件的文件数出流对象
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
// 将内容写入Excel文件
excelWorkBook.write(fileOutputStream);
// 调用flush方法强制刷新写入文件
fileOutputStream.flush();
// 关闭文件输出流
fileOutputStream.close();
} catch (Exception e) {
// TODO: handle exception
throw (e);
}
}
}
Java的selenium代码随笔(1)的更多相关文章
- Java的selenium代码随笔(8)
Selenium截图方法一: Selenium中截图类TakeScreenshout,这个类主要是获取浏览器窗体内的内容,不包括浏览器的菜单和桌面的任务栏区域,我们用百度首页来截图,看看截图效果. F ...
- Java的selenium代码随笔(5)
//以下七种方法主要用于生成年.月.日.小时.分钟和秒的信息,用于生成保存截图的文件目录名和文件名/** 格式化输出日期* * @return 返回字符型日期*/public static Strin ...
- Java的selenium代码随笔(2)
import java.awt.AWTException;import java.awt.Robot;import java.awt.Toolkit;import java.awt.datatrans ...
- Java的selenium代码随笔(7)
//判断元素是否存在public boolean IsElementPresent (WebElement webElement, By by) { boolean status = false; t ...
- Java的selenium代码随笔(6)
//获取元素列表public List<WebElement> ListElements(WebElement webElement, By parentBy, By childrenBy ...
- Java的selenium代码随笔(4)
//高亮操作元素public void highlight(WebElement webElement) {JavascriptExecutor javascriptExecutor = (Javas ...
- Java的selenium代码随笔(3)
/** 以下方法主要用于切换页面*/public void SetPageSwitch(String pageTitle) {Set<String> allWindowsHandles = ...
- Java使用Selenium几个例子
零.姿势 Selenium分为两个版本:Selenium RC和Selenium Webdriver.现在用Selenium Webdriver比较多. Selenium是一套工具,而不仅仅是一个操纵 ...
- 正则表达式学习笔记(附:Java版示例代码)
具体学习推荐:正则表达式30分钟入门教程 . 除换行符以外的任意字符\w word,正常字符,可以当做变量名的,字母.数字.下划线.汉字\s space,空白符 ...
随机推荐
- 微服务定义及.Net Core中用的技术
微服务 定义: 它是一种架构模式,提倡将大的单体系统,按业务拆分成一个个较小且独立的服务,服务与服务之前进行相互协作和配合. 历史: 针对互联网行业的蓬勃发展,需要支撑的业务越来越多,越来越大,单体程 ...
- struct的匿名用法详解
Go只提供类型而不用写字段名的方式,也就是匿名字段,也称为嵌入字段. 当匿名字段是一个struct的时候,那么这个struct所拥有的全部字段都被隐式地引入了当前定义的这个struct. 举个例子,看 ...
- JAVA程序员面试30问(附带答案)
第一,谈谈final, finally, finalize的区别. 最常被问到.final修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能 ...
- SpringBoot 之Actuator.
一.Actuator 介绍 Actuator 是 SpringBoot 项目中一个非常强大一个功能,有助于对应用程序进行监视和管理,通过 restful api 请求来监管.审计.收集应用的运行情况. ...
- HTML5跳转页面并传值以及localStorage的用法
1.首先,你得在那个页面把数据存入localStorage中吧.这个是必须的! localStorage.setItem("user",JSON.stringify(data.al ...
- html初步学习
①:<meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,minimum-scale ...
- Dynamics CRM教程:图表的Top设置及导出修改和导入
关注本人微信和易信公众号: 微软动态CRM专家罗勇,回复144或者20150412可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 上一篇博客制作的图表放在Dashb ...
- [python爬虫]Requests-BeautifulSoup-Re库方案--Requests库介绍
[根据北京理工大学嵩天老师“Python网络爬虫与信息提取”慕课课程编写 文章中部分图片来自老师PPT 慕课链接:https://www.icourse163.org/learn/BIT-10018 ...
- Android 手机连不上电脑
[个人经验] 给大家分享一下,最近Android开发中一个坑. 在Android开发中,有时会需要自己开发服务端,就需要连接自己的电脑. ①首先,我们得知道我们电脑的ip地址是多少: 开始菜单---- ...
- c/c++ 重载运算符 标准库function的用法
重载运算符 标准库function的用法 问题:int(int, int)算不算一种比较通用的类型?? 比如函数: int add(int a, int b); 比如lambda:auto mod = ...