Skip to content

Support connecting to a custom uiautomator2 server port - #76

Open
sspatank wants to merge 7 commits into
codeskyblue:masterfrom
sspatank:custom-port-simpler
Open

sspatank wants to merge 7 commits into
codeskyblue:masterfrom
sspatank:custom-port-simpler

Conversation

@sspatank

Copy link
Copy Markdown

Summary

  • Adds an optional custom port for the uiautomator2 server connection, threaded through U2AndroidDriver and AndroidProvider
  • Exposes it as the UIAUTODEV_U2_PORT environment variable, wired into the app, CLI, and case.py providers
  • Pins uiautomator2>=3.6.0, which is required for custom-port support
  • Documents the new env var in the README

Closes #75

Test plan

  • tests/test_envutils.py covers the new get_int() helper
  • tests/test_u2_driver.py covers U2AndroidDriver forwarding the port to connect_usb
  • tests/test_provider.py covers AndroidProvider threading the port through, and warning/ignoring it for non-U2 driver classes

🤖 Generated with Claude Code

sspatank added 6 commits July 15, 2026 16:34
Introduces get_int() to parse integer environment variables, warning
and falling back to None on invalid values instead of raising. This
lays the groundwork for configuring a custom uiautomator2 server port.
U2AndroidDriver now accepts an optional port and forwards it to
u2.connect_usb, letting connect_usb validate and raise on an invalid
port itself rather than duplicating that check here.
AndroidProvider gains a port option that it forwards to
U2AndroidDriver instances it creates. If a non-U2 driver_class is
configured alongside a port, warn and ignore the port rather than
failing, since only U2AndroidDriver knows what to do with it.
Exposes the custom uiautomator2 port support added in AndroidProvider
and U2AndroidDriver as a user-facing environment variable, threading
it through every place uiautodev builds an AndroidProvider, and
documents it in the README alongside the existing adb driver toggle.
Custom uiautomator2 ports require uiautomator2>=3.6.0. Bumping the
pyproject.toml constraint lets uiautomator2's own dependency
resolution enforce the minimum version, so uiautodev doesn't need to
parse or compare version strings at runtime.
Copilot AI review requested due to automatic review settings July 15, 2026 20:38
@codecov

codecov Bot commented Jul 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.30435% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 50.34%. Comparing base (3da93ec) to head (6c8b176).

Files with missing lines Patch % Lines
uiautodev/case.py 0.00% 2 Missing ⚠️
uiautodev/cli.py 0.00% 2 Missing ⚠️
uiautodev/driver/android/u2_driver.py 83.33% 1 Missing ⚠️
uiautodev/provider.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #76      +/-   ##
==========================================
+ Coverage   49.08%   50.34%   +1.26%     
==========================================
  Files          36       39       +3     
  Lines        2522     2582      +60     
==========================================
+ Hits         1238     1300      +62     
+ Misses       1284     1282       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for connecting to a uiautomator2 server running on a non-default port by threading an optional port through the Android driver/provider stack and exposing it via UIAUTODEV_U2_PORT.

Changes:

  • Introduces get_int() env parsing and adds Environment.UIAUTODEV_U2_PORT.
  • Threads optional port through AndroidProviderU2AndroidDriveru2.connect_usb(...), and wires it into app/CLI/case entrypoints.
  • Updates dependency constraint (uiautomator2>=3.6.0) and documents the new env var; adds unit tests for env parsing and port forwarding.

Reviewed changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
uiautodev/utils/envutils.py Adds get_int() helper and UIAUTODEV_U2_PORT environment binding.
uiautodev/provider.py Adds optional port to AndroidProvider and forwards/warns based on driver class.
uiautodev/driver/android/u2_driver.py Allows U2AndroidDriver to pass a custom port to u2.connect_usb.
uiautodev/cli.py Wires Environment.UIAUTODEV_U2_PORT into the Android CLI provider.
uiautodev/case.py Wires Environment.UIAUTODEV_U2_PORT into the case runner provider.
uiautodev/app.py Wires Environment.UIAUTODEV_U2_PORT into the FastAPI app’s Android provider.
tests/test_envutils.py Adds unit tests for get_int().
tests/test_u2_driver.py Adds unit tests verifying U2AndroidDriver forwards/omits port.
tests/test_provider.py Adds unit test verifying warning when port is ignored for non-U2 driver class.
README.md Documents UIAUTODEV_U2_PORT.
pyproject.toml Bumps uiautomator2 minimum version to support custom-port connections.
uiautodev/remote/scrcpy3.py Import reordering within stream_to_websocket.
uiautodev/remote/pipe.py Formatting-only change (blank line).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +12 to +25
def get_int(name: str) -> Optional[int]:
value = os.getenv(name)
if not value:
return None
try:
return int(value)
except ValueError:
logger.warning("Ignoring environment variable %s=%r: not a valid integer", name, value)
return None


class Environment:
UIAUTODEV_MOCK = is_enabled("UIAUTODEV_MOCK")
UIAUTODEV_U2_PORT = get_int("UIAUTODEV_U2_PORT")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the decision to not validate this here since it does get validated in uiautomator2. I can also validate it here.

Comment thread uiautodev/provider.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@sspatank

Copy link
Copy Markdown
Author

@codeskyblue This is ready for your review. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom Port support

2 participants