我有一个
global.properties
文件,必须在此属性文件中定义文件路径。
SheetPath=C:\\Users\\test\\Automation-Scripts\\DataTable.xlsx
这是一个绝对路径,但需要一种方法来定义可以在调用时使用的相对路径。
属性文件:
testPath=API_Files/duplicateToken.json
加载属性文件:
public static Properties readProperties = new Properties();
public static void loadPropertiesFile() {
File propertiesFile = new File(location of properties file);
try {
FileInputStream fileInput = new FileInputStream(propertiesFile);
readProperties.load(fileInput);
} catch (Exception e) {
Logger.LogError("Error in loading the Properties file" + e.getMessage());
}
}
读取属性文件并获取绝对路径:
String testPath = readProperties.getProperty("testPath").trim();
File absolutePath = new File(System.getProperty("user.dir") + testPath);
System.out.println(absolutePath);
示例输出:
C:\Users\test\Automation-Scripts\duplicateToken.json
属性文件应该相对于类路径。您可以在 src 级别创建一个“configs”文件夹并使用以下代码读取该文件。请参阅this了解更多解释和技巧。
private Properties properties;
private final String propertyFilePath= "configs//Configuration.properties";
BufferedReader reader = new BufferedReader(new FileReader(propertyFilePath));
Properties properties = new Properties();
properties.load(reader);
您可以在测试中使用ResourceBundle或FileInputStream类 方法一: ResourceBundle rb=ResourceBundle.getBundle("config"); String br=rb.getString("浏览器");
方法2:
File src = new File(".\\resources\\config.properties");
FileInputStream fis = new FileInputStream(src);
Properties pro = new Properties();
pro.load(fis);
string br = pro.getProperty("browser");