Android -- How to detect if Monkey Tool has finished working?

1.2k views Asked by At

I'm developing an application which is basically about performing stress/functional tests for the devices that are being produced.

The application should wait until Monkey Tool finishes working so that it can produce a .log file and inform about the UI test. I have no troubles generally but I need to know if the Monkey Tool has finished the test.

I have checked the main documentation here: Monkey Android Documentation.

Also I had some time to look at Monkey source codes. An example one is: Monkey.java

Unfortunately, I couldn't really find any clue about detecting the moment when Monkey finishes working.

So the real question is:

Is there a way to detect or get information when the Monkey Tool finishes testing the UI?

---[EDIT]---

I have tried an amateur solution by checking running Monkey process using shell and reading the output from BufferedReader. It works, but since it's not the best solution, I'm still waiting for a more professional answer if exists. Thanks in advance.

Here's the code I tried:

boolean is_monkey_running = true;
    while(is_monkey_running) {
        try {
            //checking process state
            Process process = Runtime.getRuntime().exec("ps");
            BufferedReader buffered_reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            StringBuilder log = new StringBuilder();
            String line = "";

            while ((line = buffered_reader.readLine()) != null) {
                log.append(line);
            }

            if (log.indexOf("com.android.commands.monkey") == -1) {
                //Monkey process has been finished.
                Log.i("AATS", GET_DATE_AND_TIME() +  "Monkey has finished testing.");
                is_monkey_running = false;
            }

            else {
                //Monkey is still running.
                WAIT_N_MILISECONDS(1000);
            }
        }
        catch (IOException e) {
        }
    }
1

There are 1 answers

4
Burak Day On BEST ANSWER

Allright. I guess this is the best solution I have tried so far. Posting it so it might help anyone who's interested. Still open for any clearer answers.

Here's the code:

    //example String monkey_options = "--ignore-crashes --throttle 500 -v 500"; 
    try {
        Process monkey_process = new ProcessBuilder().command("monkey", monkey_options).start();
        monkey_process.waitFor();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

EDIT: Your application has to run as system apk in order to call monkey process within the application