无法读取 aws 设备场中的属性文件 [英] Can't read properties file in aws device farm
问题描述
我的 Appium + JUnit 测试在本地运行良好,但在 aws 上找不到属性文件.我的测试放在 src/test/java
下,测试中使用的属性文件放在 src/test/resources/locale
下.带有依赖项内容的压缩包:
├── app-0.1-tests.jar├── app-0.1.jar└── 依赖jars├── ...
app-0.1-tests.jar
内容:
├── META-INF│ ├── MANIFEST.MF│ ├── 行家│ ├── ....├── com│ ├── ....└── 现场├── en_EN.properties
不幸的是,当我尝试加载属性文件时出现 IOException - 文件未在以下位置找到:file:/tmp/scratchcC4yMz.scratch/test-packagefQ2euI/app-0.1-tests.jar!/locale/en_EN.properties
我尝试以多种方式加载它们,但每次都遇到相同的问题.在当地,一切都像魅力一样.代码示例:
File file = new File(ClassName.class.getResource("/locale/en_EN.properties").getPath());试试 (InputStream inputStream = new FileInputStream(file.getPath());InputStreamReader streamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"))) {属性 properties = new Properties();properties.load(streamReader);返回属性;} catch (IOException e) {System.err.println("找不到属性文件:" + file.getPath());}
或使用类加载器:
ClassLoader classLoader = getClass().getClassLoader();URL 资源 = classLoader.getResource("/locale/en_EN.properties");
有人可以建议如何读取位于资源中的属性文件的解决方案吗?
这是我所做的,它似乎有效:
/*** 严格的测试 :-)*/公共无效 testApp(){//取自 https://www.mkyong.com/java/java-properties-file-examples///创建属性对象属性 prop = new Properties();InputStream 输入 = 空;//从 src/test/resources 加载属性文件尝试 {input = Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperties.properties");//加载属性文件prop.load(输入);//获取属性值并打印出来System.out.println(prop.getProperty("devicefarm"));} catch (IOException ex) {ex.printStackTrace();} 最后 {如果(输入!= null){尝试 {input.close();} catch (IOException e) {e.printStackTrace();}}}}
这是我从设备场的 java 输出:
[TestNG] 运行:命令行套件你好,世界
这是我的属性文件的内容:
devicefarm=helloWorld
在我的本地测试中它似乎也有效:
-------------------------------------------------------测试-------------------------------------------------------运行 com.devicefarm.www.AppTest你好,世界测试运行:1,失败:0,错误:0,跳过:0,经过的时间:0.018 秒结果 :测试运行:1,失败:0,错误:0,跳过:0
看起来,在属性文件被打包后,我们不能像我们通过 maven 在本地那样在 java 中引用它.如果我们尝试使 test-jar 可执行并运行 jar,我认为会发生同样的事情.
有关类加载器和线程加载器之间的区别,请参阅此 SO 帖子.
然后,当测试完成时,我们可以点击客户工件链接并获取该导出的 zip.
My Appium + JUnit tests works perfectly fine locally, but on aws it can not find properties files. My tests are placed under src/test/java
and properties files used in tests under src/test/resources/locale
.
Zip with dependencies content:
├── app-0.1-tests.jar
├── app-0.1.jar
└── dependency-jars
├── ...
app-0.1-tests.jar
content:
├── META-INF
│ ├── MANIFEST.MF
│ ├── maven
│ ├── ....
├── com
│ ├── ....
└── locale
├── en_EN.properties
Unfortunately when I try to load properties files I have an IOException - file is not found in location:
file:/tmp/scratchcC4yMz.scratch/test-packagefQ2euI/app-0.1-tests.jar!/locale/en_EN.properties
I tried to load them in a couple of ways, each time I have the same issue. Locally everything works like a charm. Example of code:
File file = new File(ClassName.class.getResource("/locale/en_EN.properties").getPath());
try (InputStream inputStream = new FileInputStream(file.getPath());
InputStreamReader streamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"))) {
Properties properties = new Properties();
properties.load(streamReader);
return properties;
} catch (IOException e) {
System.err.println("Properties file not found: " + file.getPath());
}
or by using class loader:
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource("/locale/en_EN.properties");
Can someone suggest solution how to read properties file located in resources?
Here is what I did and it seemed to work:
/**
* Rigourous Test :-)
*/
public void testApp()
{
//taken from https://www.mkyong.com/java/java-properties-file-examples/
//create properties object
Properties prop = new Properties();
InputStream input = null;
//load in the properties file from src/test/resources
try {
input = Thread.currentThread().getContextClassLoader().getResourceAsStream("myproperties.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("devicefarm"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Here is my java output from device farm:
[TestNG] Running:
Command line suite
helloWorld
and here is the contents of my properties file:
devicefarm=helloWorld
In my local test it seemed to work too:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.devicefarm.www.AppTest
helloWorld
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
It would appear that after the properties file is package we can not reference it in java the same way we do locally through maven. If we try to make the test-jar executable and run the jar I think the same thing would happen.
Edit: see this SO post for difference between the class loader and the thread loader.
https://stackoverflow.com/a/22653795/4358385
Regarding exporting the tmp directory, on this page we can tell device farm to export whatever directories we want
Then when the test is finished we can click on the customer artifacts link and get a zip of that export.
这篇关于无法读取 aws 设备场中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!