UiDevice新增API
2015-11-22 00:52
501 查看
InstrumentationRegistry 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参数 |
UiDevices新增API
返回类型 | API | 解释 |
---|---|---|
void | dumpWindowHierarchy(OutputStream out) | 获取当前页面层级到输出流(OutputStream) |
java.lang.String | executeShellCommand() | 执行shell命令使用shell用户身份,并返回字符串的标准输出 |
UiObject2 | findObject(BySelector selector) | 返回符合选择条件的第一个对象 |
UiObject | findObject(UiSelector selector) | 返回符合选择条件的UiObject |
List<UiObject2> | findObjects(BySelector selector) | 返回所有符合选择条件的对象 |
<R> R | wait(SearchCondition<R> condition, long timeout) | 等待的条件得到满足 |
package mqqzt.myuitest3; import android.app.Instrumentation; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.Environment; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.By; import android.support.test.uiautomator.UiDevice; import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.support.test.uiautomator.Until; import android.widget.TextView; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.List; /** * Created by mqqzt-yang on 2015/11/22. */ @RunWith(AndroidJUnit4.class) public class UiDeviceTest { UiDevice uiDevice; Instrumentation instrumentation; @Before public void setUp(){ uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); instrumentation = InstrumentationRegistry.getInstrumentation(); uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); } @After public void tearDown(){ uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); } @Test public void testInstrumentation(){ //获取运行Intrumentation命令时,传入的参数 Bundle bundle = InstrumentationRegistry.getArguments(); //把获取到的参数发送到测试结果中 instrumentation.sendStatus(888, bundle); Context curContext = InstrumentationRegistry.getContext(); Context tarContext = InstrumentationRegistry.getTargetContext(); PackageManager pm = curContext.getPackageManager(); Bundle contextBundle = new Bundle(); contextBundle.putString("CUR_CONTEXT", pm.getInstalledPackages(PackageManager.GET_ACTIVITIES).get(0).packageName); InstrumentationRegistry.registerInstance(instrumentation, contextBundle); Bundle b2 = InstrumentationRegistry.getArguments(); instrumentation.sendStatus(889, b2); } @Test public void testUiDeviceNewAPI() throws IOException, UiObjectNotFoundException { File file = new File(Environment.getExternalStorageDirectory() + File.separator + "uidump.xml"); if(file.exists()){ file.delete(); try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } }else { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } OutputStream os = new FileOutputStream(file); uiDevice.dumpWindowHierarchy(os); uiDevice.executeShellCommand("am start -n com.android.settings/.Settings"); uiDevice.wait(Until.findObject(By.text("Display")), 20000); UiObject2 uiObject2 = uiDevice.findObject(By.text("Display")); uiObject2.click(); UiObject uiObject = uiDevice.findObject(new UiSelector().text("Wallpaper")); uiObject.clickAndWaitForNewWindow(); Bundle bundle = new Bundle(); List<UiObject2> list = uiDevice.findObjects(By.clazz(TextView.class)); for(UiObject2 uiObject21: list){ bundle.putString("UIDEVICE", uiObject21.getText()); } InstrumentationRegistry.registerInstance(instrumentation, bundle); instrumentation.sendStatus(111, bundle); } @Test public void testLauncherAPP(){ Context context = InstrumentationRegistry.getContext(); PackageManager pm = context.getPackageManager(); List<ApplicationInfo> list = pm.getInstalledApplications(0); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> resolveInfoList = pm.queryIntentActivities(mainIntent, 0); for(ResolveInfo resolveInfo : resolveInfoList){ String activityName = resolveInfo.activityInfo.name; String packageName = resolveInfo.activityInfo.packageName; Intent launcherIntent = new Intent(); launcherIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); launcherIntent.setComponent(new ComponentName(packageName, activityName)); context.startActivity(launcherIntent); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); uiDevice.pressBack(); } } }
相关文章推荐
- HDU 2767 Proving Equivalences(强连通缩点)
- EasyUI常用控件的禁用方法
- request biji
- leetcode笔记:N-Queens II
- Druid监控数据库
- duilib 入门一之界面库基本原理
- 如何解决virtualbox中不能打开一个虚拟任务之uuid不匹配的问题
- 移植opencv到pcDuino
- arduino ide找不到目标文件
- HDU-5568 sequence2(DP+高精度)
- HDU5568/BestCoder Round #63 (div.2) B.sequence2 dp+高精度
- HDU5567/BestCoder Round #63 (div.2) A sequence1 水
- hdoj5567sequence1
- UILabel,UITextField,UIButton三大基础控件总结
- BestCoder Round #63 (div.1) A.sequence2
- UItableView
- UESTC 1217 The Battle of Chibi (树状数组 + 离散化 + 动态规划)
- sequence1(暴力)
- hdoj sequence1 5567 (简单暴力)
- HDOJ 5567 sequence1