Question

My goal is to make a monkey visit all pages/activity of a given android application. I am currently using Chimpchat and my first steps are as following :

1 - Connection to the device :

TreeMap<String, String> options = new TreeMap<String, String>();
options.put("backend", "adb");
options.put("adbLocation", ADB);
mChimpchat = ChimpChat.getInstance(options);
mDevice = mChimpchat.waitForConnection(TIMEOUT, ".*");
mDevice.wake();

2 - Getting a list of view IDs :

mDevice.getViewIdList();

3 - For each strings (using iterator it) ID contains in list returned by getViewIdList(), I would like to access Class, Text if any, bounds, etc ...

 while (it.hasNext()) {
        String s = it.next();
        System.out.println(s + " : ");
        try {
            IChimpView v = mDevice.getView(By.id(s));
            System.out.println(v);
            System.out.println(v.getViewClass() + "  : " );
            if (v.getViewClass().toString() == "TextView") {
                System.out.print(v.getText());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

I get an exception on the

v.getViewClass()


com.android.chimpchat.core.ChimpException: Node with given ID does not exist
at com.android.chimpchat.ChimpManager.queryView(ChimpManager.java:415)
at com.android.chimpchat.core.ChimpView.queryView(ChimpView.java:53)
at com.android.chimpchat.core.ChimpView.getViewClass(ChimpView.java:96)
at JavaMonkey.listViewsID(JavaMonkey.java:80)
at JavaMonkey.main(JavaMonkey.java:114)

If anyone can point my mistake(s) or point me to another approach, it would be greatly appreciated !

Was it helpful?

Solution 2

I think that the issue is that there is no Activity running. As I commented above you might be able to use startActivity to start one. However that will take some digging to figure out what all needs to be passed in.
Another solution is as follows:

StringBuilder builder = new StringBuilder();
builder.append("am start -a android.intent.action.MAIN -n ");
builder.append(mPackage).append("/").append(mActivity);
String output = mDevice.shell(builder.toString());

This will use the adb shell to launch the application. mPackage = the package path (com.company.application) and mActivity = the activity (.MyActivity). From there you should be able to mDevice.getHierarchyViewer() or mDevice.getViewIdList()

OTHER TIPS

I think Robotium would be much better suited for this type of testing. Accessing views on a remote device using adb/MonkeyRunner is not very reliable in my experience. In addition, Robotium has a bunch of cool features and can be easily integrated into an existing test suite.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top