5


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?

sg_ro
  • 53
  • 3
  • I haven't reproduced this but I think the issue is with the / in front of locale. Could you change it to: classLoader.getResource("locale/en_EN.properties"); And let me know of that helps? – jmp Mar 23 '18 at 01:56
  • One thing you could do to figure out where it is, is put /tmp/ in the Host input for which files to pull. You should see this on the specify device state' page. Then you'll get a zip of everything in the tmp directory in the customer artifacts download – jmp Mar 23 '18 at 02:23
  • @jmp without extra / in front `getResource` returns null, so it is not working. Can you extend your second thought? I'm not sure how to get into specify device tmp folder in aws device farm. – sg_ro Mar 23 '18 at 08:48

1 Answers1

3

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

jmp
  • 2,175
  • 2
  • 17
  • 16
  • maybe you also know how to read properties file located in main project directory? – sg_ro Mar 26 '18 at 08:09
  • Then we would need a test resources tag to tell maven where those resources are if they are out of the resources directory. ${project.basedir}/src/test/resources – jmp Mar 26 '18 at 09:54