Skip to content

🐡🧠 SmartMonkey - Intelligent Android App Testing Tool. Goes beyond random monkey testing with weighted exploration strategies, crash detection, and comprehensive reporting.

Notifications You must be signed in to change notification settings

devload/smartmonkey

Repository files navigation

SmartMonkey 🐡🧠

Intelligent Android App Testing Tool with Grafana Dashboards

Version Python License Platform

Features β€’ Installation β€’ Quick Start β€’ Grafana Setup β€’ Documentation


🎯 What is SmartMonkey?

SmartMonkey is an intelligent Android app testing tool that goes beyond traditional random monkey testing. While MonkeyRunner clicks randomly, SmartMonkey uses a weighted exploration strategy to intelligently test your Android applications with beautiful Grafana dashboards for visualization.

πŸ€– How It Works

  • πŸ“Š Weighted Strategy: Prioritizes unvisited UI elements (10x weight) to maximize code coverage
  • 🎯 Smart Targeting: Bonus scoring for buttons (1.5x) and submit actions (2x)
  • πŸ” State Detection: MD5 hashing to avoid duplicate states
  • πŸ’₯ Crash Detection: Automatically detects when app exits or moves to background
  • πŸ“Έ Visual Documentation: Screenshots at every step with Grafana gallery

✨ Features

🎯 Intelligent Exploration

  • Weighted Strategy: Unvisited elements get 10x priority
  • Context-Aware: Recognizes buttons, text fields, and interactive elements
  • State Hashing: Avoids testing duplicate UI states

πŸ’₯ Crash Detection

  • Real-time Monitoring: Detects when app stops running or moves to background
  • Empty State Detection: Identifies UI deadlocks
  • Detailed Reports: Full crash context with screenshots

πŸ“Š Grafana Dashboard Integration

  • Beautiful Visualizations: Interactive test result dashboards
  • Screenshot Gallery: Scrollable gallery of all test screenshots
  • Test History: Track multiple test runs over time
  • Drill-Down Navigation: Click test ID to view detailed results

πŸ”§ Developer-Friendly

  • Full CLI Parameters: Device, package, steps, strategy all configurable
  • Multi-device Support: Works with physical devices and emulators
  • JSON & Text Reports: Both machine and human-readable formats

πŸ“Έ Screenshots

Grafana Dashboard - Test Runs Overview

Main Dashboard View all test runs with status, duration, and crash detection

Grafana Dashboard - Test Detail with Screenshot Gallery

Detail Dashboard Interactive screenshot gallery and detailed test metrics


πŸš€ Installation

Prerequisites

  • Python 3.9 or higher
  • Android SDK with ADB installed
  • At least one Android device or emulator connected

Install SmartMonkey

# Clone the repository
git clone https://github.com/yourusername/smartmonkey.git
cd smartmonkey

# Install dependencies
pip install -r requirements.txt

# Set PYTHONPATH
export PYTHONPATH=$(pwd):$PYTHONPATH

# Verify installation
python3 -m smartmonkey.cli.main --version

πŸƒ Quick Start

1. List Connected Devices

python3 -m smartmonkey.cli.main list-devices

Output:

Available devices:
  - emulator-5556 (sdk_gphone64_arm64)
  - RFCX919P8ZF (Samsung SM-A356N)

2. Run a Basic Test

python3 -m smartmonkey.cli.main run \
  --device emulator-5556 \
  --package com.android.settings \
  --steps 20

3. Run Multiple Tests

# Run 5 tests with 20 steps each
for i in {1..5}; do
  python3 -m smartmonkey.cli.main run \
    --device emulator-5556 \
    --package io.whatap.session.sample \
    --steps 20 \
    --strategy weighted \
    --output ./reports/test_run_$(printf "%03d" $i)
  sleep 2
done

πŸ“– CLI Parameters

Full Command Syntax

python3 -m smartmonkey.cli.main run [OPTIONS]

Available Options

Parameter Short Description Default Required
--device -d Device serial number Auto-detect No*
--package -p App package name - Yes
--steps -n Maximum number of steps 50 No
--strategy -s Exploration strategy (random or weighted) weighted No
--output -o Output directory path ./reports/<timestamp> No
--screenshots - Save screenshots yes No
--no-screenshots - Disable screenshots - No

* Required if multiple devices are connected

Examples

Basic Test (Auto-detect device)

python3 -m smartmonkey.cli.main run --package com.example.app

Specify All Parameters

python3 -m smartmonkey.cli.main run \
  --device emulator-5556 \
  --package com.example.app \
  --steps 100 \
  --strategy weighted \
  --output ./my_test_results

Disable Screenshots

python3 -m smartmonkey.cli.main run \
  --package com.example.app \
  --no-screenshots

Random Strategy

python3 -m smartmonkey.cli.main run \
  --package com.example.app \
  --strategy random \
  --steps 50

πŸ“Š Grafana Dashboard Setup

Step 1: Install Grafana

# macOS
brew install grafana
brew services start grafana

# Linux (Ubuntu/Debian)
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/oss/release/grafana_10.0.0_amd64.deb
sudo dpkg -i grafana_10.0.0_amd64.deb
sudo systemctl start grafana-server

# Access Grafana
open http://localhost:3000  # Default: admin/admin

Step 2: Install Required Plugins

  1. Go to Configuration β†’ Plugins
  2. Install Infinity Data Source plugin
  3. Install HTML Graphics Panel plugin (gapit-htmlgraphics-panel)

Step 3: Configure Data Source

  1. Go to Connections β†’ Data sources β†’ Add data source
  2. Search and select Infinity
  3. Name it (e.g., "SmartMonkey Reports")
  4. Click Save & Test

Step 4: Start HTTP Server for Reports

cd /path/to/smartmonkey/reports
python3 -m http.server 8000

Keep this running in a separate terminal.

Step 5: Import Dashboards

Import Main Dashboard (Test List)

  1. Go to Dashboards β†’ Import
  2. Click Upload JSON file
  3. Select grafana/dashboard-main.json
  4. Select your Infinity data source
  5. Click Import

Import Detail Dashboard (Test Results)

  1. Go to Dashboards β†’ Import
  2. Click Upload JSON file
  3. Select grafana/dashboard-detail.json
  4. Select your Infinity data source
  5. Click Import

Step 6: View Results

  1. Open SmartMonkey - Test Runs dashboard
  2. You'll see a list of all test runs
  3. Click on any Test ID to drill down to detailed results
  4. Browse the screenshot gallery and test metrics

πŸ“‚ Project Structure

smartmonkey/
β”œβ”€β”€ smartmonkey/              # Main package
β”‚   β”œβ”€β”€ cli/                  # CLI interface
β”‚   β”‚   └── main.py          # Command-line entry point
β”‚   β”œβ”€β”€ device/               # Device communication (ADB)
β”‚   β”‚   β”œβ”€β”€ adb_manager.py   # ADB wrapper
β”‚   β”‚   β”œβ”€β”€ app_manager.py   # App lifecycle management
β”‚   β”‚   β”œβ”€β”€ device.py        # Device abstraction
β”‚   β”‚   └── event_manager.py # Touch/swipe events
β”‚   β”œβ”€β”€ exploration/          # UI exploration & strategies
β”‚   β”‚   β”œβ”€β”€ element.py       # UI element representation
β”‚   β”‚   β”œβ”€β”€ exploration_engine.py  # Main exploration logic
β”‚   β”‚   β”œβ”€β”€ state.py         # UI state management
β”‚   β”‚   β”œβ”€β”€ ui_parser.py     # UIAutomator parser
β”‚   β”‚   └── strategies/      # Exploration strategies
β”‚   β”‚       β”œβ”€β”€ base_strategy.py
β”‚   β”‚       β”œβ”€β”€ random_strategy.py
β”‚   β”‚       └── weighted_strategy.py
β”‚   β”œβ”€β”€ reporting/            # Report generation
β”‚   β”‚   └── report_generator.py  # JSON/Text reports
β”‚   └── utils/                # Utilities
β”‚       β”œβ”€β”€ logger.py        # Logging setup
β”‚       β”œβ”€β”€ helpers.py       # Helper functions
β”‚       └── exceptions.py    # Custom exceptions
β”œβ”€β”€ grafana/                  # Grafana dashboard configs
β”‚   β”œβ”€β”€ dashboard-main.json   # Test list dashboard
β”‚   └── dashboard-detail.json # Test detail dashboard
β”œβ”€β”€ docs/                     # Documentation
β”‚   β”œβ”€β”€ design/               # Design documents
β”‚   └── images/               # Screenshots
β”œβ”€β”€ reports/                  # Generated test reports (gitignored)
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ pyproject.toml            # Project metadata
β”œβ”€β”€ .gitignore                # Git ignore rules
└── README.md                 # This file

πŸ§ͺ Test Report Structure

Generated Files

After running a test, the following structure is created:

reports/
β”œβ”€β”€ index.json                     # Test runs index (for Grafana)
└── test_run_001/
    β”œβ”€β”€ report.json                # Structured test data
    β”œβ”€β”€ report.txt                 # Human-readable summary
    └── screenshots/
        β”œβ”€β”€ screenshot_0000.png    # Step 0 screenshot
        β”œβ”€β”€ screenshot_0001.png    # Step 1 screenshot
        └── ...

report.json Schema

{
  "start_time": "2025-10-24T10:33:31.743373",
  "end_time": "2025-10-24T10:35:00.084500",
  "duration_seconds": 88.341127,
  "total_events": 20,
  "unique_states": 13,
  "total_states": 20,
  "crash_detected": false,
  "crash_info": null,
  "states": [
    {
      "step": 0,
      "timestamp": 1761201226.080551,
      "datetime": "2025-10-24T10:33:31.743373",
      "activity": "io.whatap.session.sample.Screen1Activity",
      "element_count": 17,
      "state_hash": "f0d3dd1f...",
      "screenshot": "./screenshots/screenshot_0000.png",
      "screenshot_url": "http://localhost:8000/test_run_001/screenshots/screenshot_0000.png"
    }
  ],
  "actions": [
    {
      "step": 0,
      "timestamp": 1761201227.0,
      "type": "tap",
      "repr": "TapAction(element=Button, text='GO TO SCREEN 2')"
    }
  ]
}

report.txt Example

============================================================
SmartMonkey Exploration Report
============================================================

Start Time: 2025-10-24 10:33:31
End Time: 2025-10-24 10:35:00
Duration: 88.3 seconds

Total Events: 20
Unique States: 13
Total States Visited: 20

States Explored:
------------------------------------------------------------
  1. Screen1Activity (2 elements)
  2. Screen2Activity (3 elements)
  3. Screen3Activity (2 elements)
  ...

Actions Performed:
------------------------------------------------------------
  tap: 15
  back: 3
  swipe_up: 2

🎯 Exploration Strategies

Weighted Strategy (Default)

  • Prioritizes unvisited elements: 10x weight for new elements
  • Better coverage: Explores unique UI states more thoroughly
  • Smart targeting: Bonus for buttons and submit actions
  • Recommended for: Thorough testing and code coverage

Random Strategy

  • Random selection: Picks any clickable element randomly
  • Faster execution: No state tracking overhead
  • Good for: Quick smoke testing and chaos engineering
  • Use case: Finding unexpected crashes

πŸ› Troubleshooting

Issue: "No devices found"

# Check ADB connection
adb devices

# If no devices shown, restart ADB
adb kill-server
adb start-server

# Check device is authorized
adb devices
# Should show "device" not "unauthorized"

Issue: "App crashes immediately"

# Check app is installed
adb shell pm list packages | grep <package>

# Launch app manually first
adb shell am start -n <package>/.MainActivity

# Check logcat for errors
adb logcat | grep <package>

Issue: "Grafana shows 'No data'"

  1. Check HTTP server is running:

    curl http://localhost:8000/index.json
  2. Verify Data Source URL:

    • Should be http://localhost:8000/
    • Check Grafana Data Source settings
  3. Check browser console: F12 β†’ Console tab for errors

Issue: "Screenshots not loading in Grafana"

  1. Verify image URLs are accessible:

    curl -I http://localhost:8000/test_run_001/screenshots/screenshot_0000.png
  2. Check CORS settings: HTTP server should allow cross-origin requests

  3. Refresh Grafana dashboard: Click refresh button or Ctrl+R


πŸ“ Example: Real Test Results

# Test Configuration
Device: Emulator (emulator-5556)
Package: io.whatap.session.sample
Steps: 20
Strategy: weighted
Duration: 88.3 seconds

# Results
βœ… Total Events: 20
βœ… Unique States: 13
βœ… Screenshots: 20
βœ… Status: Passed

# Actions Breakdown
tap: 15 (75%)
back: 3 (15%)
swipe_up: 2 (10%)

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ—ΊοΈ Roadmap

v0.2.0 (Planned)

  • Crash/ANR detection layer enhancements
  • HTML report generation
  • DFS and BFS exploration strategies

v0.3.0 (Planned)

  • Performance monitoring (FPS, memory, CPU)
  • Configuration file support (YAML)
  • Code coverage tracking

v0.4.0+ (Future)

  • ML-based exploration strategy
  • CI/CD integration (GitHub Actions, Jenkins)
  • Cloud testing support

πŸ™ Acknowledgments

  • Android Debug Bridge (ADB) - Device communication
  • UIAutomator - UI hierarchy parsing
  • Grafana - Data visualization platform
  • Infinity Data Source - JSON data loading for Grafana
  • HTML Graphics Panel - Screenshot gallery rendering

πŸ“¬ Contact


Made with ❀️ by SmartMonkey Team

⭐ Star this repo if you find it useful!

About

🐡🧠 SmartMonkey - Intelligent Android App Testing Tool. Goes beyond random monkey testing with weighted exploration strategies, crash detection, and comprehensive reporting.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages