捕获所有屏幕的布局 - Android App

时间:2014-12-04 00:44:36

标签: android testing monkeyrunner uiautomator

我想测试Android应用程序的可访问性违规。是否可以使用uiautomator api和monkeyrunner api来捕获所有屏幕的布局?

2 个答案:

答案 0 :(得分:0)

您只需使用 AndroidViewClient 的转储:

$ 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

在你的情况下,可能是

$ dump -d

就够了。

修改

如果你想要比保存屏幕的逻辑内容更复杂的东西,你可以使用culebra(AndroidViewClient中的另一个工具),它允许你使用GUI生成自动化测试:

$ culebra -UG -o mytest.py

您可以在culebra主窗口中与设备屏幕的镜像表示进行交互以生成测试。保存。在任何设备上运行它,即使是完全不同的设备。

答案 1 :(得分: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);
            }
}