基于APK的Robotium登录人人网与发状态

搭建好Robotium的环境,大致就是下载安装jdk并配置环境变量,下载并打开Eclipse,下载安装Android SDK Tools并配置环境变量,下载安装ADT插件,创建并打开Android Virtual Device,下载、apk重签名并安装到该device,手动打开人人网应用没问题后环境就算是准备好了。(我学习Robotium时买了杨志伟编著的《手机测试Robotium实战教程》,我自己也总结了一份学习笔记,考虑到作者出书不易我就不上传了,这本书挺好的。)

一些常用的cmd命令,来查看device的状态和jdk的路径(如果adb不是内部或者外部命令,将Android SDK Tools中的adb.exe文件从platform-tools目录下粘贴到tools目录下,然后将platform-tools的路径添加到Path环境变量中即可):

人人网安好后在device中是这样的:

通过打开uiautomatorviewer.bat来捕获安卓界面上的元素。写两个测试,分别实现登录人人网和发状态:

package com.renren.test;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import com.robotium.solo.Solo;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

@SuppressWarnings("rawtypes")
public class RenrenTest extends ActivityInstrumentationTestCase2 {
    // The activity name after re-signed.
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.renren.mobile.android.ui.WelcomeScreen";
    private static Class LauncherActivityClass;
    private Solo solo;
    private static final String TAG = "MyResult";

    // Initiate.
    static {
        try {
            LauncherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @SuppressWarnings("unchecked")
    public RenrenTest() throws ClassNotFoundException {
        super(LauncherActivityClass);
    }

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

    public void testCanLogin() throws InterruptedException {
        boolean result = waitForTextUnless("登录", "照片");
        if (result == true) {
            if (solo.searchText("登录")) {
                solo.clickOnView(solo.getView("com.renren.mobile.android:id/welcome_hot_spot_login"));
            }
            waitForText("请输入用户名");
            if (solo.searchText("请输入用户名")) {
                EditText usrNameText = (EditText) solo
                        .getView("com.renren.mobile.android:id/welcome_hot_spot_account_layout");
                String userName = "用户名";
                solo.enterText(usrNameText, userName);
                EditText pwdText = (EditText) solo
                        .getView("com.renren.mobile.android:id/welcome_hot_spot_password_edit");
                String pwd = "密码";
                solo.enterText(pwdText, pwd);
                // Click the login button.
                solo.clickOnView(solo.getView("com.renren.mobile.android:id/welcome_hot_spot_login_button"));
            }
            waitForText("照片");
            Thread.sleep(3000);
            Log.i(TAG, "已登录");
        } else {
            Log.i(TAG, "无需登录");
        }
    }

    public void testSendInfo() throws InterruptedException {
        waitForText("照片");
        String sendButtonId = "com.renren.mobile.android:id/tab_item_publisher";
        View sendButton = solo.getView(sendButtonId);
        solo.clickLongOnView(sendButton);
        waitForText("发状态");
        try {
            String inputEditorId = "com.renren.mobile.android:id/input_editor";
            EditText editText = (EditText) solo.getView(inputEditorId);
            String message = "Android test!";
            solo.enterText(editText, message);
            solo.clickOnButton("发布");
            waitForText(message);
            Thread.sleep(3000);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }

    private boolean waitForTextUnless(String string, String string2) throws InterruptedException {
        boolean signal = true;
        while (!solo.searchText(string) && signal) {
            if (solo.searchText(string2)) {
                signal = false;
                return signal;
            } else {
                Thread.sleep(1000);
            }
        }
        return signal;
    }
    
    private void waitForText(String text) throws InterruptedException {
        while (!solo.searchText(text)) {
            Thread.sleep(1000);
        }
    }

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

1、SuppressWarnings的意思是压制警告,详细有几种使用方法可以上网搜一下;

2、创建一个solo类来实现对device当前app页面上元素的交互操作;

3、声明一个TAG,让Log类在LogCat中输出一些指定的信息,方便我们在LogCat中查找与判断测试中经历的一些状态(安卓专用的打log方法哦);

4、Robotium的测试方法名要以test开头,testXXX的形式,运行时要选择Android JUnit Test运行;

5、setUp和tearDown是固定的测试类所必须的加载和销毁应用程序的方法;

5、写了两个wait方法,用来等待页面上的元素,和如果出现例外元素就不继续等待目标元素。

注意:

1、要配置AndroidManifest.xml文件中instrumentation标签中的targetPackage值为apk的包名。否则会运行失败,提示“Test run failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'”;

2、BeforeMethod和AfterMethod这两个标签需要testNg的支持,如果没有testNg的话将这两个标签去掉并换成“@Override”即可。

posted @ 2016-01-14 18:16  天外归云  阅读(429)  评论(0编辑  收藏  举报