Robotium 测试Android apk安装包

介绍

要测试apk程序必须和我们编写的测试程序拥有相同的签名(signature)。如果没有apk程序的签名秘钥,就要去除apk程序的签名,然后再使用自己的key对其签名(这一步中,我们可以使用debug key),已经有现成的工具可用,下载地址re-sign.jar,这个工具可以去掉apk程序的原签名,然后使用我们自己的debug key对其签名。

详细

编写测试用例之前,我们需要知道apk程序的包名(package name)和启动页面名称(launcher activity name),这两个名称我们可以通过程序启动时的日志(adb logcat)来获取。程序日志会打印出包名和activity名称。 日志格式类似于:Starting activity: Intent { act=android.intent.action.MAIN cat=android.intent.category.LAUNCHER? flg=0x10200000 cmp=com.example.android.notepad/.NotesList

上面的日志表明程序包名为:com.example.android.notepad、启动activity名为:com.example.android.notepad.NotesList,这里的activity name就是下面代码中的LAUNCHER_ACTIVITYFULLCLASSNAME

下面是测试代码的例子:

package com.yourcompany.yourtestname;

import com.jayway.android.robotium.solo.Solo;

import android.test.ActivityInstrumentationTestCase2;

@SuppressWarnings("rawtypes")
public class ReallyBlackboxTest extends ActivityInstrumentationTestCase2 {

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.newsrob.DashboardListActivity";

    private static Class<?> launcherActivityClass;
    static{
            try {
                    launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
            } catch (ClassNotFoundException e) {
                    throw new RuntimeException(e);
            }
    }

    @SuppressWarnings("unchecked")
    public ReallyBlackboxTest() throws ClassNotFoundException {
            super(launcherActivityClass);
    }

    private Solo solo;

    @Override
    protected void setUp() throws Exception {
            solo = new Solo(getInstrumentation(), getActivity());
    }

    public void testCanOpenSettings(){
            solo.pressMenuItem(0);
    }

    @Override
    public void tearDown() throws Exception {
            solo.finishOpenedActivities();

    }
}
版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=573