atx860和java_APP自动化框架-ATX原理解析及JAVA版客户端

packagecom.github.uiautomator.stub;importandroid.content.Context;importandroid.content.Intent;importandroid.os.RemoteException;importandroid.support.test.InstrumentationRegistry;importandroid.support.test.filters.LargeTest;importandroid.support.test.filters.SdkSuppress;importandroid.support.test.runner.AndroidJUnit4;importandroid.support.test.uiautomator.By;importandroid.support.test.uiautomator.Configurator;importandroid.support.test.uiautomator.UiDevice;importandroid.support.test.uiautomator.Until;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.googlecode.jsonrpc4j.JsonRpcServer;importorg.junit.After;importorg.junit.Before;importorg.junit.Test;importorg.junit.runner.RunWith;/*** Use JUnit test to start the uiautomator jsonrpc server.

*

*@authorxiaocong@gmail.com*/@RunWith(AndroidJUnit4.class)

@SdkSuppress(minSdkVersion= 18)public classStub {private final String TAG = "UIAUTOMATOR";private static final int LAUNCH_TIMEOUT = 5000;int PORT = 9008;

AutomatorHttpServer server= newAutomatorHttpServer(PORT);

@Beforepublic void setUp() throwsException {

launchService();//这是关键核心代码,把AutomatorService使用jsonRpcServer进行反射处理

server.route("/jsonrpc/0", new JsonRpcServer(new ObjectMapper(), new AutomatorServiceImpl(), AutomatorService.class));

server.start();

}private voidlaunchPackage(String packageName) {

Log.i(TAG,"Launch " +packageName);

UiDevice device=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

Context context=InstrumentationRegistry.getContext();final Intent intent =context.getPackageManager()

.getLaunchIntentForPackage(packageName);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);

context.startActivity(intent);

device.wait(Until.hasObject(By.pkg(packageName).depth(0)), LAUNCH_TIMEOUT);

device.pressHome();

}private void launchService() throwsRemoteException {

UiDevice device=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

Context context=InstrumentationRegistry.getContext();

device.wakeUp();//Wait for launcher

String launcherPackage =device.getLauncherPackageName();

Boolean ready= device.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);if (!ready) {

Log.i(TAG,"Wait for launcher timeout");return;

}

Log.d("Launch service");

context.startService(new Intent("com.github.uiautomator.ACTION_START"));//Reset Configurator Wait Timeout

Configurator configurator =Configurator.getInstance();

configurator.setWaitForSelectorTimeout(0L);//BUG(uiautomator): setWaitForIdleTimeout is useless//Refs:https://www.ydkf.me/archives/22

}

@Afterpublic voidtearDown() {

server.stop();

Context context=InstrumentationRegistry.getContext();

context.startService(new Intent("com.github.uiautomator.ACTION_STOP"));

}

@Test

@LargeTestpublic void testUIAutomatorStub() throwsInterruptedException {while(server.isAlive()) {

Thread.sleep(100);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值