Android UiAutomator锁定设备并停止响应adb命令
我目前正试图通过UIautomator创建一个非常简单的UI转储程序,但有一些非常奇怪的问题。在我目前的设计中,我使用nanohttpd与我的apk通信 因此,我的uiautomator测试如下所示:Android UiAutomator锁定设备并停止响应adb命令,android,android-uiautomator,nanohttpd,Android,Android Uiautomator,Nanohttpd,我目前正试图通过UIautomator创建一个非常简单的UI转储程序,但有一些非常奇怪的问题。在我目前的设计中,我使用nanohttpd与我的apk通信 因此,我的uiautomator测试如下所示: @RunWith(AndroidJUnit4.class) @SdkSuppress(minSdkVersion = 18) public class Start { public static final String Tag = "UiDuper"; private int
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {
public static final String Tag = "UiDuper";
private int port;
private UiAutomatorHttpServer server;
public Start() {
Bundle extras = InstrumentationRegistry.getArguments();
port = 9008;
if(extras.containsKey("port")) {
port = Integer.parseInt(extras.getString("port"));
Log.i(Tag, "UIAutomator server port is " + port);
}
}
@Before
public void setUp() throws IOException, RemoteException {
Log.i(Tag, "Start server with port " + port);
server = new UiAutomatorHttpServer(port);
server.start();
}
@After
public void TearDown() {
Log.i(Tag, "Tear down server");
if(server != null) {
server.stop();
}
}
@Test
@LargeTest
public void RunServer() throws InterruptedException {
while(true) {
Thread.sleep(100);
if(server == null)
return;
if(!server.isAlive()) {
return;
}
if(server.ShouldStopServer()) {
return;
}
}
}
}
public class UiAutomatorHttpServer extends NanoHTTPD {
private final String stop = "/stop";
private final String ping = "/ping";
private final String dump = "/dump";
private UiAutomatorService service;
private boolean shouldStopServer;
public UiAutomatorHttpServer(int port) throws RemoteException {
super(port);
service = new UiAutomatorService();
stop = false;
}
@Override
public Response serve(IHTTPSession session) {
Log.i(Start.Tag, "New request");
if(session.getUri().equals(ping)) {
Log.i(Start.Tag, "Sending ping response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
}
if(session.getUri().equals(dump)) {
Log.i(Start.Tag, "Sending dump response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, service.dumpWindowHierarchy());
}
if(session.getUri().equals(stop)) {
stopServer();
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
}
Log.e(Start.Tag, "Something went wrong!");
Log.e(Start.Tag, session.getUri());
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
}
public void stopServer() {
Log.i(Start.Tag, "Stopping server");
shouldStopServer = true;
}
public boolean ShouldStopServer() {
return shouldStopServer;
}
}
adb shell am instrument -w -r -e debug false -e port 9030 -e class com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
我的“服务器”如下所示:
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {
public static final String Tag = "UiDuper";
private int port;
private UiAutomatorHttpServer server;
public Start() {
Bundle extras = InstrumentationRegistry.getArguments();
port = 9008;
if(extras.containsKey("port")) {
port = Integer.parseInt(extras.getString("port"));
Log.i(Tag, "UIAutomator server port is " + port);
}
}
@Before
public void setUp() throws IOException, RemoteException {
Log.i(Tag, "Start server with port " + port);
server = new UiAutomatorHttpServer(port);
server.start();
}
@After
public void TearDown() {
Log.i(Tag, "Tear down server");
if(server != null) {
server.stop();
}
}
@Test
@LargeTest
public void RunServer() throws InterruptedException {
while(true) {
Thread.sleep(100);
if(server == null)
return;
if(!server.isAlive()) {
return;
}
if(server.ShouldStopServer()) {
return;
}
}
}
}
public class UiAutomatorHttpServer extends NanoHTTPD {
private final String stop = "/stop";
private final String ping = "/ping";
private final String dump = "/dump";
private UiAutomatorService service;
private boolean shouldStopServer;
public UiAutomatorHttpServer(int port) throws RemoteException {
super(port);
service = new UiAutomatorService();
stop = false;
}
@Override
public Response serve(IHTTPSession session) {
Log.i(Start.Tag, "New request");
if(session.getUri().equals(ping)) {
Log.i(Start.Tag, "Sending ping response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
}
if(session.getUri().equals(dump)) {
Log.i(Start.Tag, "Sending dump response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, service.dumpWindowHierarchy());
}
if(session.getUri().equals(stop)) {
stopServer();
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
}
Log.e(Start.Tag, "Something went wrong!");
Log.e(Start.Tag, session.getUri());
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
}
public void stopServer() {
Log.i(Start.Tag, "Stopping server");
shouldStopServer = true;
}
public boolean ShouldStopServer() {
return shouldStopServer;
}
}
adb shell am instrument -w -r -e debug false -e port 9030 -e class com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
在设备上安装apk后,我将执行以下操作:
adb forward tcp:9008 tcp:9030
然后我可以像这样启动服务器:
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {
public static final String Tag = "UiDuper";
private int port;
private UiAutomatorHttpServer server;
public Start() {
Bundle extras = InstrumentationRegistry.getArguments();
port = 9008;
if(extras.containsKey("port")) {
port = Integer.parseInt(extras.getString("port"));
Log.i(Tag, "UIAutomator server port is " + port);
}
}
@Before
public void setUp() throws IOException, RemoteException {
Log.i(Tag, "Start server with port " + port);
server = new UiAutomatorHttpServer(port);
server.start();
}
@After
public void TearDown() {
Log.i(Tag, "Tear down server");
if(server != null) {
server.stop();
}
}
@Test
@LargeTest
public void RunServer() throws InterruptedException {
while(true) {
Thread.sleep(100);
if(server == null)
return;
if(!server.isAlive()) {
return;
}
if(server.ShouldStopServer()) {
return;
}
}
}
}
public class UiAutomatorHttpServer extends NanoHTTPD {
private final String stop = "/stop";
private final String ping = "/ping";
private final String dump = "/dump";
private UiAutomatorService service;
private boolean shouldStopServer;
public UiAutomatorHttpServer(int port) throws RemoteException {
super(port);
service = new UiAutomatorService();
stop = false;
}
@Override
public Response serve(IHTTPSession session) {
Log.i(Start.Tag, "New request");
if(session.getUri().equals(ping)) {
Log.i(Start.Tag, "Sending ping response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
}
if(session.getUri().equals(dump)) {
Log.i(Start.Tag, "Sending dump response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, service.dumpWindowHierarchy());
}
if(session.getUri().equals(stop)) {
stopServer();
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
}
Log.e(Start.Tag, "Something went wrong!");
Log.e(Start.Tag, session.getUri());
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
}
public void stopServer() {
Log.i(Start.Tag, "Stopping server");
shouldStopServer = true;
}
public boolean ShouldStopServer() {
return shouldStopServer;
}
}
adb shell am instrument -w -r -e debug false -e port 9030 -e class com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
然后像这样转储UI:
@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 18)
public class Start {
public static final String Tag = "UiDuper";
private int port;
private UiAutomatorHttpServer server;
public Start() {
Bundle extras = InstrumentationRegistry.getArguments();
port = 9008;
if(extras.containsKey("port")) {
port = Integer.parseInt(extras.getString("port"));
Log.i(Tag, "UIAutomator server port is " + port);
}
}
@Before
public void setUp() throws IOException, RemoteException {
Log.i(Tag, "Start server with port " + port);
server = new UiAutomatorHttpServer(port);
server.start();
}
@After
public void TearDown() {
Log.i(Tag, "Tear down server");
if(server != null) {
server.stop();
}
}
@Test
@LargeTest
public void RunServer() throws InterruptedException {
while(true) {
Thread.sleep(100);
if(server == null)
return;
if(!server.isAlive()) {
return;
}
if(server.ShouldStopServer()) {
return;
}
}
}
}
public class UiAutomatorHttpServer extends NanoHTTPD {
private final String stop = "/stop";
private final String ping = "/ping";
private final String dump = "/dump";
private UiAutomatorService service;
private boolean shouldStopServer;
public UiAutomatorHttpServer(int port) throws RemoteException {
super(port);
service = new UiAutomatorService();
stop = false;
}
@Override
public Response serve(IHTTPSession session) {
Log.i(Start.Tag, "New request");
if(session.getUri().equals(ping)) {
Log.i(Start.Tag, "Sending ping response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Hello human.");
}
if(session.getUri().equals(dump)) {
Log.i(Start.Tag, "Sending dump response");
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, service.dumpWindowHierarchy());
}
if(session.getUri().equals(stop)) {
stopServer();
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "Stop");
}
Log.e(Start.Tag, "Something went wrong!");
Log.e(Start.Tag, session.getUri());
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "Not found.");
}
public void stopServer() {
Log.i(Start.Tag, "Stopping server");
shouldStopServer = true;
}
public boolean ShouldStopServer() {
return shouldStopServer;
}
}
adb shell am instrument -w -r -e debug false -e port 9030 -e class com.dumper.screenDumperServer.Start#RunServer com.dumper.screenDumperServer.test/android.support.test.runner.AndroidJUnitRunner
问题是它似乎在一段时间后停止工作,我可以看到以下情况:
- 停止回应
- 所有adb命令停止工作(adb logcat、adb shell等)。他们没有给出任何错误,但什么也没有发生李>
- 插入和拔出usb(大部分时间可以工作)
- 重新启动设备