sg_ro
sg_ro

Reputation: 53

Can't read properties file in aws device farm


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?

Upvotes: 5

Views: 1408

Answers (1)

jmp
jmp

Reputation: 2375

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

exporting the tmp directory

Then when the test is finished we can click on the customer artifacts link and get a zip of that export.

enter image description here

Upvotes: 3

Related Questions