Android 基于事件的测试

发布于 2024-11-28 16:08:07 字数 375 浏览 5 评论 0原文

我正在使用 robotsium 进行测试,并且遇到了一系列计时问题,这些问题使我很难知道活动(或视图)何时完成加载。因此,我正在编写的测试不是很稳健。

我正在寻找一种方法来使用可以推送到测试框架的事件来检测我的测试应用程序。如果我的被测应用程序可以在预期事件发生时“告诉”我的测试,这将非常有用。我在 Windows\Windows Phone 中使用了 Windows 事件跟踪过去效果很大。

我正在寻找的穷人执行此操作的方法是让我的测试应用程序实时读取 logcat 并在发生预期事件时通知测试。

还有人有其他想法吗?

I'm using robotium for testing and I'm running into a bunch of timing issues that are making it difficult for me to know when an activity (or view) is done loading. As a result the tests I'm writing are not very robust.

I'm looking for a way to instrument my application under test with events that can be pushed to the test framework. If my application under test could "tell" my tests when an expect event occurs, this would be very useful. I've used Event Tracing for Windows for windows\windows phone in the past to great effect.

The poor mans way of doing this that I'm looking at is to have my test application read the logcat in realtime and notify the test when an expected event occurs.

Does anyone have any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2024-12-05 16:08:07

为了解决这个问题,我依靠操作系统 logcat。我使用跟踪消息检测正在测试的代码部分,并等待它们在测试代码中出现。它可能不是最高效的做事方式,但它适合我的需求。我首先尝试创建一个从 logcat 读取的异步任务,但遇到了计时问题。我最终实现了这样的东西:

    logger.info("Click on something");
    EmSingleton.get().setCheckPoint();
    solo.clickOnText(SOMETHING, 1, true); // writes RELOAD_COMPLETE to logcat when done

    logger.info("Validate that the event is done);
    EmSingleton.get().waitForEvent(GrouponLnConstants.TodaysDeal.RELOAD_COMPLETE, 15000);
    // Do other stuff... 

EventManager 代码(我将其包装在名为 EmSingleton 的单例中):

public class EventManager {
private ArrayList<String> args = null;
private long startTime;

public EventManager() {
    args = new ArrayList<String>(Arrays.asList("-v", "time", "-d"));
    setCheckPoint();
}

public EventManager(ArrayList<String> _args) {
    this();
    for(String s: args) {
        if (!args.contains(s)) {
            args.add(s);
        }
    }
}

public void setCheckPoint()
{
    Time startTimeOS = new android.text.format.Time();
    startTimeOS.setToNow();
    startTime = startTimeOS.toMillis(true);
}

public LogEvent checkEvent(String filter) throws Exception {
    ArrayList<LogEvent> events = gatherEvents();

    for(LogEvent event: events){
        if(event.checkMsgSubstring(filter)){
            return event;
        }
    }

    return null;
}

public LogEvent waitForEvent(String filter, int timeout) throws Exception
{
    int retries = timeout/1000 == 0 ? 1 : timeout/1000;
    for(int i = 0; i < retries; i++)
    {
        LogEvent event = checkEvent(filter);
        if(event != null){
            return event;
        }
        Thread.sleep(1000);
    }
    return null;
}

public ArrayList<LogEvent> getEvents() {
    return gatherEvents();
}

public void clearEvents() throws Exception{
    ArrayList<String> commandLine = new ArrayList<String>();
    commandLine.add("logcat");
    commandLine.add("-c");

    ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
    pb.redirectErrorStream(true);
    Process logcatProcess = pb.start();

    logcatProcess.waitFor();
}

protected ArrayList<LogEvent> gatherEvents() {
    ArrayList<LogEvent> events = new ArrayList<LogEvent>();
    final StringBuilder log = new StringBuilder();
    BufferedReader br;
    try {
        ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("logcat");
        if (null != args) {
            commandLine.addAll(args);
        }

        ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
        pb.redirectErrorStream(true);
        Process logcatProcess = pb.start();

        InputStream is = logcatProcess.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        br = new BufferedReader(isr);

        String line = null;

        while (true) {
            line = br.readLine();
            if(line == null){
                break;
            }
            // Add events to the events arraylist if they occur after the LogCollector has started to run
            LogEvent event = new LogEvent(line);
            if(event.peekTimeMS() > startTime)
            {
                events.add(event);
            }
        }
    } catch (Exception e) {
        TestLogger.get().error("GrouponTest", String.format("gatherEvents doInBackground failed: %s", e.toString()));
    }

    return events;
}
}

您需要在应用程序中设置以下权限:

<uses-permission android:name="android.permission.READ_LOGS">

我还创建了一个 LogEvent 对象来解析来自 logcat 的事件:

public class LogEvent {
String orignalString = null;
boolean parsed = false;

long timeMs = 0;
String timeString = null;
String verbosity = null;
String tag = null;
int pid = 0;
String msg = null;

// Lazy factory design pattern
// http://en.wikipedia.org/wiki/Lazy_initialization
public LogEvent(String s) {
    orignalString = s;
}

public long getTimeMs() {
    checkInit();
    return timeMs;
}

public long peekTimeMS()
{
    // Time is always formattted as such: MM-DD HH:MM:SS.XXX
    return parseTime(orignalString.substring(0,5), orignalString.substring(6,18));
}

public String getTimeString() {
    checkInit();
    return timeString;
}

public String getVerbosity() {
    checkInit();
    return verbosity;
}

public String getTag() {
    checkInit();
    return tag;
}

 public int getPid() {
    checkInit();
    return pid;
}

public String getMsg() {
    checkInit();
    return msg;
}

/**
 * Checks to see if the event message contains a substring
 * @param check
 * @return
 */
public Boolean checkMsgSubstring(String check)
{
    int index = orignalString.indexOf(check);
    boolean isSubstring = (index >= 0);
    return isSubstring;
}

public String toString()
{
    checkInit();
    return String.format("%s %s/%s(%d): %s", timeString, verbosity, tag, pid, msg);
}

private void checkInit()
{
    if(!parsed)
    {
        parseEvent();
        parsed = true;
    }
}

private void parseEvent()
{
    try{
    String [] splits = orignalString.split("[ ]+");
    // Sometimes the PID is of format ( XX) instead of (XX)
    if(splits[2].indexOf(")") < 0)
    {
        splits[2] += splits[3];
        ArrayList<String> formatted = new ArrayList<String>(Arrays.asList(splits));
        formatted.remove(3);
        splits = formatted.toArray(new String[formatted.size()]);
    }

    // Parse time
    timeMs = parseTime(splits[0], splits[1]);
    timeString = String.format("%s %s", splits[0], splits[1]);
    // Parse tag
    verbosity = parseVerbosity(splits[2]);
    tag = parseTag(splits[2]);
    pid = parsePid(splits[2]);
    // Parse message (may be empty)
    if (splits.length > 3) {
        msg = orignalString.substring(orignalString.indexOf(splits[3]));
    } else {
        msg = "";
    }
    }
    catch (Exception e)
    {
        // TODO: there are some strangely formated events in the system. need to deal with these?
    }
}

/**
 * Time comes in following format: 08-11 20:03:17.182:
 * Parse into milliseconds
 * @param day string of format 08-11
 * @param hours string of format 20:03:17.182:
 * @return
 */
private long parseTime(String day, String hours)
{
    Time timeToSet = new Time();
    Time currentTime = new Time();
    currentTime.setToNow();

    // Parse fields
    String[] daySplits = day.split("-");
    if(daySplits.length < 2)
        return 0;

    String[] hourSplits = hours.split(":");
    if(hourSplits.length < 2)
        return 0;

    String[] secondSplits = hourSplits[2].split("\\.");
    if(secondSplits.length < 2)
        return 0;

    int _year = currentTime.year;
    int _month = Integer.parseInt(daySplits[0])-1;
    int _day = Integer.parseInt(daySplits[1]);
    int _hour = Integer.parseInt(hourSplits[0]);
    int _min = Integer.parseInt(hourSplits[1]);
    int _sec = Integer.parseInt(secondSplits[0]);
    int _mili = Integer.parseInt(secondSplits[1]);

    //set(int second, int minute, int hour, int monthDay, int month, int year)
    timeToSet.set(_sec, _min, _hour, _day, _month, _year);

    // return calculated value
    long parsedTimeInMili = timeToSet.toMillis(true) + (long)_mili;
    return parsedTimeInMili;
}

private String parseVerbosity(String s)
{
    return s.split("/")[0];
}

private String parseTag(String s)
{
    int verbosityLength = parseVerbosity(s).length() +1;
    String tagPart = s.substring(verbosityLength);
    return tagPart.split("\\(")[0];
}

private int parsePid(String s)
{
    try {
        String pidPart = s.split("\\(")[1];
        return Integer.parseInt(pidPart.split("\\)")[0]);
    } catch (Exception e) {
        e.toString();
    }
    return -1;
}
}

To solve this I'm relying on the OS logcat. I instrument portions of the code being tested with trace messages and wait for them to occur in the test code. Its perhaps not the most performant way of doing things, but it suits my needs. I first tried to make an asynctask that read from the logcat, but ran into timing issues. I finally implemented something like this:

    logger.info("Click on something");
    EmSingleton.get().setCheckPoint();
    solo.clickOnText(SOMETHING, 1, true); // writes RELOAD_COMPLETE to logcat when done

    logger.info("Validate that the event is done);
    EmSingleton.get().waitForEvent(GrouponLnConstants.TodaysDeal.RELOAD_COMPLETE, 15000);
    // Do other stuff... 

EventManager code (That I wrapped in a singleton called EmSingleton):

public class EventManager {
private ArrayList<String> args = null;
private long startTime;

public EventManager() {
    args = new ArrayList<String>(Arrays.asList("-v", "time", "-d"));
    setCheckPoint();
}

public EventManager(ArrayList<String> _args) {
    this();
    for(String s: args) {
        if (!args.contains(s)) {
            args.add(s);
        }
    }
}

public void setCheckPoint()
{
    Time startTimeOS = new android.text.format.Time();
    startTimeOS.setToNow();
    startTime = startTimeOS.toMillis(true);
}

public LogEvent checkEvent(String filter) throws Exception {
    ArrayList<LogEvent> events = gatherEvents();

    for(LogEvent event: events){
        if(event.checkMsgSubstring(filter)){
            return event;
        }
    }

    return null;
}

public LogEvent waitForEvent(String filter, int timeout) throws Exception
{
    int retries = timeout/1000 == 0 ? 1 : timeout/1000;
    for(int i = 0; i < retries; i++)
    {
        LogEvent event = checkEvent(filter);
        if(event != null){
            return event;
        }
        Thread.sleep(1000);
    }
    return null;
}

public ArrayList<LogEvent> getEvents() {
    return gatherEvents();
}

public void clearEvents() throws Exception{
    ArrayList<String> commandLine = new ArrayList<String>();
    commandLine.add("logcat");
    commandLine.add("-c");

    ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
    pb.redirectErrorStream(true);
    Process logcatProcess = pb.start();

    logcatProcess.waitFor();
}

protected ArrayList<LogEvent> gatherEvents() {
    ArrayList<LogEvent> events = new ArrayList<LogEvent>();
    final StringBuilder log = new StringBuilder();
    BufferedReader br;
    try {
        ArrayList<String> commandLine = new ArrayList<String>();
        commandLine.add("logcat");
        if (null != args) {
            commandLine.addAll(args);
        }

        ProcessBuilder pb = new ProcessBuilder(commandLine.toArray(new String[0]));
        pb.redirectErrorStream(true);
        Process logcatProcess = pb.start();

        InputStream is = logcatProcess.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        br = new BufferedReader(isr);

        String line = null;

        while (true) {
            line = br.readLine();
            if(line == null){
                break;
            }
            // Add events to the events arraylist if they occur after the LogCollector has started to run
            LogEvent event = new LogEvent(line);
            if(event.peekTimeMS() > startTime)
            {
                events.add(event);
            }
        }
    } catch (Exception e) {
        TestLogger.get().error("GrouponTest", String.format("gatherEvents doInBackground failed: %s", e.toString()));
    }

    return events;
}
}

You'll need to set the following permission in your application:

<uses-permission android:name="android.permission.READ_LOGS">

I also created a LogEvent Object to parse events coming from the logcat:

public class LogEvent {
String orignalString = null;
boolean parsed = false;

long timeMs = 0;
String timeString = null;
String verbosity = null;
String tag = null;
int pid = 0;
String msg = null;

// Lazy factory design pattern
// http://en.wikipedia.org/wiki/Lazy_initialization
public LogEvent(String s) {
    orignalString = s;
}

public long getTimeMs() {
    checkInit();
    return timeMs;
}

public long peekTimeMS()
{
    // Time is always formattted as such: MM-DD HH:MM:SS.XXX
    return parseTime(orignalString.substring(0,5), orignalString.substring(6,18));
}

public String getTimeString() {
    checkInit();
    return timeString;
}

public String getVerbosity() {
    checkInit();
    return verbosity;
}

public String getTag() {
    checkInit();
    return tag;
}

 public int getPid() {
    checkInit();
    return pid;
}

public String getMsg() {
    checkInit();
    return msg;
}

/**
 * Checks to see if the event message contains a substring
 * @param check
 * @return
 */
public Boolean checkMsgSubstring(String check)
{
    int index = orignalString.indexOf(check);
    boolean isSubstring = (index >= 0);
    return isSubstring;
}

public String toString()
{
    checkInit();
    return String.format("%s %s/%s(%d): %s", timeString, verbosity, tag, pid, msg);
}

private void checkInit()
{
    if(!parsed)
    {
        parseEvent();
        parsed = true;
    }
}

private void parseEvent()
{
    try{
    String [] splits = orignalString.split("[ ]+");
    // Sometimes the PID is of format ( XX) instead of (XX)
    if(splits[2].indexOf(")") < 0)
    {
        splits[2] += splits[3];
        ArrayList<String> formatted = new ArrayList<String>(Arrays.asList(splits));
        formatted.remove(3);
        splits = formatted.toArray(new String[formatted.size()]);
    }

    // Parse time
    timeMs = parseTime(splits[0], splits[1]);
    timeString = String.format("%s %s", splits[0], splits[1]);
    // Parse tag
    verbosity = parseVerbosity(splits[2]);
    tag = parseTag(splits[2]);
    pid = parsePid(splits[2]);
    // Parse message (may be empty)
    if (splits.length > 3) {
        msg = orignalString.substring(orignalString.indexOf(splits[3]));
    } else {
        msg = "";
    }
    }
    catch (Exception e)
    {
        // TODO: there are some strangely formated events in the system. need to deal with these?
    }
}

/**
 * Time comes in following format: 08-11 20:03:17.182:
 * Parse into milliseconds
 * @param day string of format 08-11
 * @param hours string of format 20:03:17.182:
 * @return
 */
private long parseTime(String day, String hours)
{
    Time timeToSet = new Time();
    Time currentTime = new Time();
    currentTime.setToNow();

    // Parse fields
    String[] daySplits = day.split("-");
    if(daySplits.length < 2)
        return 0;

    String[] hourSplits = hours.split(":");
    if(hourSplits.length < 2)
        return 0;

    String[] secondSplits = hourSplits[2].split("\\.");
    if(secondSplits.length < 2)
        return 0;

    int _year = currentTime.year;
    int _month = Integer.parseInt(daySplits[0])-1;
    int _day = Integer.parseInt(daySplits[1]);
    int _hour = Integer.parseInt(hourSplits[0]);
    int _min = Integer.parseInt(hourSplits[1]);
    int _sec = Integer.parseInt(secondSplits[0]);
    int _mili = Integer.parseInt(secondSplits[1]);

    //set(int second, int minute, int hour, int monthDay, int month, int year)
    timeToSet.set(_sec, _min, _hour, _day, _month, _year);

    // return calculated value
    long parsedTimeInMili = timeToSet.toMillis(true) + (long)_mili;
    return parsedTimeInMili;
}

private String parseVerbosity(String s)
{
    return s.split("/")[0];
}

private String parseTag(String s)
{
    int verbosityLength = parseVerbosity(s).length() +1;
    String tagPart = s.substring(verbosityLength);
    return tagPart.split("\\(")[0];
}

private int parsePid(String s)
{
    try {
        String pidPart = s.split("\\(")[1];
        return Integer.parseInt(pidPart.split("\\)")[0]);
    } catch (Exception e) {
        e.toString();
    }
    return -1;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文