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
"""
Tests for AXTerminator element actions.

Tests cover:
- Background click (CRITICAL - no focus stealing)
- Focus click
- Double click
- Right click
- Text input
- Value setting
"""

from __future__ import annotations

import time
from typing import TYPE_CHECKING, Callable

import pytest

if TYPE_CHECKING:
    from conftest import PerformanceResult, TestApp


class TestBackgroundClick:
    """
    Tests for background click - the WORLD FIRST feature.

    This is the most critical test class as background clicking
    without focus stealing is AXTerminator's key differentiator.
    """

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_no_focus_steal(
        self,
        calculator_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """
        CRITICAL: Background click must NOT steal focus.

        This is the core differentiator of AXTerminator.
        Competitors steal focus; AXTerminator doesn't.
        """
        import axterminator as ax

        # Get current focus state
        before_focus = focus_tracker()

        # Connect to Calculator (it's in background)
        app = ax.app(name="Calculator")
        element = app.find("5")

        # Perform background click (default mode)
        element.click()

        # Small delay to let any focus change propagate
        time.sleep(0.2)

        # Get focus state after click
        after_focus = focus_tracker()

        # CRITICAL ASSERTION: Focus should NOT have changed
        assert before_focus.frontmost_app == after_focus.frontmost_app, (
            f"FOCUS STOLEN! Was '{before_focus.frontmost_app}', "
            f"now '{after_focus.frontmost_app}'"
        )

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_explicit_mode(
        self,
        calculator_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """Background click with explicit BACKGROUND mode."""
        import axterminator as ax

        before_focus = focus_tracker()

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

        # Explicit background mode
        element.click(mode=ax.BACKGROUND)

        time.sleep(0.2)
        after_focus = focus_tracker()

        assert before_focus.frontmost_app == after_focus.frontmost_app

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_default_is_background(
        self, calculator_app: TestApp
    ) -> None:
        """Default click mode should be BACKGROUND."""
        import axterminator as ax

        # Verify BACKGROUND is the default
        assert ax.ActionMode.Background == ax.BACKGROUND

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

        # Default click should work without error
        element.click()

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_multiple_times(
        self,
        calculator_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """Multiple background clicks don't steal focus."""
        import axterminator as ax

        before_focus = focus_tracker()

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

        # Click multiple buttons in sequence
        for digit in ["1", "2", "3"]:
            element = app.find(digit)
            element.click()
            time.sleep(0.1)

        after_focus = focus_tracker()

        assert before_focus.frontmost_app == after_focus.frontmost_app

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_on_disabled_element(
        self, calculator_app: TestApp
    ) -> None:
        """Background click on disabled element handles gracefully."""
        import axterminator as ax

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

        # AC button is always enabled, but we test the flow
        element = app.find("AC")
        element.click()  # Should work

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_click_returns_none(self, calculator_app: TestApp) -> None:
        """Click returns None (not the element for chaining)."""
        import axterminator as ax

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

        result = element.click()

        assert result is None

    @pytest.mark.background
    @pytest.mark.requires_app
    def test_background_mode_constant(self) -> None:
        """BACKGROUND constant is available."""
        import axterminator as ax

        assert hasattr(ax, "BACKGROUND")
        assert ax.BACKGROUND == ax.ActionMode.Background


class TestFocusClick:
    """Tests for focus click mode."""

    @pytest.mark.requires_app
    def test_focus_click_brings_app_forward(
        self,
        calculator_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """Focus click brings application to foreground."""
        import axterminator as ax

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

        # Focus click should bring Calculator to front
        element.click(mode=ax.FOCUS)

        time.sleep(0.3)
        after_focus = focus_tracker()

        # Calculator should now be frontmost
        assert "Calculator" in after_focus.frontmost_app

    @pytest.mark.requires_app
    def test_focus_click_explicit(self, calculator_app: TestApp) -> None:
        """Focus click works with explicit mode."""
        import axterminator as ax

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

        # Should not raise
        element.click(mode=ax.FOCUS)

    @pytest.mark.requires_app
    def test_focus_mode_constant(self) -> None:
        """FOCUS constant is available."""
        import axterminator as ax

        assert hasattr(ax, "FOCUS")
        assert ax.FOCUS == ax.ActionMode.Focus

    @pytest.mark.requires_app
    def test_focus_click_on_text_field(self, textedit_app: TestApp) -> None:
        """Focus click on text field prepares for input."""
        import axterminator as ax

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

        try:
            # Find text area
            element = app.find_by_role("AXTextArea")
            element.click(mode=ax.FOCUS)
        except RuntimeError:
            # May not find element
            pass


class TestDoubleClick:
    """Tests for double-click action."""

    @pytest.mark.requires_app
    def test_double_click_background(
        self,
        calculator_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """Double-click in background mode doesn't steal focus."""
        import axterminator as ax

        before_focus = focus_tracker()

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

        element.double_click()

        time.sleep(0.2)
        after_focus = focus_tracker()

        assert before_focus.frontmost_app == after_focus.frontmost_app

    @pytest.mark.requires_app
    def test_double_click_focus_mode(self, calculator_app: TestApp) -> None:
        """Double-click in focus mode."""
        import axterminator as ax

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

        # Should not raise
        element.double_click(mode=ax.FOCUS)

    @pytest.mark.requires_app
    def test_double_click_timing(self, calculator_app: TestApp) -> None:
        """Double-click has appropriate timing between clicks."""
        import axterminator as ax

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

        start = time.perf_counter()
        element.double_click()
        elapsed = time.perf_counter() - start

        # Double-click should include a small delay (50ms in impl)
        # but not be too slow
        assert elapsed < 0.5


class TestRightClick:
    """Tests for right-click (context menu) action."""

    @pytest.mark.requires_app
    def test_right_click_background(
        self,
        finder_app: TestApp,
        focus_tracker: Callable,
    ) -> None:
        """Right-click in background mode doesn't steal focus."""
        import axterminator as ax

        # Use Finder which has context menus
        app = ax.app(name="Finder")

        before_focus = focus_tracker()

        try:
            window = app.main_window()
            window.right_click()
        except RuntimeError:
            # May not find window
            pass

        time.sleep(0.2)
        after_focus = focus_tracker()

        # Focus should not change
        assert before_focus.frontmost_app == after_focus.frontmost_app

    @pytest.mark.requires_app
    def test_right_click_shows_menu(self, calculator_app: TestApp) -> None:
        """Right-click triggers AXShowMenu action."""
        import axterminator as ax

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

        try:
            element = app.find("1")
            element.right_click()
        except RuntimeError:
            # Element may not support context menu
            pass

    @pytest.mark.requires_app
    def test_right_click_with_mode(self, calculator_app: TestApp) -> None:
        """Right-click accepts mode parameter."""
        import axterminator as ax

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

        # Should accept mode parameter
        try:
            element.right_click(mode=ax.BACKGROUND)
        except RuntimeError:
            # May fail if ShowMenu not supported
            pass


class TestTypeText:
    """Tests for text input action."""

    @pytest.mark.requires_app
    def test_type_text_requires_focus(self, textedit_app: TestApp) -> None:
        """Type text requires FOCUS mode."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")

            # Typing with BACKGROUND should raise error
            with pytest.raises(RuntimeError, match="FOCUS"):
                element.type_text("test", mode=ax.BACKGROUND)
        except RuntimeError:
            # May not find element
            pass

    @pytest.mark.requires_app
    def test_type_text_focus_mode(self, textedit_app: TestApp) -> None:
        """Type text works in focus mode."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")

            # Should work with FOCUS mode (or default)
            element.type_text("Hello", mode=ax.FOCUS)
        except RuntimeError:
            # May fail if not implemented yet
            pass

    @pytest.mark.requires_app
    def test_type_text_default_focus_mode(self, textedit_app: TestApp) -> None:
        """Type text defaults to FOCUS mode."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")

            # Default should be FOCUS for type_text
            element.type_text("World")
        except RuntimeError:
            # May fail if not implemented
            pass

    @pytest.mark.requires_app
    def test_type_text_special_characters(self, textedit_app: TestApp) -> None:
        """Type text handles special characters."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")

            # Test special characters
            element.type_text("Hello! @#$%")
        except RuntimeError:
            pass

    @pytest.mark.requires_app
    def test_type_text_unicode(self, textedit_app: TestApp) -> None:
        """Type text handles unicode."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")

            # Test unicode
            element.type_text("Cafe")
        except RuntimeError:
            pass

    def test_type_text_empty_string(self, textedit_app: TestApp) -> None:
        """Type text with empty string is handled."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")
            element.type_text("")
        except RuntimeError:
            pass


class TestSetValue:
    """Tests for set_value action."""

    @pytest.mark.requires_app
    def test_set_value_on_text_field(self, textedit_app: TestApp) -> None:
        """Set value on text field."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")
            element.set_value("Direct value")
        except RuntimeError:
            # May not be implemented
            pass

    @pytest.mark.requires_app
    def test_set_value_clears_existing(self, textedit_app: TestApp) -> None:
        """Set value replaces existing content."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")
            element.set_value("First")
            element.set_value("Second")

            # Value should be "Second", not "FirstSecond"
            value = element.value()
            if value:
                assert "First" not in value or value == "Second"
        except RuntimeError:
            pass

    @pytest.mark.requires_app
    def test_set_value_empty_string(self, textedit_app: TestApp) -> None:
        """Set value to empty string clears field."""
        import axterminator as ax

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

        try:
            element = app.find_by_role("AXTextArea")
            element.set_value("")
        except RuntimeError:
            pass

    def test_set_value_on_button_fails(self, calculator_app: TestApp) -> None:
        """Set value on button should fail (no value attribute)."""
        import axterminator as ax

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

        with pytest.raises(RuntimeError):
            element.set_value("cannot set")


class TestActionModes:
    """Tests for action mode constants and behavior."""

    def test_action_mode_enum_values(self) -> None:
        """ActionMode enum has expected values."""
        import axterminator as ax

        assert hasattr(ax, "ActionMode")
        assert hasattr(ax.ActionMode, "Background")
        assert hasattr(ax.ActionMode, "Focus")

    def test_background_constant(self) -> None:
        """BACKGROUND constant equals ActionMode.Background."""
        import axterminator as ax

        assert ax.BACKGROUND == ax.ActionMode.Background

    def test_focus_constant(self) -> None:
        """FOCUS constant equals ActionMode.Focus."""
        import axterminator as ax

        assert ax.FOCUS == ax.ActionMode.Focus

    def test_default_mode_is_background(self) -> None:
        """Default action mode is Background."""

        # ActionMode default should be Background

        # Can't test default() from Python, but BACKGROUND should be usable as default


class TestActionErrors:
    """Tests for action error handling."""

    @pytest.mark.requires_app
    def test_click_on_nonexistent_element_raises(self, calculator_app: TestApp) -> None:
        """Clicking destroyed/invalid element raises error."""
        import axterminator as ax

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

        # Terminate app
        calculator_app.terminate()
        time.sleep(0.5)

        # Clicking should now fail
        with pytest.raises(RuntimeError):
            element.click()

    def test_action_not_supported_error(self) -> None:
        """Actions not supported by element raise appropriate error."""
        # Some elements don't support certain actions
        # This would need specific UI elements to test
        pass


class TestActionPerformance:
    """Performance tests for actions."""

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

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

        result = perf_timer(
            lambda: element.click(),
            iterations=50,
            name="background_click",
        )

        # Click should be sub-10ms p95
        assert result.p95_ms < 10, f"Click too slow: {result.p95_ms}ms"

    @pytest.mark.slow
    @pytest.mark.requires_app
    def test_double_click_performance(
        self,
        calculator_app: TestApp,
        perf_timer: Callable[..., PerformanceResult],
    ) -> None:
        """Double-click includes internal delay but should still be fast."""
        import axterminator as ax

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

        result = perf_timer(
            lambda: element.double_click(),
            iterations=20,
            name="double_click",
        )

        # Double-click has 50ms internal delay, so allow 100ms
        assert result.p95_ms < 150, f"Double-click too slow: {result.p95_ms}ms"


class TestScreenshot:
    """Tests for screenshot functionality."""

    @pytest.mark.requires_app
    def test_app_screenshot(self, calculator_app: TestApp) -> None:
        """App screenshot returns PNG bytes."""
        import axterminator as ax

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

        try:
            data = app.screenshot()

            assert data is not None
            assert len(data) > 0
            # PNG magic bytes
            assert data[:4] == b"\x89PNG" or len(data) > 100
        except RuntimeError:
            # Screenshot may require additional permissions
            pass

    @pytest.mark.requires_app
    def test_element_screenshot(self, calculator_app: TestApp) -> None:
        """Element screenshot captures just that element."""
        import axterminator as ax

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

        try:
            data = element.screenshot()

            assert data is not None
        except RuntimeError:
            # May not be implemented
            pass