无法读取 aws 设备场中的属性文件

sg_*_*_ro 5 junit amazon-web-services appium aws-device-farm


\n我的 Appium + JUnit 测试在本地工作得很好,但在 aws 上找不到属性文件。我的测试位于 下src/test/java,测试中使用的属性文件位于src/test/resources/locale. \n包含依赖项内容的压缩包:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app-0.1-tests.jar\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 app-0.1.jar\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 dependency-jars     \n    \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ...\n
Run Code Online (Sandbox Code Playgroud)\n\n

app-0.1-tests.jar内容:

\n\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 META-INF\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 MANIFEST.MF\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 maven\n\xe2\x94\x82      \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ....\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 com\n\xe2\x94\x82      \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 ....\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 locale\n       \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 en_EN.properties\n
Run Code Online (Sandbox Code Playgroud)\n\n

不幸的是,当我尝试加载属性文件时,出现 IOException - 文件未在以下位置找到:\nfile:/tmp/scratchcC4yMz.scratch/test-packagefQ2euI/app-0.1-tests.jar!/locale/en_EN.properties

\n\n

每次遇到同样的问题时,我都尝试通过多种方式加载它们。在当地,一切都很有魅力。代码示例:

\n\n
File file = new File(ClassName.class.getResource("/locale/en_EN.properties").getPath());\ntry (InputStream inputStream = new FileInputStream(file.getPath());\nInputStreamReader streamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"))) {\n    Properties properties = new Properties();\n    properties.load(streamReader);\n    return properties;\n} catch (IOException e) {\n    System.err.println("Properties file not found: " + file.getPath());\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者使用类加载器:

\n\n
ClassLoader classLoader = getClass().getClassLoader();\nURL resource = classLoader.getResource("/locale/en_EN.properties");\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n

jmp*_*jmp 3

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

/**
 * 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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我从设备场的 java 输出:

[TestNG] Running:
  Command line suite

helloWorld
Run Code Online (Sandbox Code Playgroud)

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

devicefarm=helloWorld
Run Code Online (Sandbox Code Playgroud)

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

-------------------------------------------------------
 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
Run Code Online (Sandbox Code Playgroud)

看来,属性文件打包后,我们无法像通过maven在本地那样在java中引用它。如果我们尝试使 test-jar 可执行并运行该 jar,我认为会发生同样的事情。

编辑:有关类加载器和线程加载器之间的区别,请参阅此 SO 帖子。

/sf/answers/1585765681/

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

导出tmp目录

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

在此输入图像描述