近期忙了挺久的一个项目,前几天已经实现一个了,但是代码质量并不怎么理想,所以又重构了一遍 。现在看起来还将就着。注释基本没有,但是如果稍微看了一下帮助文档或者是书的段理解起来并不难。可惜现在自己水平不是很高且没太多的时间去弄,不然将这个代码仔细思去重写一遍,运用断言并加上一些判断方法,那样的话人就不需要再去检查一遍,直接看设置产生的log就能轻轻松松实现设置了。
package com;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class Setting_lib extends UiAutomatorTestCase{
public static final int LEFT=0;
public static final int RIGHT=1;
public static final int UP=2;
public static final int DOWN=3;
//**************** system function****************************************************************
// ֱ�ӴӺ�̨����Ӧ��
public static void startActivity(String pack,String activity)
{
Process starter = null;
try
{
starter=Runtime.getRuntime().exec("am start "+pack+"/"+activity);
starter.waitFor();
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(starter!=null)
starter.destroy();
}
}
// ɱ������
public static void stopPackage(String pack)
{
Process starter = null;
try
{
starter=Runtime.getRuntime().exec("am force-stop "+pack);
starter.waitFor();
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(starter!=null)
starter.destroy();
}
}
//**************** click function **********************************************************
/********************************************************************************************/
public static void get_Text(String text )
{
UiObject mObj=new UiObject(new UiSelector().text(text));
if(mObj.exists()) {
try {
mObj.click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
else {}
}
public static boolean find_text(String text)
{
UiObject mObj=new UiObject(new UiSelector().text(text));
if(mObj.exists())
return true;
else
return false;
}
public static UiObject get_description(String strDes) {
UiObject mObject = new UiObject(new UiSelector().description(strDes));
return mObject;
}
public static UiObject get_id(String strId) {
UiObject mObject = new UiObject(new UiSelector().resourceId(strId));
return mObject;
}
public static UiObject get_id_description(String strId, String strdesc) {
UiObject mObject = new UiObject(new UiSelector().resourceId(strId).description(strdesc));
return mObject;
}
public static UiObject get_id_instance(String strId, int strins) {
UiObject mObject = new UiObject(new UiSelector().resourceId(strId).instance(strins));
return mObject;
}
public static UiObject get_id_class_intance(String strId,String strclass, int strins) {
UiObject mObject = new UiObject(new UiSelector().resourceId(strId).className(strclass).instance(strins));
return mObject;
}
public static UiObject get_id_text(String strId,String strtext ) {
UiObject mObject = new UiObject(new UiSelector().resourceId(strId).text(strtext));
return mObject;
}
public static void SLEEP(long time)
{
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void scroll(UiDevice ud,int direction)
{
int w=ud.getDisplayWidth();
int h=ud.getDisplayHeight();
switch(direction)
{
case LEFT:
ud.swipe(w/2+150, h/2, w/2-150, h/2, 5);
break;
case RIGHT:
ud.swipe(w/2-150, h/2, w/2+150, h/2, 5);
break;
case UP:
ud.swipe(w/2, h/2+200, w/2, h/2-200, 5);
break;
case DOWN:
ud.swipe(w/2, h/2-200, w/2, h/2+200, 5);
break;
}
}
}
package com;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import android.R.dimen;
import android.hardware.camera2.CameraManager;
import android.provider.Browser;
public class Auto_Setting17081 extends Setting_lib{
public UiDevice ud;
public void testDemo(){
ud = getUiDevice();
//��֤����
try{
String[] s = getParams().getString("choice").split(",");
int[] t=new int[s.length];
for(int x=0;x<s.length;x++)
{
t[x]=Integer.parseInt(s[x]);
}
//for(int x=0;x<s.length;x++)
//{
switch (t[0]) {
case 0:
Setting(ud);
musicSetting(ud);
photoSetting(ud);
fileSetting(ud);
ebookSetting(ud);
scroll(ud,LEFT);
gameSetting(ud);
qqSetting(ud);
CameraSetting(ud);
break;
case 1:
Setting(ud);
break;
case 2:
musicSetting(ud);
break;
case 3:
photoSetting(ud);
break;
case 4:
fileSetting(ud);
break;
case 5:
ebookSetting(ud);
break;
case 6:
gameSetting(ud);
break;
case 7:
qqSetting(ud);
break;
case 8:
CameraSetting(ud);
break;
case 9:
break;
}
}catch(Exception e){}
}
private void gameSetting(UiDevice d) throws UiObjectNotFoundException {
get_Text("地铁跑酷");
SLEEP(3000);
get_Text("跳过");
for(int i =0; i<10; i++) {
get_Text("始终允许");
}
SLEEP(8000);
d.pressHome();
}
private void qqSetting(UiDevice d) {
try {
get_Text("QQ");
get_Text("允许");
SLEEP(3000);
UiObject login = new UiObject(new UiSelector().resourceId("com.tencent.mobileqq:id/btn_login"));
login.click();
d.pressHome();
get_Text("微信");
for(int i=0;i<5;i++) {
get_Text("始终允许");
SLEEP(500);
}
SLEEP(500);
get_id_text("com.tencent.mm:id/cgw", "登录").click();
get_Text("使用其他方式登录");
d.pressHome();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}finally {
d.pressHome();
}
}
private void CameraSetting(UiDevice d) {
try {
get_Text("相机");
SLEEP(2000);
get_Text("同意并继续");
d.pressBack();
get_Text("浏览器");
SLEEP(2000);
scroll(ud, LEFT);
SLEEP(1000);
scroll(ud, LEFT);
SLEEP(1000);
scroll(ud, LEFT);
SLEEP(500);
scroll(ud, LEFT);
SLEEP(1000);
SLEEP(1000);
d.click(967,104);
SLEEP(1000);
get_Text("同意并继续");
get_id_text("com.android.browser:id/a2", "百度").click();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
private void fileSetting(UiDevice d) {
try {
get_Text("文件管理");
get_Text("手机存储");
get_Text("所有文件");
get_Text("APK续航资源0109");
get_Text("HeKnowNo.mp4");
get_Text("不再提醒");
UiObject mObject = new UiObject(new UiSelector().className("android.view.View").description("视频"));
mObject.click();
d.pressBack();
d.pressBack();
d.pressBack();
d.pressBack();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
private void musicSetting(UiDevice d) {
d.pressHome();
d.pressHome();
try {
get_Text("音乐");
//get_Text("同意并继续").click();
//SLEEP(500);
SLEEP(3000);
get_Text("You Belong With Me");
SLEEP(500);
d.click(473,2026);
SLEEP(500);
get_id("com.oppo.music:id/pause").click();
SLEEP(500);
get_id("com.oppo.music:id/iv_play_mode").click();
d.pressBack();
d.pressBack();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
private void ebookSetting(UiDevice d) {
try {
get_Text("阅读");
SLEEP(2000);
get_id("android:id/button1").click();
SLEEP(3000);
/*
*
get_id_text("com.oppo.reader:id/splash_category_title", "听书•知识").click();
get_id("com.oppo.reader:id/cloud_btn_know").click();
get_id("com.oppo.reader:id/btn_select").click();
get_id("com.oppo.reader:id/iv_close").click();
get_description("bookshelf_button").click();
// get_id("com.oppo.reader:id/actionbar_bookshelf_plus").click();
* */
get_id_text("com.oppo.reader:id/splash_category_title", "听书•知识").click();
get_id("com.oppo.reader:id/btn_select").click();
// get_Text("跳过");
SLEEP(3000);
d.click(182,2048);
SLEEP(1000);
d.click(182,2048);
SLEEP(1000);
d.click(182,2048);
SLEEP(1000);
// get_description("bookshelf_button").click();
SLEEP(1000);
d.click(996,177);
SLEEP(1000);
get_id("com.oppo.reader:id/bookshelf_menu_local").click();
SLEEP(2000);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(1000);
get_id_text("com.oppo.reader:id/text","NormalLife").click();
SLEEP(2000);
d.click(500,1000);
SLEEP(500);
get_Text("设置");
SLEEP(1000);
ud.swipe(733, 1800, 199, 1850, 10);
SLEEP(500);
get_Text("更多设置 > >");
get_id_text("android:id/title","屏幕关闭时间").click();
get_id_text("android:id/text1","常亮").click();
get_id_text("android:id/title","休息提醒").click();
get_id_text("android:id/text1","关闭").click();
get_id_class_intance("com.oppo.reader:id/switchWidget", "android.widget.Switch", 3).click();
SLEEP(1000);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(500);
get_id_text("android:id/title","同步阅读进度和想法").click();
get_id_text("android:id/title","WiFi下字体自动下载").click();
d.pressBack();
d.pressBack();
d.pressBack();
d.pressBack();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}finally {
d.pressHome();
}
}
public void photoSetting(UiDevice d) {
d.pressHome();
d.pressHome();
try {
get_Text("相册");
// get_Text("同意并继续").click();
SLEEP(1000);
d.click(535,1735);
SLEEP(500);
d.pressHome();
get_Text("手机管家");
get_Text("权限隐私");
get_id("android:id/title").click();
get_id_text("android:id/title","发送短信").click();
get_id_text("com.coloros.securitypermission:id/name", "地铁跑酷").click();
get_id_text("com.coloros.securitypermission:id/list_title", "禁止").click();
d.pressHome();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
public void Setting(UiDevice d) {
try {
d.pressHome();
scroll(d,UP);
SLEEP(500);
scroll(d,LEFT);
SLEEP(500);
scroll(d,RIGHT);
get_Text("设置");
get_id_text("android:id/title","双卡与移动网络").click();
SLEEP(1000);
d.click(505, 391);
SLEEP(500);
get_id_class_intance("android:id/switch_widget","android.widget.Switch",2).click();
d.pressBack();
get_id_text("android:id/title","每日流量提醒").click();
get_id_text("com.coloros.simsettings:id/text1","100 GB").click();
get_id_text("android:id/title","使用 WLAN 与移动网络的应用").click();
SLEEP(1000);
d.click(536, 550);
SLEEP(500);
d.pressBack();
get_id_text("com.coloros.simsettings:id/title","地铁跑酷").click();
get_id_text("com.coloros.simsettings:id/text1","WLAN 与移动数据").click();
get_id_text("com.coloros.simsettings:id/title","QQ").click();
get_id_text("com.coloros.simsettings:id/text1","WLAN 与移动数据").click();
SLEEP(1000);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(1000);
get_id_text("com.coloros.simsettings:id/title","微信").click();
get_id_text("com.coloros.simsettings:id/text1","WLAN 与移动数据").click();
d.pressBack();
d.pressBack();
get_id_text("android:id/title","通知与状态栏").click();
// get_id("android:id/switch_widget").click();
get_id("android:id/title").click();
SLEEP(2000);
d.click(1036, 1811);
SLEEP(1000);
get_id_text("com.coloros.notificationmanager:id/app_name","信息").click();
get_id("android:id/switch_widget").click();
d.pressBack();
d.pressBack();
d.pressBack();
get_id_text("android:id/title","显示与亮度").click();
get_id("android:id/switch_widget").click();
get_id_text("android:id/title","自动息屏").click();
get_id_text("android:id/title","30 分钟").click();
d.pressBack();
get_id_text("android:id/title","桌面与锁屏杂志").click();
get_id_class_intance("android:id/switch_widget","android.widget.Switch",1).click();
d.pressBack();
SLEEP(500);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(500);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(500);
get_id_text("android:id/title","电话").click();
get_id_text("android:id/title","骚扰拦截").click();
get_id("android:id/title").click();
get_id_class_intance("android:id/switch_widget","android.widget.Switch",2).click();
get_id("android:id/button1").click();
d.pressBack();
d.pressBack();
d.pressBack();
get_id_text("android:id/title","手机管家").click();
get_id_text("android:id/title","自动体检频率").click();
get_id_text("com.coloros.phonemanager:id/list_title","关闭").click();
get_id("com.coloros.phonemanager:id/switchWidget").click();
get_id_class_intance("com.coloros.phonemanager:id/switchWidget","android.widget.Switch",1).click();
get_id_class_intance("com.coloros.phonemanager:id/switchWidget","android.widget.Switch",2).click();
d.pressBack();
SLEEP(500);
ud.swipe(500, 1800, 500, 300, 100);
SLEEP(500);
get_id_text("android:id/title","软件商店").click();
get_id_class_intance("com.oppo.market:id/switchWidget","android.widget.Switch",1).click();
d.pressBack();
get_id_text("android:id/title","游戏中心").click();
get_id_class_intance("com.nearme.gamecenter:id/switchWidget","android.widget.Switch",1).click();
d.pressBack();
get_id_text("android:id/title","主题商店").click();
get_id_class_intance("com.nearme.themespace:id/jl","android.widget.Switch",1).click();
d.pressBack();
get_id_text("android:id/title","浏览器").click();
get_id_description("android:id/checkbox","允许网站请求获取位置信息").click();
get_id_description("android:id/checkbox","获取消息通知和最新资讯").click();
get_id_description("android:id/checkbox","自动全屏播放网页视频").click();
get_id_description("android:id/checkbox","恢复上次浏览页面提醒").click();
get_id_description("android:id/checkbox","复制内容快捷访问搜索").click();
d.pressBack();
d.pressHome();
} catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
}
}