axterminator 0.1.0

World's most superior macOS GUI testing framework with background testing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
"""
Tests for AXTerminator element operations.

Tests cover:
- Finding elements by title
- Finding elements by role
- Finding elements by identifier
- Timeout and waiting behavior
- Element not found errors
- Element properties (role, title, value, etc.)
"""

from __future__ import annotations

import time
from typing import TYPE_CHECKING, Callable

import pytest

if TYPE_CHECKING:
    from conftest import MockAXElement, PerformanceResult, TestApp


class TestFindByTitle:
    """Tests for finding elements by title."""

    @pytest.mark.requires_app
    def test_find_by_title_simple(self, calculator_app: TestApp) -> None:
        """Find element by simple title string."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # Calculator has buttons with number titles
        element = app.find("1")

        assert element is not None

    @pytest.mark.requires_app
    def test_find_by_title_with_spaces(self, calculator_app: TestApp) -> None:
        """Find element with spaces in title."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # Try finding AC (All Clear) button
        element = app.find("AC")

        assert element is not None

    def test_find_by_title_mock(self, mock_calculator_tree: MockAXElement) -> None:
        """Test title search with mock tree."""
        # Search logic test with mock
        tree = mock_calculator_tree

        # Find button with title "5"
        def find_by_title(node: MockAXElement, title: str) -> MockAXElement | None:
            if node.title == title:
                return node
            for child in node.get_children():
                result = find_by_title(child, title)
                if result:
                    return result
            return None

        button = find_by_title(tree, "5")

        assert button is not None
        assert button.role == "AXButton"
        assert button.title == "5"

    @pytest.mark.requires_app
    def test_find_by_title_returns_first_match(self, finder_app: TestApp) -> None:
        """When multiple elements have same title, first is returned."""
        import axterminator as ax

        app = ax.app(name="Finder")

        # This depends on Finder's actual UI structure
        # Just verify we get a single element back
        try:
            element = app.find("File")
            assert element is not None
        except RuntimeError:
            # Element not found is acceptable for this test
            pass

    def test_find_by_title_case_sensitive(
        self, mock_calculator_tree: MockAXElement
    ) -> None:
        """Title search should be case-sensitive."""

        def find_by_title(node: MockAXElement, title: str) -> MockAXElement | None:
            if node.title == title:
                return node
            for child in node.get_children():
                result = find_by_title(child, title)
                if result:
                    return result
            return None

        # "AC" exists, "ac" should not find it (case sensitive)
        ac_button = find_by_title(mock_calculator_tree, "AC")
        lowercase = find_by_title(mock_calculator_tree, "ac")

        assert ac_button is not None
        assert lowercase is None


class TestFindByRole:
    """Tests for finding elements by accessibility role."""

    @pytest.mark.requires_app
    def test_find_by_role_button(self, calculator_app: TestApp) -> None:
        """Find elements with AXButton role."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # Find any button
        element = app.find_by_role("AXButton")

        assert element is not None
        assert element.role() == "AXButton"

    @pytest.mark.requires_app
    def test_find_by_role_with_title(self, calculator_app: TestApp) -> None:
        """Find element by role AND title."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # Find specific button
        element = app.find_by_role("AXButton", title="5")

        assert element is not None
        assert element.title() == "5"

    @pytest.mark.requires_app
    def test_find_by_role_window(self, calculator_app: TestApp) -> None:
        """Find window element."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        element = app.find_by_role("AXWindow")

        assert element is not None
        assert element.role() == "AXWindow"

    @pytest.mark.requires_app
    def test_find_by_role_with_identifier(self, calculator_app: TestApp) -> None:
        """Find element by role and identifier."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # May or may not find element depending on app structure
        try:
            element = app.find_by_role("AXButton", identifier="some_id")
            assert element is not None
        except RuntimeError:
            # Not found is acceptable
            pass

    def test_find_by_role_invalid_role(self) -> None:
        """Invalid role name raises or returns nothing."""
        import axterminator as ax

        app = ax.app(name="Finder")

        with pytest.raises(RuntimeError):
            app.find_by_role("AXInvalidRole12345")

    @pytest.mark.requires_app
    def test_find_by_role_statictext(self, calculator_app: TestApp) -> None:
        """Find static text element (display)."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        try:
            element = app.find_by_role("AXStaticText")
            assert element is not None
        except RuntimeError:
            # Structure may vary
            pass


class TestFindByIdentifier:
    """Tests for finding elements by accessibility identifier."""

    def test_find_by_identifier_mock(self, mock_calculator_tree: MockAXElement) -> None:
        """Test identifier search with mock tree."""

        def find_by_id(node: MockAXElement, identifier: str) -> MockAXElement | None:
            if node.identifier == identifier:
                return node
            for child in node.get_children():
                result = find_by_id(child, identifier)
                if result:
                    return result
            return None

        button = find_by_id(mock_calculator_tree, "calc_btn_5")

        assert button is not None
        assert button.title == "5"

    @pytest.mark.requires_app
    def test_find_by_identifier_using_find(self, calculator_app: TestApp) -> None:
        """Find element using identifier: prefix in query."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        try:
            # Use query syntax: identifier:value
            element = app.find("identifier:_NS:9")  # Example identifier
            assert element is not None
        except RuntimeError:
            # Identifier may not exist
            pass

    def test_find_by_identifier_not_found(
        self, mock_calculator_tree: MockAXElement
    ) -> None:
        """Non-existent identifier returns None."""

        def find_by_id(node: MockAXElement, identifier: str) -> MockAXElement | None:
            if node.identifier == identifier:
                return node
            for child in node.get_children():
                result = find_by_id(child, identifier)
                if result:
                    return result
            return None

        result = find_by_id(mock_calculator_tree, "nonexistent_identifier")

        assert result is None


class TestFindWithTimeout:
    """Tests for element finding with timeout."""

    @pytest.mark.requires_app
    def test_find_with_timeout_found_immediately(self, calculator_app: TestApp) -> None:
        """Element found immediately returns without waiting."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        start = time.perf_counter()
        element = app.find("1", timeout_ms=5000)
        elapsed = time.perf_counter() - start

        assert element is not None
        # Should return quickly, not wait full timeout
        assert elapsed < 1.0

    @pytest.mark.requires_app
    @pytest.mark.slow
    def test_find_with_timeout_waits(self, calculator_app: TestApp) -> None:
        """When element not found, waits for timeout."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        start = time.perf_counter()
        with pytest.raises(RuntimeError, match="not found"):
            app.find("NonExistentElement12345", timeout_ms=500)
        elapsed = time.perf_counter() - start

        # Should have waited approximately the timeout
        assert 0.4 < elapsed < 1.5  # Allow some margin

    @pytest.mark.requires_app
    def test_wait_for_element_method(self, calculator_app: TestApp) -> None:
        """wait_for_element() explicitly waits for element."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        element = app.wait_for_element("1", timeout_ms=2000)

        assert element is not None

    @pytest.mark.requires_app
    @pytest.mark.slow
    def test_wait_for_element_timeout_raises(self, calculator_app: TestApp) -> None:
        """wait_for_element() raises after timeout."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        with pytest.raises(RuntimeError):
            app.wait_for_element("NonExistent12345", timeout_ms=500)

    @pytest.mark.requires_app
    def test_timeout_zero_no_wait(self, calculator_app: TestApp) -> None:
        """Timeout of 0 means no waiting."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        start = time.perf_counter()
        with pytest.raises(RuntimeError):
            app.find("NonExistent", timeout_ms=0)
        elapsed = time.perf_counter() - start

        # Should return immediately
        assert elapsed < 0.5


class TestElementNotFound:
    """Tests for element not found error handling."""

    @pytest.mark.requires_app
    def test_element_not_found_error_message(self, calculator_app: TestApp) -> None:
        """Error message includes the query."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        query = "UniqueNonExistentElement12345"

        with pytest.raises(RuntimeError) as exc_info:
            app.find(query, timeout_ms=100)

        error_msg = str(exc_info.value)
        assert "not found" in error_msg.lower()

    @pytest.mark.requires_app
    def test_find_by_role_not_found(self, calculator_app: TestApp) -> None:
        """find_by_role raises when no match."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        with pytest.raises(RuntimeError):
            app.find_by_role("AXButton", title="NonExistentTitle12345")

    def test_element_not_found_after_healing(self) -> None:
        """Error type changes after healing attempts fail."""
        # This tests the ElementNotFoundAfterHealing error type
        # Mock implementation would be needed
        pass


class TestElementProperties:
    """Tests for element property accessors."""

    @pytest.mark.requires_app
    def test_element_role(self, calculator_app: TestApp) -> None:
        """Element has role() method returning role string."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        role = element.role()

        assert role is not None
        assert isinstance(role, str)
        assert role == "AXButton"

    @pytest.mark.requires_app
    def test_element_title(self, calculator_app: TestApp) -> None:
        """Element has title() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("5")

        title = element.title()

        assert title == "5"

    @pytest.mark.requires_app
    def test_element_value(self, calculator_app: TestApp) -> None:
        """Element has value() method for elements with values."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        # Try to find display element which has a value
        try:
            element = app.find_by_role("AXStaticText")
            value = element.value()
            assert value is not None or value == ""
        except RuntimeError:
            # May not find this element
            pass

    @pytest.mark.requires_app
    def test_element_description(self, calculator_app: TestApp) -> None:
        """Element has description() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        # description may be None
        desc = element.description()

        assert desc is None or isinstance(desc, str)

    @pytest.mark.requires_app
    def test_element_label(self, calculator_app: TestApp) -> None:
        """Element has label() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        label = element.label()

        assert label is None or isinstance(label, str)

    @pytest.mark.requires_app
    def test_element_identifier(self, calculator_app: TestApp) -> None:
        """Element has identifier() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        identifier = element.identifier()

        assert identifier is None or isinstance(identifier, str)

    @pytest.mark.requires_app
    def test_element_enabled(self, calculator_app: TestApp) -> None:
        """Element has enabled() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        enabled = element.enabled()

        assert isinstance(enabled, bool)
        assert enabled is True  # Calculator buttons should be enabled

    @pytest.mark.requires_app
    def test_element_focused(self, calculator_app: TestApp) -> None:
        """Element has focused() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        focused = element.focused()

        assert isinstance(focused, bool)

    @pytest.mark.requires_app
    def test_element_exists(self, calculator_app: TestApp) -> None:
        """Element has exists() method."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        exists = element.exists()

        assert exists is True

    @pytest.mark.requires_app
    def test_element_bounds(self, calculator_app: TestApp) -> None:
        """Element has bounds() method returning position/size."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("1")

        bounds = element.bounds()

        # bounds may be None if not implemented
        if bounds is not None:
            assert len(bounds) == 4
            x, y, width, height = bounds
            assert isinstance(x, (int, float))
            assert isinstance(y, (int, float))
            assert isinstance(width, (int, float))
            assert isinstance(height, (int, float))


class TestElementPropertyEdgeCases:
    """Edge cases for element properties."""

    def test_properties_on_mock_element(
        self, mock_ax_element: Callable[..., MockAXElement]
    ) -> None:
        """All properties work on mock element."""
        element = mock_ax_element(
            role="AXButton",
            title="Test",
            value="Value",
            identifier="test_id",
            description="A test button",
            label="Test Label",
            enabled=True,
            focused=False,
            bounds=(100, 100, 50, 30),
        )

        assert element.role == "AXButton"
        assert element.title == "Test"
        assert element.value == "Value"
        assert element.identifier == "test_id"
        assert element.description == "A test button"
        assert element.label == "Test Label"
        assert element.enabled is True
        assert element.focused is False
        assert element.bounds == (100, 100, 50, 30)

    def test_properties_with_none_values(
        self, mock_ax_element: Callable[..., MockAXElement]
    ) -> None:
        """Properties handle None values gracefully."""
        element = mock_ax_element(
            role="AXButton",
            title=None,
            value=None,
            identifier=None,
        )

        assert element.role == "AXButton"
        assert element.title is None
        assert element.value is None
        assert element.identifier is None

    def test_properties_with_empty_strings(
        self, mock_ax_element: Callable[..., MockAXElement]
    ) -> None:
        """Properties handle empty strings."""
        element = mock_ax_element(
            role="AXButton",
            title="",
            value="",
        )

        assert element.title == ""
        assert element.value == ""

    def test_properties_with_unicode(
        self, mock_ax_element: Callable[..., MockAXElement]
    ) -> None:
        """Properties handle unicode characters."""
        element = mock_ax_element(
            role="AXButton",
            title="Save Changes",
            description="Saves the current document",
        )

        assert element.title == "Save Changes"
        assert "Saves" in element.description


class TestQuerySyntax:
    """Tests for various query syntax formats."""

    @pytest.mark.requires_app
    def test_simple_title_query(self, calculator_app: TestApp) -> None:
        """Simple string is treated as title search."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        element = app.find("7")

        assert element is not None

    @pytest.mark.requires_app
    def test_role_prefix_query(self, calculator_app: TestApp) -> None:
        """role: prefix searches by role."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        try:
            element = app.find("role:AXButton")
            assert element is not None
        except RuntimeError:
            # Query syntax may not be implemented
            pass

    @pytest.mark.requires_app
    def test_combined_query(self, calculator_app: TestApp) -> None:
        """Combined role and title query."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        try:
            element = app.find("role:AXButton title:8")
            assert element is not None
        except RuntimeError:
            # Query syntax may not be implemented
            pass


class TestElementPerformance:
    """Performance tests for element operations."""

    @pytest.mark.slow
    @pytest.mark.requires_app
    def test_find_performance(
        self,
        calculator_app: TestApp,
        perf_timer: Callable[..., PerformanceResult],
    ) -> None:
        """Element find should be fast."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        result = perf_timer(
            lambda: app.find("5", timeout_ms=100),
            iterations=100,
            name="find_element",
        )

        # Target: <10ms p95 for element access
        # Claimed: 242us, allowing 10ms for real-world
        assert result.p95_ms < 10, f"Find too slow: {result.p95_ms}ms"

    @pytest.mark.slow
    @pytest.mark.requires_app
    def test_property_access_performance(
        self,
        calculator_app: TestApp,
        perf_timer: Callable[..., PerformanceResult],
    ) -> None:
        """Property access should be very fast."""
        import axterminator as ax

        app = ax.app(name="Calculator")
        element = app.find("5")

        result = perf_timer(
            lambda: element.role(),
            iterations=1000,
            name="property_access",
        )

        # Property access should be sub-millisecond
        assert result.p95_ms < 1, f"Property access too slow: {result.p95_ms}ms"

    @pytest.mark.slow
    @pytest.mark.requires_app
    def test_multiple_finds_performance(
        self,
        calculator_app: TestApp,
        perf_timer: Callable[..., PerformanceResult],
    ) -> None:
        """Multiple consecutive finds remain fast."""
        import axterminator as ax

        app = ax.app(name="Calculator")

        def find_multiple() -> None:
            for i in range(10):
                app.find(str(i % 10), timeout_ms=100)

        result = perf_timer(
            find_multiple,
            iterations=10,
            name="find_multiple",
        )

        # 10 finds should complete in <100ms total
        assert result.avg_ms < 100, f"Multiple finds too slow: {result.avg_ms}ms"