dtmilano / AndroidViewClient

Android ViewServer and ADB client
Apache License 2.0
1.62k stars 345 forks source link

Take SnapShot with TimeOutException #309

Closed answerhua96 closed 2 years ago

answerhua96 commented 2 years ago

Hey first that all, thanks for the amazing tool.

I'm trying to take snapshot for some of my app with method device.takeSnapShot(reconnect = true) but it some times show me some error likes

File "/usr/local/lib/python3.6/site-packages/com/dtmilano/android/adb/adbclient.py", line 820, in takeSnapshot
    received = self.__receive(1 * 4 + 12 * 4)
  File "/usr/local/lib/python3.6/site-packages/com/dtmilano/android/adb/adbclient.py", line 339, in __receive
    raise Timer.TimeoutException('%d EXPIRED' % timerId)
com.dtmilano.android.adb.adbclient.Timer.TimeoutException: 413 EXPIRED

What should I do to avoid this error? Thanks very much

dtmilano commented 2 years ago

What's the device you are using? What's the screen resolution?

answerhua96 commented 2 years ago

I am using emulator to run my code. If this error occurs, there will be no screen resolution. I think it might be something error with adb connect.

answerhua96 commented 2 years ago

The timing of this error is rather random

dtmilano commented 2 years ago

Can you show a minimal reproducible example? Are you running takeSnapshot() in a loop? Multithreding? etc.

Also, CulebraTester2-public as a backend tends to be more stable than adb in the long term and will be able to capture screenshots faster.

answerhua96 commented 2 years ago

I am running takeSnapshot() in a loop and running with multithreading, but one thread corresponds to one emulator. The code just like

if __name__ == '__main__':
    thread_pool = ThreadPoolExecutor(max_workers=4)
    for i in range(0, 4):
        try:
            r = thread_pool.submit(sub_thread, host_list[i], int(port_list[i]))
        except:
            pass

def sub_thread(host, port):
    while True:
        device, serialno = ViewClient.connectToDeviceOrExit(serialno=host + ':' + str(port),timeout=120)
        ##do_some_thing
        img = device.takeSnapshot(reconnect=True)
        img.save(path)
        ##do_some_thing
answerhua96 commented 2 years ago

Should I use CulebraTester2-public instead of takeSnapshot()

dtmilano commented 2 years ago

Definitely, something you can try. Notice that ViewClient.writeImagetoFile() uses self.uiAutomatorHelper.ui_device.take_screenshot() if useuiautomatorhelper=True is specified when you create a ViewClient object.

See https://github.com/dtmilano/AndroidViewClient/blob/master/src/com/dtmilano/android/viewclient.py#L4305

This will also help you: https://stackoverflow.com/questions/72300306/how-to-start-culebratester2-server-on-more-than-one-device

answerhua96 commented 2 years ago

I will try to use ViewClient.writeImagetoFile() instead of

img = device.takeSnapshot(reconnect=True)
img.save(path)

Thanks very much

dtmilano commented 2 years ago

Don't forget you need useuiautomatorhelper=True when creating ViewClient, otherwise it will use the same device.takeSnapshot(reconnect=True), and the service up and running as described here.

answerhua96 commented 2 years ago

When I use ViewClient(device=self.device, serialno=self.SERIALNO) to create a ViewClient, it shows me dump() Error.

ValueError: received does not contain valid XML: ERROR: null root node returned by UiTestAutomationBridge

dtmilano commented 2 years ago
ViewClient(device=self.device, serialno=self.SERIALNO, useuiautomatorhelper=True)

check (and possibly run as a test too) the examples in https://github.com/dtmilano/AndroidViewClient/tree/master/examples/helper

answerhua96 commented 2 years ago

Thanks a lot. I will try this method.

answerhua96 commented 2 years ago

Thanks a lot, the new method works. By the way I have another question.I have a plenty of App to takeSnapShot.Some of these apps should be displayed in landscape(such as some games), others should be displayed vertically.Can ViewClient judge whether an APP should be displayed on a landscape screen or a vertical screen? Now, all of my app are displayed vertically.

dtmilano commented 2 years ago

CulebraTester2-public API supports a bunch of orientation and rotation methods like

answerhua96 commented 2 years ago

Thanks very much, I will close this issue