http://www.cnblogs.com/by-dream/p/4996000.html  上面是别人的写法

我自己的写法:

package qq.test;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until; import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith; import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat; /**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
* http://blog.sina.com.cn/s/blog_51335a0001017x8v.html see LargeTest
* http://blog.csdn.net/zr940326/article/details/51586789
*/ @RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest { private static final String BASIC_SAMPLE_PACKAGE
= "com.android.music";
private static final int LAUNCH_TIMEOUT = 5000;
private static final String STRING_TO_BE_TYPED = "UiAutomator";
private UiDevice mDevice;
UiautomatorAssistant uiautomatorAssistant ; @BeforeClass
public static void testBeforeClass()
{
System.out.println("Public Static void testBeforeClass");
} @Before
public void startMainActivityFromHomeScreen() throws Exception
{
// Intialize UiDevice instance your android device
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // start from the home screen // press the home key
mDevice.pressHome(); // wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage,notNullValue());
// wait for the music can see in the screen
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT); // launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent); // Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
} @Test
public void PictureTest() throws Exception
{
/*
UiObject musicTabButton = mDevice.findObject(new UiSelector()
.resourceId("com.android.music:id/songtab"));
if(!musicTabButton.isSelected() && musicTabButton.exists() && musicTabButton.isEnabled())
musicTabButton.click(); UiCollection musicList = new UiCollection(new UiSelector()
.className("android.widget.RelativeLayout"));
UiObject music = musicList.getChild(new UiSelector()
.index(3));
if(!music.isSelected()) music.click();
*/
uiautomatorAssistant = new UiautomatorAssistant(mDevice);
uiautomatorAssistant.ClickById("com.android.music:id/songtab");
uiautomatorAssistant.ClickByCollCLASS("android.widget.RelativeLayout","com.android.music:id/line1","3");
} @After
public void tearDown() throws Exception {
}
} /*
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class ChangeTextBehaviorTest { private static final String BASIC_SAMPLE_PACKAGE
= "com.android.music";
private static final int LAUNCH_TIMEOUT = 5000;
private static final String STRING_TO_BE_TYPED = "UiAutomator";
private UiDevice mDevice; public void startMainActivityFromHomeScreen() {
// Initialize UiDevice instance
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); // Start from the home screen
mDevice.pressHome(); // Wait for launcher
final String launcherPackage = mDevice.getLauncherPackageName();
assertThat(launcherPackage, notNullValue());
mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)),
LAUNCH_TIMEOUT); // Launch the app
Context context = InstrumentationRegistry.getContext();
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);
// Clear out any previous instances
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent); // Wait for the app to appear
mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(0)),
LAUNCH_TIMEOUT);
}
@Test
public void testMusic(){
startMainActivityFromHomeScreen();
} }
*/

 

封装代码类:

package qq.test;

import android.support.test.uiautomator.UiCollection;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector; /**
* Created by Administrator on 2017/5/11.
*/ public class UiautomatorAssistant {
UiDevice mdevice;
public String m_logpathString = "/mnt/sdcard/PerformanceLog.txt"; final static int CLICK_ID = 1000;
final static int CLICK_TEXT = 1001;
final static int CLICK_CLASS = 1002;
UiautomatorAssistant(UiDevice device)
{
mdevice = device;
}// struct function
public boolean ClickById(String id)
{
return ClickByInfo(CLICK_ID, id);
}
public boolean ClickByText(String text)
{
return ClickByInfo(CLICK_TEXT, text);
}
public boolean ClickByCollCLASS(String classname,String id,String text)
{
return ClickByCollInfo(CLICK_CLASS,classname,id,text);
}
private boolean ClickByCollInfo(int CLICK, String classname, String id, String text) { UiSelector uiselector = null;
UiCollection musicList = null;
switch (CLICK)
{
case CLICK_CLASS:
musicList = new UiCollection(new UiSelector().className(classname));
uiselector = new UiSelector().resourceId(id).text(text);
break;
default:
return false;
}
UiObject uiobject = null;
try {
uiobject = musicList.getChild(uiselector);
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
int i = 0;
if(!uiobject.exists()) return false;
try {
uiobject.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
return true;
} private boolean ClickByInfo(int CLICK, String str)
{
UiSelector uiselector = null;
switch (CLICK)
{
case CLICK_ID:
uiselector = new UiSelector().resourceId(str);
break;
case CLICK_TEXT:
uiselector = new UiSelector().text(str);
break;
default:
return false;
}
// UiObject uiobject = new UiObject(uiselector);
UiObject uiobject = mdevice.findObject(uiselector);
int i = 0;
while (!uiobject.exists() && i < 5)
{
//SolveProblems();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
} if (i == 4) {
//TakeScreen(str + "-not-find");
return false;
}
i++;
}
try {
//UiAutomationLog("Click type:" + CLICK + "content:" + str);
uiobject.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
return true;
} }

  

Uiautomator ---(1) 封装代码的更多相关文章

  1. 七、Block 封装代码

    1.概念:封装代码块,调用的时候使用 2.声明 返回类型(^名字)(参数1,参数2..) = (参数类型 变量1,参数类型, 变量2){ }; int (^Sum)(int,int)  = ^(int ...

  2. [Effective JavaScript 笔记]第27条:使用闭包而不是字符串来封装代码

    函数是一种将代码作为数据结构存储的便利方式,代码之后可以被执行.这使得富有表现力的高阶函数抽象如map和forEach成为可能.它也是js异步I/O方法的核心.与此同时,也可以将代码表示为字符串的形式 ...

  3. python解析xml模块封装代码

    在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...

  4. python网页请求urllib2模块简单封装代码

    这篇文章主要分享一个python网页请求模块urllib2模块的简单封装代码. 原文转自:http://www.jbxue.com/article/16585.html 对python网页请求模块ur ...

  5. <<海闻电子发票接口 ESB 封装 代码指示 文档>>

    <<海闻电子发票接口 ESB 封装 代码指示 文档>> isValid 是否有效标志 代码 中文 说明 true 成功 false 失败   code 海闻错误说明 代码 中文 ...

  6. Ajax--json(Ajax调用返回json封装代码、格式及注意事项)

    Ajax调用json封装代码<dbda.php>: //Ajax调用返回JSON public function JsonQuery($sql,$type=1,$db="mydb ...

  7. jdbc封装代码

    jdbc封装代码 package jdbcUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...

  8. (转载)Android支付宝支付封装代码

    Android支付宝支付封装代码 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-12-22我要评论 这篇文章主要介绍了Android支付宝支付封装代码,Android支付的时候 ...

  9. 51book机票接口对接,吐血整理(含PHP封装代码)

    前言 最近在对接51book的机票接口,遇到了挺多坑,所以整理一份作为记录 机票有两个不同的接口,一个是机票,另一个是保险 一.申请 要接51book的机票,首先是要申请账号,这时候应该是有客户经理跟 ...

随机推荐

  1. Git在Xcode中的配置与使用常见问题总结

    书接上回提出的Git在Xcode中的配置与使用常见问题4个问题 问题1,如何在Xcode中创建代码库,并添加和提交代码到代码库? 问题2,如何在Xcode中提交推送给远程服务器代码库? 问题3,如何在 ...

  2. 小目标 | DAX高级实践-Power BI与Excel联合应用

    · 适用人群:数据分析专业人士,在数据分析方向需求发展人士 · 应用场景:数据汇报.数据可视化展现.数据建模分析 · 掌握难度:★★★★☆ 本期讲师 DAX高级实践-Power BI与Excel联合应 ...

  3. 使用poi或jxl,通过java读写xls、xlsx文档

    package nicetime.com.baseutil; import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffExcepti ...

  4. HDU 1124 Factorial (阶乘后缀0)

    题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...

  5. oracle 11g r2安装

    安装环境:windows 7 安装版本:Oracle_win32_11gR2 目的:用于模拟服务器环境,学习Oracle操作 1. 下载oracle 11g r2,下载地址:www.oracle.co ...

  6. iOS动画——Layer Animations

    我们先来看一下今天我们要实现的效果,今天实现的效果用第一篇View Animations能实现相同效果. 动画由书籍<iOS Animations by tutorials>提供,我只是一 ...

  7. HDU 5459 Jesus Is Here (递推,组合数学)

    有点麻烦的递推,递推的原则:向小的问题方向分解,注意边界. 字符串的递推式为 定义f为Si中的总方案数 首先可以得到 fi=fi-1+fi-2+组合(si-2,si-1) 然后考虑Si-2和Si-1之 ...

  8. [神经网络]一步一步使用Mobile-Net完成视觉识别(五)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第五篇,讲解如何调用测试训练结果. 上一篇中我们输出了训练的模型,这一篇中我们通过调用训练好的模型来完 ...

  9. 爬虫3_python2

    # coding=utf-8 import urllib params=urllib.urlencode({'t':1,'eggs':2,'bacon':0})#现在大多数网站都是动态网页,需要你动态 ...

  10. javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件

    有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...