(四)PC自动化测试框架之自定义框架介绍(二)--function篇(一)(java)

上一篇博客,已经介绍了自定义框架的第一组成部分,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) {

		}
}


如果你也搞定了,开心的同时请小编喝个咖啡也极好的呀。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43574761/article/details/89092024