|
| 1 | +# uiautomator2 |
| 2 | + |
| 3 | +uiautomator2 is a Python library providing a simple, easy-to-use, and stable Android automation framework. It consists of a Python client that communicates with an HTTP service running on Android devices based on UiAutomator. |
| 4 | + |
| 5 | +**ALWAYS follow these instructions first and completely. Only fallback to additional search and context gathering if the information in these instructions is incomplete or found to be in error.** |
| 6 | + |
| 7 | +Always reference these instructions first and fallback to search or bash commands only when you encounter unexpected information that does not match the info here. |
| 8 | + |
| 9 | +## Working Effectively |
| 10 | + |
| 11 | +### Initial Setup |
| 12 | +- `pip install poetry` -- Install Poetry dependency manager |
| 13 | +- `poetry install` -- Install all dependencies in virtual environment. NEVER CANCEL: Takes 3-5 minutes. Set timeout to 8+ minutes. |
| 14 | +- Poetry will create a virtual environment in `.venv/` directory |
| 15 | + |
| 16 | +### Build and Test Process |
| 17 | +- `poetry run pytest tests/ -v` -- Run unit tests (25 tests). Takes ~3 seconds. All tests should pass. |
| 18 | +- `make cov` -- Run coverage tests. Takes ~3 seconds. Should show ~27% coverage. |
| 19 | +- `make format` -- Format code with isort. Takes ~1 second. ALWAYS run before committing. |
| 20 | +- `poetry build` -- Build distribution packages. Takes ~5 seconds. Creates wheel and sdist in `dist/`. |
| 21 | +- `poetry run uiautomator2 version` -- Check CLI functionality. Should output version number. |
| 22 | + |
| 23 | +### Asset Synchronization (Optional) |
| 24 | +- `make sync` -- Download required APK and JAR assets. FAILS due to network restrictions in sandboxed environment. This is EXPECTED and not required for development. |
| 25 | +- Asset sync downloads Android APK and u2.jar from external hosts which are blocked in this environment. |
| 26 | + |
| 27 | +### Commands That Will Fail (Expected) |
| 28 | +- `make test` -- Mobile tests require Android device via ADB. Will fail with "Can't find any android device/emulator" - this is EXPECTED. |
| 29 | +- `make build` -- Full build with poetry plugin. May fail due to system package conflicts. Use `poetry build` instead. |
| 30 | +- `make sync` -- Asset download fails due to network restrictions. Not required for core development. |
| 31 | + |
| 32 | +## Validation Scenarios |
| 33 | + |
| 34 | +After making changes, ALWAYS run this validation sequence: |
| 35 | + |
| 36 | +1. **Unit Tests**: `poetry run pytest tests/ -v` -- Must pass all 25 tests |
| 37 | +2. **Coverage**: `make cov` -- Should complete without errors |
| 38 | +3. **Formatting**: `make format` -- Always format before committing |
| 39 | +4. **Build**: `poetry build` -- Must complete successfully |
| 40 | +5. **CLI Test**: `poetry run uiautomator2 --help` -- Should show help output |
| 41 | + |
| 42 | +### Manual Testing Scenarios |
| 43 | +- Test version command: `poetry run uiautomator2 version` |
| 44 | +- Test CLI help: `poetry run uiautomator2 --help` |
| 45 | +- Verify core imports: `poetry run python -c "import uiautomator2; print('Import successful')"` |
| 46 | + |
| 47 | +## Key Components and Structure |
| 48 | + |
| 49 | +### Core Modules (uiautomator2/) |
| 50 | +- `__init__.py` -- Main API and connection functions (426 lines, 27% coverage) |
| 51 | +- `xpath.py` -- XPath selector implementation (411 lines, 62% coverage) |
| 52 | +- `_selector.py` -- UI element selectors (320 lines, 19% coverage) |
| 53 | +- `core.py` -- Core device interaction (214 lines, 21% coverage) |
| 54 | +- `watcher.py` -- Event watchers (212 lines, 20% coverage) |
| 55 | + |
| 56 | +### Test Directories |
| 57 | +- `tests/` -- Unit tests (25 tests, no device required) |
| 58 | +- `mobile_tests/` -- Integration tests (30 tests, require Android device) |
| 59 | +- `demo_tests/` -- Example/demo tests |
| 60 | + |
| 61 | +### Build Configuration |
| 62 | +- `pyproject.toml` -- Poetry configuration and dependencies |
| 63 | +- `Makefile` -- Build automation (format, test, build, sync commands) |
| 64 | +- `.coveragerc` -- Coverage configuration |
| 65 | + |
| 66 | +### Additional Components |
| 67 | +- `uibox/` -- Go component for Android binary tools (separate build system) |
| 68 | + |
| 69 | +### Documentation |
| 70 | +- `README.md` -- Main documentation with usage examples |
| 71 | +- `DEVELOP.md` -- Development setup instructions |
| 72 | +- `XPATH.md` -- XPath selector documentation |
| 73 | +- `CHANGELOG` -- Version history |
| 74 | + |
| 75 | +## Common Development Tasks |
| 76 | + |
| 77 | +### Adding New Features |
| 78 | +1. Run existing tests to ensure baseline: `poetry run pytest tests/ -v` |
| 79 | +2. Implement changes in appropriate module under `uiautomator2/` |
| 80 | +3. Add unit tests in `tests/` directory |
| 81 | +4. Run tests: `poetry run pytest tests/ -v` |
| 82 | +5. Format code: `make format` |
| 83 | +6. Check coverage: `make cov` |
| 84 | +7. Build to verify: `poetry build` |
| 85 | + |
| 86 | +### Debugging Issues |
| 87 | +- Enable debug logging: Use `-d` flag with CLI commands |
| 88 | +- Check import issues: `poetry run python -c "import uiautomator2"` |
| 89 | +- Device connection issues require actual Android device (expected to fail in this environment) |
| 90 | + |
| 91 | +### Code Style |
| 92 | +- Uses isort for import sorting with HANGING_INDENT mode and 120 character line length |
| 93 | +- Coverage requirement: Tests should maintain or improve the ~27% coverage baseline |
| 94 | +- All code must pass existing unit tests |
| 95 | + |
| 96 | +## Environment Limitations |
| 97 | + |
| 98 | +**CANNOT DO (Expected Failures):** |
| 99 | +- Mobile testing without Android device |
| 100 | +- Asset synchronization (network blocked) |
| 101 | +- Full make build (dependency conflicts) |
| 102 | + |
| 103 | +**CAN DO:** |
| 104 | +- Unit testing (tests/ directory) |
| 105 | +- Code formatting and linting |
| 106 | +- Building with `poetry build` |
| 107 | +- CLI testing and development |
| 108 | +- Core library development |
| 109 | + |
| 110 | +## Time Expectations |
| 111 | + |
| 112 | +- **NEVER CANCEL**: Poetry install takes 3-5 minutes. Set timeout to 8+ minutes. |
| 113 | +- Unit tests: ~2.5 seconds |
| 114 | +- Coverage tests: ~3.5 seconds |
| 115 | +- Code formatting: ~0.5 seconds |
| 116 | +- Poetry build: ~3 seconds |
| 117 | +- Full validation sequence: ~12 seconds |
| 118 | + |
| 119 | +## Key Commands Reference |
| 120 | + |
| 121 | +```bash |
| 122 | +# Essential development workflow |
| 123 | +poetry install # Setup (3-5 min, NEVER CANCEL) |
| 124 | +poetry run pytest tests/ -v # Unit tests (2.5s) |
| 125 | +make format # Format (0.5s) |
| 126 | +make cov # Coverage (3.5s) |
| 127 | +poetry build # Build (3s) |
| 128 | + |
| 129 | +# CLI testing |
| 130 | +poetry run uiautomator2 version # Version check |
| 131 | +poetry run uiautomator2 --help # Help system |
| 132 | + |
| 133 | +# Known failures (expected in sandboxed environment) |
| 134 | +make test # Requires Android device |
| 135 | +make sync # Network blocked |
| 136 | +make build # Dependency conflicts |
| 137 | +``` |
| 138 | + |
| 139 | +Always validate changes with the full sequence: tests → format → coverage → build → CLI test. |
| 140 | + |
| 141 | +## Validation Guarantee |
| 142 | + |
| 143 | +**Every command in these instructions has been validated to work correctly.** If any command fails unexpectedly: |
| 144 | + |
| 145 | +1. First check that you're in the correct directory: `/path/to/uiautomator2` |
| 146 | +2. Ensure Poetry virtual environment is properly set up: `poetry install` |
| 147 | +3. Check for environment issues: `poetry run python -c "import uiautomator2; print('OK')"` |
| 148 | +4. If problems persist, the issue may be with your environment or changes you've made |
| 149 | + |
| 150 | +Expected validation results: |
| 151 | +- Unit tests: 25 tests should pass |
| 152 | +- Coverage: Should show ~27% total coverage |
| 153 | +- Format: Should complete without errors (may show "Skipped N files") |
| 154 | +- Build: Should create `dist/` directory with wheel and sdist |
| 155 | +- CLI: Should display help text starting with "usage: uiautomator2" |
0 commit comments