无法读取aws device farm中的属性文件

Can't read properties file in aws device farm


我的 Appium + JUnit 测试在本地运行得很好,但在 aws 上找不到属性文件。我的测试放在 src/test/java 下,测试中使用的属性文件放在 src/test/resources/locale 下。 包含依赖项内容的 Zip:

├── app-0.1-tests.jar
├── app-0.1.jar
└── dependency-jars     
    ├── ...

app-0.1-tests.jar内容:

├── META-INF
│   ├── MANIFEST.MF
│   ├── maven
│      ├── ....
├── com
│      ├── ....
└── locale
       ├── 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());
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());
}

或使用 class 加载程序:

ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource("/locale/en_EN.properties");

有人可以建议如何读取位于资源中的属性文件的解决方案吗?

这是我所做的并且似乎有效:

/**
 * 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();
            }
        }
    }
}

这是我从 device farm 得到的 java 输出:

[TestNG] Running:
  Command line suite

helloWorld

这是我的属性文件的内容:

devicefarm=helloWorld

在我的本地测试中它似乎也有效:

-------------------------------------------------------
 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

似乎在属性文件被打包后,我们不能像在本地通过maven 那样在java 中引用它。如果我们尝试制作 test-jar 可执行文件和 运行 jar,我认为同样的事情会发生。

编辑:请参阅此 SO post 以了解 class 加载程序和线程加载程序之间的区别。

关于导出 tmp 目录,在此页面上我们可以告诉设备农场导出我们想要的任何目录

测试完成后,我们可以单击客户工件 link 并获取该导出的 zip。