Run monkey test. Args: panel: Target panel name. duration: Max duration in seconds. count: Max number of actions. seed: Random seed for reproducibility. type_filter: Filter elements by type. class_filter: Filter element
(
self,
panel: str,
duration: float | None = None,
count: int | None = None,
seed: int | None = None,
type_filter: str | None = None,
class_filter: str | None = None,
stop_on_error: bool = False,
interval: float = 0.2,
error_check_interval: int = 5,
)
| 38 | self._console = console |
| 39 | |
| 40 | def run( |
| 41 | self, |
| 42 | panel: str, |
| 43 | duration: float | None = None, |
| 44 | count: int | None = None, |
| 45 | seed: int | None = None, |
| 46 | type_filter: str | None = None, |
| 47 | class_filter: str | None = None, |
| 48 | stop_on_error: bool = False, |
| 49 | interval: float = 0.2, |
| 50 | error_check_interval: int = 5, |
| 51 | ) -> MonkeyResult: |
| 52 | """Run monkey test. |
| 53 | |
| 54 | Args: |
| 55 | panel: Target panel name. |
| 56 | duration: Max duration in seconds. |
| 57 | count: Max number of actions. |
| 58 | seed: Random seed for reproducibility. |
| 59 | type_filter: Filter elements by type. |
| 60 | class_filter: Filter elements by USS class. |
| 61 | stop_on_error: Stop on first console error. |
| 62 | interval: Delay between actions in seconds. |
| 63 | error_check_interval: Check console errors every N actions. |
| 64 | |
| 65 | Returns: |
| 66 | MonkeyResult with actions performed and errors found. |
| 67 | """ |
| 68 | if error_check_interval < 1: |
| 69 | msg = "error_check_interval must be >= 1" |
| 70 | raise ValueError(msg) |
| 71 | if duration is None and count is None: |
| 72 | count = 100 |
| 73 | |
| 74 | actual_seed = seed if seed is not None else random.randint(0, 2**31) |
| 75 | rng = random.Random(actual_seed) |
| 76 | |
| 77 | self._console.clear() |
| 78 | start = time.time() |
| 79 | result = MonkeyResult(seed=actual_seed) |
| 80 | |
| 81 | action_count = 0 |
| 82 | while not _should_stop(action_count, count, start, duration): |
| 83 | try: |
| 84 | elements = self._query_elements(panel, type_filter, class_filter) |
| 85 | except Exception as e: |
| 86 | result.errors.append({"source": "query", "message": str(e)}) |
| 87 | break |
| 88 | if not elements: |
| 89 | break |
| 90 | |
| 91 | action_count += 1 |
| 92 | self._perform_action(rng, elements, result) |
| 93 | |
| 94 | if action_count % error_check_interval == 0 and self._handle_errors(result, stop_on_error): |
| 95 | break |
| 96 | |
| 97 | time.sleep(interval) |