0

I want to test android apps for accessibility violation. Is it possible to use uiautomator api and monkeyrunner api to capture the layout of all screens?

Arjun
  • 25
  • 4

2 Answers2

0

You can simply use AndroidViewClient's dump:

$ dump --help
usage: dump [OPTION]... [serialno]

Options:
  -H, --help                       prints this help                             
  -V, --verbose                    verbose comments                             
  -v, --version
  -I, --ignore-secure-device       ignore secure device                         
  -E, --ignore-version-check       ignores ADB version check                    
  -F, --force-view-server-use      force view server use (even if UiAutomator present)
  -S, --do-not-start-view-server   don't start ViewServer                       
  -k, --do-not-ignore-uiautomator-killed don't ignore UiAutomator killed              
  -w, --window=WINDOW              dump WINDOW content (default: -1, all windows)
  -i, --uniqueId                   dump View unique IDs                         
  -x, --position                   dump View positions                          
  -d, --content-description        dump View content descriptions               
  -c, --center                     dump View centers                            
  -f, --save-screenshot=FILE       save screenshot to file                      
  -W, --save-view-screenshots=DIR  save View screenshots to files in directory  
  -D, --do-not-dump-views          don't dump views, only useful if you specified -f or -W

In you case, probably

$ dump -d

is enough.

EDIT

If you want something more complex than just saving the logical content of the screen, you can use culebra (another tool in AndroidViewClient) that allows you to generate automated tests using a GUI:

$ culebra -UG -o mytest.py

You can interact with the mirror representation of the device screen in culebra's main window to generate the test. Save it. Run it on any device, even a completely different one.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I'm able to get the details(layout/position) of a particular screen. Does the tool provide exploration logic which automatically generates events, transitions to new state(screen) and then captures it's details? – Arjun Dec 04 '14 at 23:51
  • Hi, I get this error when I run dump - "raise RuntimeError("ERROR: Connecting to %s:%d: %s.\nIs adb running on your computer?" % (self.socket, self.port, ex)) RuntimeError: ERROR: Connecting to :5037: [Errno 111] Connection refused." – Arjun Dec 15 '14 at 18:17
  • It's because dump doesnt start the adb server by itself – Diego Torres Milano Dec 15 '14 at 18:58
  • One of the apk needed by Culebra tester is no more available on app store atleast from India. – Gautam Sep 24 '18 at 12:07
0
import android.util.Log;

import com.android.uiautomator.testrunner.UiAutomatorTestCase;

import java.io.File;

public class TestUtils {

        private UiAutomatorTestCase mTestCase;

        public TestUtils(UiAutomatorTestCase testCase) {
            mTestCase = testCase;
        }
            public void sleepInSeconds(int timeInSeconds) {
                    mTestCase.sleep(timeInSeconds * 1000);
                }

            public String getFileName(String fileNameSuffix) {
                    String tempFileName = fileNameSuffix.toLowerCase().replace(" ", "_")
                            .replace("?", "_");
                    String fileName = String.format("%d_%s", System.currentTimeMillis(),
                            tempFileName);
                    return fileName;
                }

            // Note: You need to have file name as a single string.
            // This will get the UiHierarchy
            public String saveUiHierarchyToFile(String nameOfFile) {
                    String fileName = nameOfFile;
                    if (nameOfFile.contains(" ")) {
                        fileName = getFileName(nameOfFile);
                    }
                    sleepInSeconds(10);
                    try {
                        File hierarchyFile = new File(fileName + ".uix");
                        mTestCase.getUiDevice().dumpWindowHierarchy(hierarchyFile
                                .getAbsolutePath());
                        Log.d(mTestCase.getClass().getSimpleName(), String.format("\t ** UI hierarchy: " +
                                "/data/local/tmp%s", hierarchyFile.getAbsolutePath()));
                        return hierarchyFile.getAbsolutePath();
                    } catch (Exception e) {
                        Log.d(mTestCase.getName(), "\t ** Unable to save view hierarchy" +
                                " to a file", e);
                    }
                    return null;
                }

        // This will get the screen shot
        public String saveScreenshot(String nameOfFile) throws Exception {
                String fileName = nameOfFile;
                if (nameOfFile.contains(" ")) {
                    fileName = getFileName(nameOfFile);
                }
                sleepInSeconds(10);
                File screenshotFile = new File("/data/local/tmp",
                        fileName + ".png");
                mTestCase.getUiDevice().takeScreenshot(
                        new File(screenshotFile.getAbsolutePath()));
                Log.d(mTestCase.getClass().getSimpleName(), String.format("\t ** Screenshot: %s",
                        screenshotFile.getAbsolutePath()));
                return screenshotFile.getAbsolutePath();
            }

        // Store UiAutomator viewer
        public void storeUiAutomatorViewerFiles(String nameOfFile)
                    throws Exception {
                String fileName = getFileName(nameOfFile);
                saveScreenshot(fileName);
                saveUiHierarchyToFile(fileName);
            }
}
Amit Kumar
  • 67
  • 3
  • could you explain the answer-why it is a solution to the question? Would improve its quality a lot. – W1ll1amvl Dec 05 '14 at 01:51
  • As per my understanding he wanted to capture the UiHierarchy of the content and this help to capture the UiHierarchy – Amit Kumar Dec 05 '14 at 01:55