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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM