Android UIAutomator - read textfile on sdcard during runtime

852 views Asked by At

Is it possible to read a textfile on sdcard during runtime of an UIautomator test? As in an android application, using getExternalDirectory() etc. to create a File-object pointing to the actual file. Is it possible to send a command using getRuntime().exec("cmd"), if so, how? Or is there an easier way to simply access the device:s sdcard and read a file into the test?

The goal is to throughout the test send parameters to the test. So the test will perform certain actions, then continously look for a change on a file on the devices sdcard, and if so, read that line, and continue to perform actions. So therefor a way to read a file, and check certain things, is needed.

Or is there perhaps another way to pass information into the test during runtime? I know it can be done at the start of the testrun, but not during testrun.

1

There are 1 answers

0
Rilwan On

I use below code inside uiautomator code to read text files.instead of

    public void FileRead(String file_location) throws IOException {

    BufferedReader br = new BufferedReader(new FileReader(file_location));
    try {
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();

        while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
        }
        String everything = sb.toString();
        //you can do whatever you want here or return String
    } finally {
        br.close();
    }
}