Lukap
Lukap

Reputation: 31963

Run monkey from java

I have this script in file script.txt

And I run this like this

monkeyrunner /home/user/script.txt

this is my script.txt

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import time

device = MonkeyRunner.waitForConnection("wait forever","emulator-5554")

package = 'com.pak.pak1'

activity = 'com.pak.pak1.MyActivity'

runComponent = package + '/' + activity

#     Runs the component
device.startActivity(component=runComponent)

time.sleep(1)

The thing I want to do is to run the script from java

This code runs a shell command for example to srart the script

 try {
        new Thread() {
            public void run() {
                Process p;
                try {
                    p = Runtime.getRuntime().exec("monkeyrunner /home/user/script.txt");

                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = br.readLine()) != null) {

                        System.out.println(line);

                    }
                    br.close();

                    p.waitFor();
                } catch (Exception e) {
                    //e.printStackTrace();
                }
            }
        }.start();
    } catch (Exception ie) {

    }

And finally mu question is how can I directly from java run the monkey runner commands, I do not want to have the script.txt file. Is this possible ? My goal is to run the monkey runner but I do not want to have the script.txt file

Upvotes: 3

Views: 1210

Answers (2)

theon
theon

Reputation: 14420

Apparently, if you include the MonkeyRunner chimpchat.jar (and it's jar depedencies) on your classpath, then you can call the monkey runner Java classes directly inside your Java application. Check out this class and this class that make up an example:

Another thread on this subject

Upvotes: 1

estani
estani

Reputation: 26547

This looks awfully complicated, but still.. monkeyrunner can run interactively, so write directly to stdin (get it from p.getOutputStream()) all strings you want it to run.

you might need to exhaust the stdout before issuing any command, but I don't think that will be the case.

Upvotes: 0

Related Questions