- 在写自动化测试用例时,需要从配置文件中读取数据,首先在maven项目中建立一个.properties文件。
文件内数据的格式为key=value
格式:
username=name>email
userpass=name>password
package com.sheehan.second_maven;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ProUtil{
private Properties prop;
private String filePath;
public ProUtil(String filePath) throws IOException {
this.filePath = filePath;
this.prop = readProperties();
}
private Properties readProperties() throws IOException {
Properties prop = new Properties();
InputStream inputStream = new FileInputStream(filePath);
BufferedInputStream in = new BufferedInputStream(inputStream);
prop.load(in);
return prop;
}
public String getPro(String key){
if(prop.contains(key)) {
String username = prop.getProperty(key);
return username;
}else {
System.out.println("error");
return "";
}
}
public static void main(String[] args) throws Exception {
ProUtil properties = new ProUtil("element.properties");
String locator = properties.getPro("username");
String locatorType = locator.split(">")[0];
String locatorValue = locator.split(">")[1];
}
}