上一篇博客,已經介紹了自定義框架的第一組成部分,data篇,數據提取,不知道大家get其中的好處了沒有,有問題歡迎評論諮詢。
今天,我們就來介紹自定義框架最重要的一個組成部分,function篇。(自動化的常用方法都進行了封裝)
這裏只介紹幾個常用的方法。
1:BasicFunction
- newDirectory
新建文件夾,我這裏使用這個方法主要是將測試結果截圖保存在以當天日期命名的文件夾內。
// 新建文件夾
public static String newDirectory() {
SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd");// 設置日期格式
String datetime = df.format(new Date());
String file = System.getProperty("user.dir");// 用戶當前工作路徑
file = file + "\\src\\test_result\\" + datetime;
File filepath = new File(file);
if (filepath.exists()) {
// System.out.println("文件夾已存在!");
} else {
filepath.mkdirs();
// System.out.println("文件夾新建成功!");
}
return file;
}
- dateString
年月日時分秒組成的數字,不會出現重複數據
// 時間的形式,精確到秒,則隨機數不會
public static String dateString() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 設置日期格式
String number = df.format(new Date());
return number;
}
- randomInt
隨機數的生成,使用這個函數的時候,在調用的時候傳入一個最大值max,這個值代表你想要生成0-這個max之間的隨機數。
// 隨機數
public static int randomInt(int max) {
Random random = new Random();
int number = random.nextInt(max); // 表示生成[0,max]之間的隨機數
// System.out.println(number);
return number;
}
- sleep
這個方法就是等待時間,可封裝可不封裝,等同於這個方法
// 等待時間,單位是ms
public static void sleep(int time) throws InterruptedException {
Thread.sleep(time);
}
有疑問的加V瞭解詳情:zx1187463903
2:UserFunction
這個方法放在下一篇博客裏,因爲涉及的方法比較多。
3:WriteOrReadFile
這個類裏面包括三個方法
1:讀文件內容
2:寫入內容
3:刪除文件夾裏的所有文件(不確定數量)
package function;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
/**
* @author
*
*/
public class WriteOrReadFile {
public static void writeIntoFile(String filePath, String value) {
File file = new File(filePath);
if (file.exists() && file.isFile()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileOutputStream out;
try {
out = new FileOutputStream(file, true);
out.write(value.getBytes("utf-8"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String readValueFromFile(String filePath) {
StringBuffer sb = new StringBuffer();
File file = new File(filePath);
if (!file.exists() || file.isDirectory()) {
System.out.println("文件不存在!");
}
FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] buf = new byte[1024];
while ((fis.read(buf)) != -1) {
sb.append(new String(buf));
buf = new byte[1024];//
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath);
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static boolean delAllFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);
delFolder(path + "/" + tempList[i]);
flag = true;
}
}
return flag;
}
public static void main(String[] args) {
}
}
如果你也搞定了,開心的同時請小編喝個咖啡也極好的呀。