Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get the elements in uiautomator tool when the app is running on the device

I am having a setup where web Application in my PC is accessing the app info running on the connected device.(through USB debugging). and continuously sends the app data to the Web Application(PC).

I am automating this using selenium(web GUI) and appium(device) for my automation testing..

Issue: I am unable to connect to the device from uiautomator.bat tool once the app is getting launched in the device and communicating with the Web app(In my PC). Getting the below error. Is there a workaround for this issue.

--------uiautomator.bat log-----------

C:\Users\sat_yug\android-sdks\tools>uiautomatorviewer.bat 03:57:35 E/DeviceMonitor: Adb connection Error:An existing connection was forcibly closed by the remote host 03:57:36 E/DeviceMonitor: Connection attempts: 1 03:57:38 E/DeviceMonitor: Connection attempts: 2 03:57:40 E/DeviceMonitor: Connection attempts: 3 03:57:42 E/DeviceMonitor: Connection attempts: 4 03:57:44 E/DeviceMonitor: Connection attempts: 5 03:58:04 E/DeviceMonitor: Adb connection Error:An existing connection was forcibly closed by the remote host

------------adb devices log---------------------

C:\Users\sat_yug\android-sdks\platform-tools>adb devices List of devices attached adb server is out of date. killing... error: could not install smartsocket listener: cannot bind to 127.0.0.1:5037: Only one usage of each socket address (protocol/n etwork address/port) is normally permitted. (10048) could not read ok from ADB Server * failed to start daemon * error: cannot connect to daemon


like image 245
sat_yug Avatar asked Dec 18 '15 10:12

sat_yug


People also ask

How do you inspect element with UIAutomator?

Q #12) How do you inspect an element in UIAutomator? Answer: Once you are done with the setup, open a command prompt, and enter the command UIAutomatorViewer. A window will be displayed on your PC. Connect the mobile to a PC and click on the Device screenshot (uiautomator dump) second icon at the top.

How do you use the UIAutomator in Appium?

If you go to android SDK>tools>UIAutomator. Make sure device is connected, tap on green button on top left side. This will take the screenshot of your current screen on the device. This way you can point mouse on the screen to detect elements.

Where is Uiautomatorviewer located in Mac?

You can first check your uiautomatorviewer. bat at Library/Android/sdk/tools/bin/ and can start the same by executing Library/Android/sdk/tools/bin/uiautomatorviewer .


2 Answers

I just solved this for me so i thought i'd share even though the question is old. Simply restarting the adb is not going to work. Open a command prompt with administrator priviledges and execute this:

netstat -o -n -a | findstr 5037

This will produce a list of results. This is what came up in my case:

 TCP    127.0.0.1:5037         0.0.0.0:0              LISTENING       3408
 TCP    127.0.0.1:5037         127.0.0.1:50018        ESTABLISHED     3408
 TCP    127.0.0.1:5037         127.0.0.1:54507        ESTABLISHED     3408
 TCP    127.0.0.1:5037         127.0.0.1:63330        ESTABLISHED     3408
 TCP    127.0.0.1:5037         127.0.0.1:63332        ESTABLISHED     3408
 TCP    127.0.0.1:50018        127.0.0.1:5037         ESTABLISHED     1664
 TCP    127.0.0.1:54507        127.0.0.1:5037         ESTABLISHED     1664
 TCP    127.0.0.1:63330        127.0.0.1:5037         ESTABLISHED     1664
 TCP    127.0.0.1:63332        127.0.0.1:5037         ESTABLISHED     1664

At the most right column is the process id (PID). The proccess that is listening to the needed socket is the 3408. So this process must DIE ! Which happends if you do:

taskkill /F /PID 3408

After that you can do

adb kill-server
adb start-server

to restart the adb server and most propably your adb will start successfully.

UPDATE:

I made this little bat file to make it easier since this happens quite often. Make sure

1. to place this bat at the same folder as adb.exe 
2. run it as administrator. 

It will directly show you the PID that is using the socket. Type that PID and hit enter and the problem goes away.

netstat -o -n -a | findstr 5037 | findstr LISTENING  
set /p pid=Enter pid to kill:%=%
@echo %pid%
taskkill /F /PID %pid%
adb kill-server
adb start-server
pause
like image 147
Anonymous Avatar answered Oct 20 '22 00:10

Anonymous


As per the answer to THIS question, there is a possibility that you could have two versions of adb installed.

Try the following (quoted from same post to check if there are multiple versions) and get rid of the unwanted one.

where adb.exe

Another option you could try is to kill and start the adb server before the point of error or at the start of execution of your batch file.

adb kill-server
adb start-server
....
....
//your script here
....
....
like image 34
AndroidMechanic - Viral Patel Avatar answered Oct 20 '22 00:10

AndroidMechanic - Viral Patel