【问题标题】:Capture the layout of all screens - Android App捕获所有屏幕的布局 - Android App
【发布时间】:2014-12-04 00:44:35
【问题描述】:

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

【问题讨论】:

  • 您要截屏还是要截取逻辑内容(即视图)?
  • 您好,我要捕获逻辑内容。

标签: android testing monkeyrunner android-uiautomator


【解决方案1】:

您可以简单地使用 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 主窗口中设备屏幕的镜像表示进行交互以生成测试。保存。在任何设备上运行它,甚至是完全不同的设备。

【讨论】:

  • 非常感谢。我会试试这个。
  • 我能够获取特定屏幕的详细信息(布局/位置)。该工具是否提供自动生成事件、转换到新状态(屏幕)然后捕获其详细信息的探索逻辑?
  • 您好,我在运行转储时收到此错误 - "raise RuntimeError("ERROR: Connecting to %s:%d: %s.\nadb is running on your computer?" % (self. socket, self.port, ex)) RuntimeError: ERROR: Connecting to :5037: [Errno 111] Connection denied."
  • 因为dump没有自己启动adb server
  • Culebra 测试人员所需的其中一个 apk 至少在印度的应用商店中不再可用。
【解决方案2】:
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);
            }
}

【讨论】:

  • 您能解释一下答案吗?为什么它是问题的解决方案?会大大提高它的质量。
  • 据我了解,他想捕获内容的 UiHierarchy,这有助于捕获 UiHierarchy
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-08
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多