appium / appium-docker-android

Appium Server setup to automate android testing on real devices
https://hub.docker.com/r/appium/
Other
605 stars 230 forks source link

Appium server stops during creating session by Appium-Python-Client #170

Open didental opened 3 months ago

didental commented 3 months ago

Here is my python file

import unittest
import argparse

from pathlib import Path
from appium.options.common import AppiumOptions
from appium import webdriver

class MyTestCase(unittest.TestCase):
    def get_appium_server_url(self):
        def is_docker():
            cgroup = Path('/proc/self/cgroup')
            return Path('/.dockerenv').is_file() or cgroup.is_file() and 'docker' in cgroup.read_text()

        # return f'http://host.docker.internal:4723' if is_docker() else f'http://127.0.0.1:4723'
        return f'http://127.0.0.1:4723'

    def test_session_create(self):

        appium_server_url = self.get_appium_server_url()
        caps = {
            "platformName": "Android",
            "newCommandTimeout": 300,
            "appium:options": {
                "appPackage":  "android app package name",
                "automationName": "UiAutomator2",
                "udid": args.device_udid,
                "noReset": True,
                "clearDeviceLogsOnStart": True,
                "autoGrantPermissions": True,
            }
        }

        appium_options = AppiumOptions()
        appium_options.load_capabilities(caps)

        driver = webdriver.Remote(appium_server_url, options=appium_options)
        self.assertIsNone(driver)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--device-udid",
        required=True,
        help="Device udid or IP of a target device")
    args, unknown = parser.parse_known_args()
    print(f"Device UDID: {args.device_udid}")

    if not unknown:
        unknown = ['']

    unittest.main(argv=[unknown[0]] + unknown[1:])

I tried to execute this file with the STF connected device.

androidusr@7c63c8b12288:~$ adb devices
List of devices attached
stf.somecorp.com:7417   device
androidusr@7c63c8b12288:~$ python3 session.py --device-udid stf.somecorp.com:7417
Device UDID: stf.somecorp.com:7417
E
======================================================================
ERROR: test_session_create (__main__.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 789, in urlopen
    response = self._make_request(
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connection.py", line 464, in getresponse
    httplib_response = super().getresponse()
  File "/usr/lib/python3.8/http/client.py", line 1348, in getresponse
    response.begin()
  File "/usr/lib/python3.8/http/client.py", line 316, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.8/http/client.py", line 285, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "session.py", line 36, in test_session_create
    driver = webdriver.Remote(appium_server_url, options=appium_options)
  File "/home/androidusr/.local/lib/python3.8/site-packages/appium/webdriver/webdriver.py", line 229, in __init__
    super().__init__(
  File "/home/androidusr/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 212, in __init__
    self.start_session(capabilities)
  File "/home/androidusr/.local/lib/python3.8/site-packages/appium/webdriver/webdriver.py", line 319, in start_session
    response = self.execute(RemoteCommand.NEW_SESSION, w3c_caps)
  File "/home/androidusr/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 352, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/home/androidusr/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 302, in execute
    return self._request(command_info[0], url, body=data)
  File "/home/androidusr/.local/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 322, in _request
    response = self._conn.request(method, url, body=body, headers=headers)
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/_request_methods.py", line 144, in request
    return self.request_encode_body(
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/_request_methods.py", line 279, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/poolmanager.py", line 443, in urlopen
    response = conn.urlopen(method, u.request_uri, **kw)
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 843, in urlopen
    retries = retries.increment(
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/util/retry.py", line 474, in increment
    raise reraise(type(error), error, _stacktrace)
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/util/util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 789, in urlopen
    response = self._make_request(
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
  File "/home/androidusr/.local/lib/python3.8/site-packages/urllib3/connection.py", line 464, in getresponse
    httplib_response = super().getresponse()
  File "/usr/lib/python3.8/http/client.py", line 1348, in getresponse
    response.begin()
  File "/usr/lib/python3.8/http/client.py", line 316, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.8/http/client.py", line 285, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

----------------------------------------------------------------------
Ran 1 test in 0.785s

FAILED (errors=1)

And the appium log at the same time is here

androidusr@7c63c8b12288:~$ appium
[Appium] Welcome to Appium v2.11.2
[Appium] The autodetected Appium home path: /home/androidusr/.appium
[Appium] Attempting to load driver espresso...
[Appium] Attempting to load driver flutter...
[Appium] Attempting to load driver gecko...
[Appium] Requiring driver at /home/androidusr/.appium/node_modules/appium-espresso-driver/build/index.js
[Appium] Requiring driver at /home/androidusr/.appium/node_modules/appium-flutter-driver/build/lib/driver.js
[Appium] Requiring driver at /home/androidusr/.appium/node_modules/appium-geckodriver/build/index.js
[Appium] EspressoDriver has been successfully loaded in 10.910s
[Appium] FlutterDriver has been successfully loaded in 10.915s
[Appium] GeckoDriver has been successfully loaded in 10.916s
[Appium] Attempting to load driver uiautomator2...
[Appium] Requiring driver at /home/androidusr/.appium/node_modules/appium-uiautomator2-driver/build/index.js
[Appium] AndroidUiautomator2Driver has been successfully loaded in 2.500s
(node:1511) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [process]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
        http://127.0.0.1:4723/ (only accessible from the same host)
        http://172.17.0.3:4723/
[Appium] Available drivers:
[Appium]   - espresso@3.3.1 (automationName 'Espresso')
[Appium]   - flutter@2.9.2 (automationName 'Flutter')
[Appium]   - gecko@1.3.9 (automationName 'Gecko')
[Appium]   - uiautomator2@3.7.6 (automationName 'UiAutomator2')
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
[HTTP] --> GET /status {}
[AppiumDriver@78e6] Calling AppiumDriver.getStatus() with args: []
[AppiumDriver@78e6] Responding to client with driver.getStatus() result: {"ready":true,"message":"The server is ready to accept new connections","build":{"version":"2.11.2"}}
[HTTP] <-- GET /status 200 46 ms - 111 
[HTTP] Request idempotency key: 0e539dd4-c7ea-4bcf-96f5-67387fba56b4
[HTTP] --> POST /session {"capabilities":{"firstMatch":[{}],"alwaysMatch":{"platformName":"Android","appium:newCommandTimeout":300,"appium:options":{"appPackage":"android app package name","automationName":"UiAutomator2","udid":"stf.somecorp.com:7417","noReset":true,"clearDeviceLogsOnStart":true,"autoGrantPermissions":true}}}}
[AppiumDriver@78e6] Calling AppiumDriver.createSession() with args: [null,null,{"firstMatch":[{}],"alwaysMatch":{"platformName":"Android","appium:newCommandTimeout":300,"appium:options":{"appPackage":"android app package name","automationName":"UiAutomator2","udid":"stf.somecorp.com:7417","noReset":true,"clearDeviceLogsOnStart":true,"autoGrantPermissions":true}}}]
[AppiumDriver@78e6] Event 'newSessionRequested' logged at 1723785424366 (05:17:04 GMT+0000 (Coordinated Universal Time))
[BaseDriver] Found appium:options capability present; will promote items inside to caps
[Appium] Attempting to find matching driver for automationName 'UiAutomator2' and platformName 'Android'
[Appium] The 'uiautomator2' driver was installed and matched caps.
[Appium] Will require it at /home/androidusr/.appium/node_modules/appium-uiautomator2-driver
[Appium] Requiring driver at /home/androidusr/.appium/node_modules/appium-uiautomator2-driver/build/index.js
[AppiumDriver@78e6] Appium v2.11.2 creating new AndroidUiautomator2Driver (v3.7.6) session
[AppiumDriver@78e6] Checking BaseDriver versions for Appium and AndroidUiautomator2Driver
[AppiumDriver@78e6] Appium's BaseDriver version is 9.11.2
[AppiumDriver@78e6] AndroidUiautomator2Driver's BaseDriver version is 9.11.2
-[AndroidUiautomator2Driver@b1c7] undefined
-[AndroidUiautomator2Driver@b1c7] Creating session with W3C capabilities: {
  "alwaysMatch": {
    "platformName": "Android",
    "appium:newCommandTimeout": 300,
    "appium:appPackage": "android app package name",
    "appium:automationName": "UiAutomator2",
    "appium:udid": "stf.somecorp.com:7417",
    "appium:noReset": true,
    "appium:clearDeviceLogsOnStart": true,
    "appium:autoGrantPermissions": true
  },
  "firstMatch": [
    {}
  ]
}
[77a82ddb]-[AndroidUiautomator2Driver@b1c7] Session created with session id: 77a82ddb-e64d-4117-829e-70b6d7cecec8
[77a82ddb][ADB] Found 1 'build-tools' folders under '/opt/android' (newest first):
[77a82ddb][ADB]     /opt/android/build-tools/34.0.0
[77a82ddb][ADB] Using 'adb' from '/opt/android/platform-tools/adb'
[77a82ddb][ADB] Running '/opt/android/platform-tools/adb -P 5037 start-server'
Segmentation fault (core dumped)

I checked appium liveness by curl command with endpoint '/status', so just ignore about /status logs. By the way, why this appium server should be communicated with 'http://127.0.0.1:4723' instead of 'http://host.docker.internal:4723'?

didental commented 3 months ago

I've installed python3-pip package from apt-get and installed python packages: Appium-Python-Client, argparse

> sudo apt-get install python3-pip
> pip3 install argparse Appium-Python-Client
didental commented 3 months ago

This happens when you use Rancher Desktop.. It does not occur in a typical Docker environment.