xiaocong / uiautomator

Python wrapper of Android uiautomator test tool.
MIT License
2.03k stars 638 forks source link

"RPC server not started" persistant issue with only certain devices #210

Open aristeia opened 7 years ago

aristeia commented 7 years ago

Hi, I've reviewed some of the other issues surrounding the "RPC server not started" error, but none seem to get at the issue I'm encountering. My issue is that a small number of device models will always return the "RPC server not started" error. Despite successfully using UIAutomator with 20+ device models on Ubuntu 16.04 running python3, I've found that my two ZTE Z981 devices won't connect with UIAutomator. Reinstalling the APKs and jars on the phones didnt help. I'll look more into whatever logs they have, and post them on here.

soppag commented 7 years ago

I have similar issues with Samsung devices from variety of range. A5, S6 (edge, edge+), S7 etc, basically new ones. UiAutomator starts running test cases but somehow it freezes totally and only way is to unplug device from the USB and/or re-enable USB debugging from the dev options.

As a reference; S5 Neo works like a charm and also top tier Samsung Tablets.

Also seems that there are issues with ADB since 'adb devices' shows device as 'offline'.

aristeia commented 7 years ago

Hey @soppag, I'm not sure if this is the same issue. My S6 edges and edge+s, as well as S7s have never frozen during a test case. Try looking into your connection to those devices, the cables, ports, etc, since it sounds like something is disconnecting them during the tests.

siva-kranthi commented 7 years ago

@soppag Update adb. That may resolve the issue

snakeli commented 7 years ago

I's error while installing the test apk, and I check the code, in the method "install"(init.py): "self.adb.cmd("install", "-r -t", os.path.join(base_dir, apk)).wait()" I think it should be "self.adb.cmd("install", "-r", "-t", os.path.join(base_dir, apk)).wait()"

And it is OK after I modify this code.

soppag commented 7 years ago

After changing as  @snakeli suggested (Cheers mate!), i've been able to run automation with S7 edge+

This should be fixed in repo asap.

aristeia commented 7 years ago

Unfortunately, that fix didnt solve my original issue :(

The APK is probably installing correctly for me, but the issue is in the APK starting and communicating with ADB over this "RPC server".

I've checked ADB's logs, and found that all the same relevant UIAutomator stuff runs on the non-working phone as a working phone. The last line in common they both run is:

TestRunner: started: testUIAutomatorStub(com.github.uiautomator.stub.Stub)

After this line, a working phone will then print some JSON objects relevant to the RPC requests like ping(), but the nonworking phone will never print any more relevant stuff.

aristeia commented 7 years ago

Also did some other platform debugging, and replicated the error with Python2.7 and on macOS, so it's definitely an issue isolated to the ADB-UIAutomator stack

snakeli commented 7 years ago

Hi @aristeia , Maybe below link's infomation can help you. http://p.codekk.com/detail/Android/xiaocong/uiautomator

After install the APKs,

  1. adb shell am instrument -w com.github.uiautomator.test/android.support.test.runner.AndroidJUnitRunner
  2. adb shell curl -d '{"jsonrpc":"2.0","method":"deviceInfo","id":1}' localhost:9008/jsonrpc/0 By this , you can check whether the server is alive.

There's only one place where will raise "RPC server not started" error in this code: def start(self, timeout=5): ... if not self.alive: raise IOError("RPC server not started!") If you install the APKs correctly, maybe that's because your DUT kill the server, check your adb log.

aristeia commented 7 years ago

Thanks for the suggestion. I've ran into some problems with adb's curl command where it doesnt parse the command line option for data properly:

$ adb shell curl -d '{"jsonrpc":"2.0","method":"deviceInfo","id":1}' -v localhost:9008/jsonrpc/0

I tried it with the --header option too, and it parsed the header the same way, interpreting it as a host. I even tried replacing all the quotes with double quotes, then escaping them in the JSON, but even then curl misinterpreted the data option.

Anyway, from what I said before, I've looked over ADB's logs, and it doesnt appear that the server is being killed. If I can get this curl command working, I could probably debug this a bit more deeply :)

aristeia commented 7 years ago

Got it working! I enclosed all the arguments in a big single-quotes quote, and it works fine. The non-working phone returns a response just as quickly as a working phone:

$ adb shell curl '-d "{\"jsonrpc\":\"2.0\",\"method\":\"ping\",\"id\":1}" -v --header "ContentType: application.json"' localhost:9008/jsonrpc/0

I'll do a bit more debugging around why in the python code we don't get a response, despite the server being online and serving responses to curl. Thanks again @snakeli

aristeia commented 7 years ago

I've found that, when running an instance of uiautomator, I get the following results when trying to ping() ports:

working phone:

pinging the local port pinging the device port
through the command line with curl Failed to connect to localhost port 9009: Connection refused Works
letting python/uiautomator do it Works [Errno 111] Connection refused

non-working phone:

pinging the local port pinging the device port
through the command line with curl Failed to connect to localhost port 9009: Connection refused Works
letting python/uiautomator do it [Errno 111] Connection refused [Errno 104] Connection reset by peer

For reference, the local port is the port though which UIAutomator sends post requests to the device. When we ping this port, I think it should work regardless of if we're using python/uiautomator or the command line.

soppag commented 7 years ago

Seems that the install method workaround wasn't the solution - seems that if you just leave the device attached to USB for longer period like overnight. Next morning none of the adb commands work. you basically have to restart adb server.

After restarting adb server - 'adb devices' shows my device as offline.

Only solution to get it back online is to unplug USB and put it back on.

soppag commented 7 years ago

Seems that ADB really gets stuck if you just have device attached to USB and instrument(s) running AndroidJUnitRunner with S6 edge+

ADB version: Android Debug Bridge version 1.0.39 Revision 5943271ace17-android

UIAutomator version (0.2.7) + patched self.install

siva-kranthi commented 7 years ago

upgrade UIAutomator. Latest version is 0.3.2

siva-kranthi commented 7 years ago

We also observed same issue with some of the devices which has initially rlsed android binaries like N. Not sure exactly what was the issue. But seems to work after few days with new binaries

soppag commented 7 years ago

Seems that 0.3.2 version is much more slower when executing test case. Ui clicks etc. are very slow compared to 0.2.7. Any idea?

yanglikai0806 commented 7 years ago

I meet same problem , and resolved it . I find "def stop()" in init.py do not worked, maybe beacause of my phone is not rooted, so i change it as : def stop(self): package_names = ["com.github.uiautomator","com.github.uiautomator.test"] for pkg in package_names: self.adb.cmd("uninstall", pkg).wait() when meet rpc service stoped, will uninstall apks, than reinstall. so , rpc service can be restarted.

aristeia commented 7 years ago

@yanglikai0806 Unfortunately I've already tried this. From another thread about the RPC server not starting, they mention some ADB commands to manually uninstall and reinstall the uiautomator apk and other apks, but that didnt help my problem :(

craftpip commented 6 years ago

@snakeli that code change worked like a charm, you're a wizard.

willysys commented 6 years ago

I also meet offline problem. uiautomator version is 0.3.2 .

lmario35 commented 5 years ago

Hello,

I have the same problem with different products (Huawei P20, Xiaomi Redmi 5, Huawei Y6 2018...). ADB version : Android Debug Bridge version 1.0.40 Version 4986621 uiauomator version: 0.3.2

error: raise IOError("RPC server not started!") OSError: RPC server not started!

AgasthyaT commented 4 years ago

I have the same issue and I observed the issue for all Android Pie devices. The issue was not reproducible on Android Nougat. Not Sure why..

whyslience commented 9 months ago

Who can help me ? I see the same situation

pmb-itz commented 9 months ago
  1. It seems it is a floating issue and it is related to device initialization via adb
  2. It can be bypassed by adding additional "adb connect" call from cli. In this case workaround looks like:
from uiautomator import Device
import os

device = "192.168.88.238"

result = os.popen(f'adb connect {device}').read()
d = Device(f'{device}:5555')
print(d.dump())