• Uiautomator 2.0之UiDevice新增API学习小记


    1. InstrumentationRegistry类

    1.1. 类说明:

    一个暴露的注册实例,持有instrumentation运行的进程和参数,还提供了一种简便的方法调用instrumentation, application context和instrumentation参数。

    1.2 相关API

    返回类型 API
    static Bundle getArguments(): 返回一个instrumentation参数副本
    static Context getContext():  返回instrumentation对应包的Context
    static Instrumentation getInstrumentation(): 返回当前运行的instrumentation
    static Context getTargetContext(): 返回一个目标应用程序的Context
    static void

    registerInstance(Instrumentation instrumentation, Bundle arguments):

    记录或暴露当前instrumentation运行和instrumentation参数包的副本,存储在注册中心

    1.3 简单示例

     1.3.1 测试代码

     1 package com.test.tommyxie.hellouiautomator;
     2 
     3 import android.app.Instrumentation;
     4 import android.content.Context;
     5 import android.content.Intent;
     6 import android.net.Uri;
     7 import android.os.Bundle;
     8 import android.support.test.InstrumentationRegistry;
     9 import android.support.test.runner.AndroidJUnit4;
    10 import android.support.test.uiautomator.UiDevice;
    11 
    12 import org.junit.Before;
    13 import org.junit.Test;
    14 import org.junit.runner.RunWith;
    15 
    16 import java.io.IOException;
    17 
    18 /**
    19  * Created by tommyxie on 16/3/3.
    20  */
    21 
    22 @RunWith(AndroidJUnit4.class)
    23 public class TestClass01 {
    24     public UiDevice mDevice;
    25     public Instrumentation instrumentation;
    26 
    27     @Before
    28     public void setUp(){
    29         instrumentation = InstrumentationRegistry.getInstrumentation();
    30         mDevice = UiDevice.getInstance(instrumentation);
    31     }
    32 
    33     @Test
    34     public void testCase01() throws IOException {
    35         //获取运行时的参数
    36         Bundle args = InstrumentationRegistry.getArguments();
    37         //输出到运行报告中
    38         instrumentation.sendStatus(100, args);
    39 
    40         //获取测试包的Context
    41         Context testContext = InstrumentationRegistry.getContext();
    42         //获取被测应用的Context
    43         Context testedContext = InstrumentationRegistry.getTargetContext();
    44 
    45         //通过Context来启动一个Activity,e.g.浏览器
    46         String url = "https://www.baidu.com";
    47         Intent i1 = new Intent(Intent.ACTION_VIEW);
    48         i1.setData(Uri.parse(url));
    49         i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    50         testContext.startActivity(i1);
    51         
    52         //通过目标Context来启动拨号功能
    53         Intent i2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + 10086));
    54         i2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    55         testedContext.startActivity(i2);
    56         
    57         Bundle inputBundle = new Bundle();
    58         inputBundle.putString("key", "value");
    59         //注入一个Bundle
    60         InstrumentationRegistry.registerInstance(instrumentation, inputBundle);
    61         //获取运行参数
    62         Bundle outBundle = InstrumentationRegistry.getArguments();
    63         //输出到结果报告中
    64         instrumentation.sendStatus(110,outBundle);
    65 
    66 
    67     }
    68 
    69 }

     1.3.2 运行结果

    2. UiDevice新增API

    2.1 API 介绍

    返回类型 API
    void dumpWindowHierarchy(OutPutStream out): 获取当前页面层级到输出流
    String executeShellCommand(String cmd): 执行一个shell命令。备注:此方法只支持api21以上,手机需要5.0系统以上
    UiObject2 findObject(BySelector selector): 返回第一个匹配条件的对象
    UiObject findObject(UiSelector selector): 返回一个匹配条件的代表视图的UiObject对象
    List<UiObject2> findObjects(BySelector selector): 返回所有匹配条件的对象
    <R> R wait(SearchCondition<R> condition, long timeout): 等待的条件得到满足


    2.2 代码示例

     1 package com.test.tommyxie.hellouiautomator;
     2 
     3 import android.app.Instrumentation;
     4 import android.os.Bundle;
     5 import android.os.Environment;
     6 import android.support.test.InstrumentationRegistry;
     7 import android.support.test.runner.AndroidJUnit4;
     8 import android.support.test.uiautomator.By;
     9 import android.support.test.uiautomator.UiDevice;
    10 import android.support.test.uiautomator.UiObject;
    11 import android.support.test.uiautomator.UiObject2;
    12 import android.support.test.uiautomator.UiObjectNotFoundException;
    13 import android.support.test.uiautomator.UiSelector;
    14 import android.support.test.uiautomator.Until;
    15 import android.widget.TextView;
    16 
    17 import org.junit.Before;
    18 import org.junit.Test;
    19 import org.junit.runner.RunWith;
    20 
    21 import java.io.File;
    22 import java.io.FileOutputStream;
    23 import java.io.IOException;
    24 import java.io.OutputStream;
    25 import java.util.List;
    26 
    27 /**
    28  * Created by tommyxie on 16/3/3.
    29  */
    30 
    31 @RunWith(AndroidJUnit4.class)
    32 public class TestClass01 {
    33     public UiDevice mDevice;
    34     public Instrumentation instrumentation;
    35 
    36     @Before
    37     public void setUp(){
    38         instrumentation = InstrumentationRegistry.getInstrumentation();
    39         mDevice = UiDevice.getInstance(instrumentation);
    40     }
    41 
    42     @Test
    43     public void testCase01() throws IOException, UiObjectNotFoundException {
    44 
    45         //dumpWindowHierarchy(OutPutStream out)
    46         File file = new File(Environment.getExternalStorageDirectory()+File.separator+"dump.xml");
    47         if(file.exists()){
    48             file.delete();
    49         }
    50         file.createNewFile();
    51         OutputStream outputStream = new FileOutputStream(file);
    52         mDevice.dumpWindowHierarchy(outputStream);
    53 
    54         //executeShellCommand(String cmd)
    55         mDevice.executeShellCommand("am start -n com.tencent.mobileqq/.activity.SplashActivity ");
    56 
    57         //findObject(BySelector selector)
    58         mDevice.wait(Until.findObject(By.text("联系人")),2000);
    59         UiObject2 uiObject2 = mDevice.findObject(By.text("联系人"));
    60         uiObject2.click();
    61 
    62         //findObject(UiSelector selector)
    63         UiObject uiObject = mDevice.findObject(new UiSelector().text("短信"));
    64         uiObject.click();
    65 
    66         //findObjects(BySelector selector)
    67         List <UiObject2> uiObject21 = mDevice.findObjects(By.clazz(TextView.class));
    68         Bundle bundle = new Bundle();
    69         for (UiObject2 a:uiObject21) {
    70             bundle.putString("TextView", a.getText());
    71         }
    72         instrumentation.sendStatus(123,bundle);
    73 
    74 
    75     }
    76 
    77 }

     原创:http://blog.csdn.net/swordgirl2011/article/details/50941555

  • 相关阅读:
    币值转换
    抓老鼠啊!亏了还是赚了
    打印沙漏
    秋季学习总结
    记忆中最深刻的三位老师
    自我介绍
    docker 安装redis 和 mysql
    centos 安装docker
    celery的简单使用
    django redis配置和简单使用
  • 原文地址:https://www.cnblogs.com/successcai/p/6102039.html
Copyright © 2020-2023  润新知