Android无线测试之—UiAutmator运行命令介绍与快速调试

一、运行命令介绍:

#Test.java

package com.uiautomatortest;

import android.os.Bundle;
import android.os.RemoteException;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class Test extends UiAutomatorTestCase {
    
    public void testHome(){
        
        UiDevice.getInstance().pressHome();
        sleep(2000);
    }
    
    public void testMenu(){
        
        UiDevice.getInstance().pressMenu();
        sleep(2000);
    }
    
    public void testRecent() throws RemoteException{
        
        UiDevice.getInstance().pressRecentApps();
        sleep(2000);
    }
    
    public void testDemo(){
        
        UiDevice.getInstance().pressHome();
        sleep(2000);
        Bundle bundle=getParams();
        String phone=bundle.getString("phone");
        System.out.println("PHONE="+phone);
    }

}
Test.java

例1:运行某个测试类(运行该类下所有测试用例):

  adb shell uiautomator runtest demo.jar -c com.uiautomatortest.Test
例2:运行某个测试类下的某个测试用例:

  adb shell uiautomator runtest demo.jar -c com.uiautomatortest.Test#testHome

例3:同时运行多个测试用例:

  adb shell uiautomator runtest demo.jar -c com.uiautomatortest.Test#testHome -c com.uiautomatortest.Test#testRecent

例4:使用-e参数传入一个键值对到测试程序中:

  adb shell uiautomator runtest demo.jar -c com.uiautomatortest.Test#testDemo -e phone 12345678

二、快速调试:

  原来存在的问题:

  1)无法在eclipse直接调试

  2)使用命调试繁琐效率低

  解决办法:

    将调试步骤转换成脚本,调试步骤共5步:

    

    那么获取快速编译脚本的方法如下:

    1)单击eclipse中open Perspective按钮,选择Git Repository Exploring,单击OK:

    

    2)点击Clone a Git repository,在弹出的窗口中URI处输入https://github.com/fan297/UiAutomatorHelper.git,一路next,直至finish

    

    3)导入工程,一路next直至Finish:

    

    

    4)多出一个工程UiAutomatorDebug,将工程里面的UiAutomatorHelper.java拷贝到相应的工程下面

    

    5)在要运行的工程类中添加如下的代码
    

#将此段代码添加到Test.java的Test类的最后面

public static void main(String[] args){
        
    String jarName, testClass, testName, androidId;
    jarName="DemoTest";
    testClass="com.uiautomatortest.Test";
    testName="testDemo";
    androidId="1";
    new UiAutomatorHelper(jarName, testClass, testName, androidId);

}
Test.java

    6)在eclipse中选择run as Java Application,观察模拟器的变化。

    

posted on 2015-05-29 17:21  发卡  阅读(737)  评论(0编辑  收藏  举报