-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_pyautogui.py
823 lines (673 loc) · 32.4 KB
/
test_pyautogui.py
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
from __future__ import division, print_function
import os
import random
import sys
import threading
import time
import unittest
import doctest
from collections import namedtuple # Added in Python 2.6.
import pyautogui
# Make the cwd the folder that this test_pyautogui.py file resides in:
scriptFolder = os.path.dirname(os.path.realpath(__file__))
os.chdir(scriptFolder)
runningOnPython2 = sys.version_info[0] == 2
if runningOnPython2:
INPUT_FUNC = raw_input
else:
INPUT_FUNC = input
try:
import pytweening
except:
assert False, "The PyTweening module must be installed to complete the tests: pip install pytweening"
try:
import pyscreeze
except:
assert False, "The PyScreeze module must be installed to complete the tests: pip install pyscreeze"
"""
# TODO - pygetwindow is Windows-only for now, so don't require it for testing just yet.
try:
import pygetwindow
except:
assert False, 'The PyGetWindow module must be installed to complete the tests: pip install pygetwindow'
"""
# TODO - note that currently most of the click-related functionality is not tested.
class P(namedtuple("P", ["x", "y"])):
"""Simple, immutable, 2D point/vector class, including some basic
arithmetic functions.
"""
def __str__(self):
return "{0},{1}".format(self.x, self.y)
def __repr__(self):
return "P({0}, {1})".format(self.x, self.y)
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __ne__(self, other):
return self.x != other.x and self.y != other.y
def __add__(self, other):
return P(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return P(self.x - other.x, self.y - other.y)
def __mul__(self, other):
return P(self.x * other, self.y * other)
def __rmul__(self, other):
return self.__mul__(other)
def __floordiv__(self, other):
return P(self.x // other, self.y // other)
def __truediv__(self, other):
return P(self.x / other, self.y / other)
def __neg__(self):
return P(-self.x, -self.y)
def __pos__(self):
return self
def __neg__(self):
return P(abs(self.x), abs(self.y))
class TestGeneral(unittest.TestCase):
def setUp(self):
self.oldFailsafeSetting = pyautogui.FAILSAFE
pyautogui.FAILSAFE = False
pyautogui.moveTo(42, 42) # make sure failsafe isn't triggered during this test
pyautogui.FAILSAFE = True
def tearDown(self):
pyautogui.FAILSAFE = self.oldFailsafeSetting
def test_accessibleNames(self):
# Check that all the functions are defined.
# mouse-related API
pyautogui.moveTo
pyautogui.moveRel
pyautogui.dragTo
pyautogui.dragRel
pyautogui.mouseDown
pyautogui.mouseUp
pyautogui.click
pyautogui.rightClick
pyautogui.doubleClick
pyautogui.tripleClick
# keyboard-related API
pyautogui.typewrite
pyautogui.hotkey
pyautogui.keyDown
pyautogui.keyUp
pyautogui.press
# The functions implemented in the platform-specific modules should also show up in the pyautogui namespace:
pyautogui.position
pyautogui.size
pyautogui.scroll
pyautogui.hscroll
pyautogui.vscroll
# util API
pyautogui.KEYBOARD_KEYS
pyautogui.isShiftCharacter
# Screenshot-related API
pyautogui.locateAll
pyautogui.locate
pyautogui.locateOnScreen
pyautogui.locateAllOnScreen
pyautogui.locateCenterOnScreen
pyautogui.center
pyautogui.pixelMatchesColor
pyautogui.pixel
pyautogui.screenshot
pyautogui.grab
# TODO(denilsonsa): I believe we should get rid of these symbols. If someone wants tweening, import pytweening module instead!
# Tweening-related API
pyautogui.getPointOnLine
pyautogui.linear
pyautogui.easeInQuad
pyautogui.easeOutQuad
pyautogui.easeInOutQuad
pyautogui.easeInCubic
pyautogui.easeOutCubic
pyautogui.easeInOutCubic
pyautogui.easeInQuart
pyautogui.easeOutQuart
pyautogui.easeInOutQuart
pyautogui.easeInQuint
pyautogui.easeOutQuint
pyautogui.easeInOutQuint
pyautogui.easeInSine
pyautogui.easeOutSine
pyautogui.easeInOutSine
pyautogui.easeInExpo
pyautogui.easeOutExpo
pyautogui.easeInOutExpo
pyautogui.easeInCirc
pyautogui.easeOutCirc
pyautogui.easeInOutCirc
pyautogui.easeInElastic
pyautogui.easeOutElastic
pyautogui.easeInOutElastic
pyautogui.easeInBack
pyautogui.easeOutBack
pyautogui.easeInOutBack
pyautogui.easeInBounce
pyautogui.easeOutBounce
pyautogui.easeInOutBounce
def test_size(self):
width, height = pyautogui.size()
self.assertTrue(isinstance(width, int), "Type of width is %s" % (type(width)))
self.assertTrue(isinstance(height, int), "Type of height is %s" % (type(height)))
self.assertTrue(width > 0, "Width is set to %s" % (width))
self.assertTrue(height > 0, "Height is set to %s" % (height))
def test_position(self):
mousex, mousey = pyautogui.position()
self.assertTrue(isinstance(mousex, int), "Type of mousex is %s" % (type(mousex)))
self.assertTrue(isinstance(mousey, int), "Type of mousey is %s" % (type(mousey)))
# Test passing x and y arguments to position().
pyautogui.moveTo(mousex + 1, mousey + 1)
x, y = pyautogui.position(mousex, None)
self.assertEqual(x, mousex)
self.assertNotEqual(y, mousey)
x, y = pyautogui.position(None, mousey)
self.assertNotEqual(x, mousex)
self.assertEqual(y, mousey)
def test_onScreen(self):
zero = P(0, 0)
xone = P(1, 0)
yone = P(0, 1)
size = P(*pyautogui.size())
half = size / 2
on_screen = [zero, zero + xone, zero + yone, zero + xone + yone, half, size - xone - yone]
off_screen = [zero - xone, zero - yone, zero - xone - yone, size - xone, size - yone, size]
for value, coords in [(True, on_screen), (False, off_screen)]:
for coord in coords:
self.assertEqual(
value,
pyautogui.onScreen(*coord),
"onScreen({0}, {1}) should be {2}".format(coord.x, coord.y, value),
)
self.assertEqual(
value,
pyautogui.onScreen(list(coord)),
"onScreen([{0}, {1}]) should be {2}".format(coord.x, coord.y, value),
)
self.assertEqual(
value,
pyautogui.onScreen(tuple(coord)),
"onScreen(({0}, {1})) should be {2}".format(coord.x, coord.y, value),
)
self.assertEqual(
value, pyautogui.onScreen(coord), "onScreen({0}) should be {1}".format(repr(coord), value)
)
# These raise PyAutoGUIException.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui.onScreen([0, 0], 0)
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui.onScreen((0, 0), 0)
def test_pause(self):
oldValue = pyautogui.PAUSE
startTime = time.time()
pyautogui.PAUSE = 0.35 # there should be a 0.35 second pause after each call
pyautogui.moveTo(1, 1)
pyautogui.moveRel(0, 1)
pyautogui.moveTo(1, 1)
elapsed = time.time() - startTime
self.assertTrue(1.0 < elapsed < 1.1, "Took %s seconds, expected 1.0 < 1.1 seconds." % (elapsed))
pyautogui.PAUSE = oldValue # restore the old PAUSE value
class TestHelperFunctions(unittest.TestCase):
def test__normalizeXYArgs(self):
self.assertEqual(pyautogui._normalizeXYArgs(1, 2), pyautogui.Point(x=1, y=2))
self.assertEqual(pyautogui._normalizeXYArgs((1, 2), None), pyautogui.Point(x=1, y=2))
self.assertEqual(pyautogui._normalizeXYArgs([1, 2], None), pyautogui.Point(x=1, y=2))
pyautogui.useImageNotFoundException()
with self.assertRaises(pyautogui.ImageNotFoundException):
pyautogui._normalizeXYArgs("100x100blueimage.png", None)
pyautogui.useImageNotFoundException(False)
self.assertEqual(pyautogui._normalizeXYArgs("100x100blueimage.png", None), None)
class TestDoctests(unittest.TestCase):
def test_doctests(self):
doctest.testmod(pyautogui)
class TestMouse(unittest.TestCase):
# NOTE - The user moving the mouse during many of these tests will cause them to fail.
# There is no need to test all tweening functions.
TWEENS = [
"linear",
"easeInElastic",
"easeOutElastic",
"easeInOutElastic",
"easeInBack",
"easeOutBack",
"easeInOutBack",
]
def setUp(self):
self.oldFailsafeSetting = pyautogui.FAILSAFE
self.center = P(*pyautogui.size()) // 2
pyautogui.FAILSAFE = False
pyautogui.moveTo(*self.center) # make sure failsafe isn't triggered during this test
pyautogui.FAILSAFE = True
def tearDown(self):
pyautogui.FAILSAFE = self.oldFailsafeSetting
def test_moveTo(self):
# moving the mouse
desired = self.center
pyautogui.moveTo(*desired)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# no coordinate specified (should be a NO-OP)
pyautogui.moveTo(None, None)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# moving the mouse to a new location
desired += P(42, 42)
pyautogui.moveTo(*desired)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# moving the mouse over time (1/5 second)
desired -= P(42, 42)
pyautogui.moveTo(desired.x, desired.y, duration=0.2)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a list instead of separate x and y.
desired += P(42, 42)
pyautogui.moveTo(list(desired))
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a tuple instead of separate x and y.
desired += P(42, 42)
pyautogui.moveTo(tuple(desired))
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a sequence-like object instead of separate x and y.
desired -= P(42, 42)
pyautogui.moveTo(desired)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
def test_moveToWithTween(self):
origin = self.center - P(100, 100)
destination = self.center + P(100, 100)
def resetMouse():
pyautogui.moveTo(*origin)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, origin)
for tweenName in self.TWEENS:
tweenFunc = getattr(pyautogui, tweenName)
resetMouse()
pyautogui.moveTo(destination.x, destination.y, duration=pyautogui.MINIMUM_DURATION * 2, tween=tweenFunc)
mousepos = P(*pyautogui.position())
self.assertEqual(
mousepos,
destination,
"%s tween move failed. mousepos set to %s instead of %s" % (tweenName, mousepos, destination),
)
def test_moveRel(self):
# start at the center
desired = self.center
pyautogui.moveTo(*desired)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move down and right
desired += P(42, 42)
pyautogui.moveRel(42, 42)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move up and left
desired -= P(42, 42)
pyautogui.moveRel(-42, -42)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move right
desired += P(42, 0)
pyautogui.moveRel(42, 0)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move down
desired += P(0, 42)
pyautogui.moveRel(0, 42)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move left
desired += P(-42, 0)
pyautogui.moveRel(-42, 0)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# move up
desired += P(0, -42)
pyautogui.moveRel(0, -42)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a list instead of separate x and y.
desired += P(42, 42)
pyautogui.moveRel([42, 42])
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a tuple instead of separate x and y.
desired -= P(42, 42)
pyautogui.moveRel((-42, -42))
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
# Passing a sequence-like object instead of separate x and y.
desired += P(42, 42)
pyautogui.moveRel(P(42, 42))
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, desired)
def test_moveRelWithTween(self):
origin = self.center - P(100, 100)
delta = P(200, 200)
destination = origin + delta
def resetMouse():
pyautogui.moveTo(*origin)
mousepos = P(*pyautogui.position())
self.assertEqual(mousepos, origin)
for tweenName in self.TWEENS:
tweenFunc = getattr(pyautogui, tweenName)
resetMouse()
pyautogui.moveRel(delta.x, delta.y, duration=pyautogui.MINIMUM_DURATION * 2, tween=tweenFunc)
mousepos = P(*pyautogui.position())
self.assertEqual(
mousepos,
destination,
"%s tween move failed. mousepos set to %s instead of %s" % (tweenName, mousepos, destination),
)
def test_scroll(self):
# TODO - currently this just checks that scrolling doesn't result in an error.
pyautogui.scroll(1)
pyautogui.scroll(-1)
pyautogui.hscroll(1)
pyautogui.hscroll(-1)
pyautogui.vscroll(1)
pyautogui.vscroll(-1)
class TestRun(unittest.TestCase):
def test_getNumberToken(self):
self.assertEqual(pyautogui._getNumberToken("5hello"), "5")
self.assertEqual(pyautogui._getNumberToken("-5hello"), "-5")
self.assertEqual(pyautogui._getNumberToken("+5hello"), "+5")
self.assertEqual(pyautogui._getNumberToken("5.5hello"), "5.5")
self.assertEqual(pyautogui._getNumberToken("+5.5hello"), "+5.5")
self.assertEqual(pyautogui._getNumberToken("-5.5hello"), "-5.5")
self.assertEqual(pyautogui._getNumberToken(" 5hello"), " 5")
self.assertEqual(pyautogui._getNumberToken(" -5hello"), " -5")
self.assertEqual(pyautogui._getNumberToken(" +5hello"), " +5")
self.assertEqual(pyautogui._getNumberToken(" 5.5hello"), " 5.5")
self.assertEqual(pyautogui._getNumberToken(" +5.5hello"), " +5.5")
self.assertEqual(pyautogui._getNumberToken(" -5.5hello"), " -5.5")
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getNumberToken("") # Blank string and no number.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getNumberToken("hello") # Missing a number.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getNumberToken(" ") # Missing a number.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getNumberToken("hello 42") # Number is not at the start.
def test_getQuotedStringToken(self):
self.assertEqual(pyautogui._getQuotedStringToken("'hello'world"), "'hello'")
self.assertEqual(pyautogui._getQuotedStringToken("''world"), "''")
self.assertEqual(pyautogui._getQuotedStringToken(" 'hello'world"), " 'hello'")
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("xyz") # No quotes.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("xyz") # No quotes.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken(" xyz") # No quotes, spaces in front.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("'xyz") # Start quote only.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("xyz'") # End quote only.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken('"xyz"') # Double quotes don't count.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("") # Blank string.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getQuotedStringToken("xyz 'hello'") # Quoted string is not at the start.
def test_getCommaToken(self):
self.assertEqual(pyautogui._getCommaToken(","), ",")
self.assertEqual(pyautogui._getCommaToken(" ,"), " ,")
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getCommaToken("") # Blank string.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getCommaToken("hello,") # Comma is not at the start.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getCommaToken("hello") # No comma.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getCommaToken(" ") # No comma.
def test_getParensCommandStrToken(self):
self.assertEqual(pyautogui._getParensCommandStrToken("()"), "()")
self.assertEqual(pyautogui._getParensCommandStrToken(" ()"), " ()")
self.assertEqual(pyautogui._getParensCommandStrToken("()hello"), "()")
self.assertEqual(pyautogui._getParensCommandStrToken(" ()hello"), " ()")
self.assertEqual(pyautogui._getParensCommandStrToken("(hello)world"), "(hello)")
self.assertEqual(pyautogui._getParensCommandStrToken(" (hello)world"), " (hello)")
self.assertEqual(pyautogui._getParensCommandStrToken("(he(ll)(o))world"), "(he(ll)(o))")
self.assertEqual(pyautogui._getParensCommandStrToken(" (he(ll)(o))world"), " (he(ll)(o))")
self.assertEqual(
pyautogui._getParensCommandStrToken("(he(ll)(o)))world"), "(he(ll)(o))"
) # Extra close parentheses.
self.assertEqual(
pyautogui._getParensCommandStrToken(" (he(ll)(o)))world"), " (he(ll)(o))"
) # Extra close parentheses.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken("") # Blank string.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken(" ") # No parens.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken("hello") # No parens
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken(" (") # No close parenthesis.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken("(he(ll)o") # Not enough close parentheses.
with self.assertRaises(pyautogui.PyAutoGUIException):
pyautogui._getParensCommandStrToken("") # Blank string.
def test_tokenizeCommandStr(self):
self.assertEqual(pyautogui._tokenizeCommandStr(""), []) # Empty command string.
self.assertEqual(pyautogui._tokenizeCommandStr(" "), []) # Whitespace only command string.
self.assertEqual(pyautogui._tokenizeCommandStr("c"), ["c"])
self.assertEqual(pyautogui._tokenizeCommandStr(" c "), ["c"])
self.assertEqual(pyautogui._tokenizeCommandStr("ccc"), ["c", "c", "c"])
self.assertEqual(pyautogui._tokenizeCommandStr(" c c c "), ["c", "c", "c"])
self.assertEqual(pyautogui._tokenizeCommandStr("clmr"), ["c", "l", "m", "r"])
self.assertEqual(pyautogui._tokenizeCommandStr("susdss"), ["su", "sd", "ss"])
self.assertEqual(pyautogui._tokenizeCommandStr(" su sd ss "), ["su", "sd", "ss"])
self.assertEqual(pyautogui._tokenizeCommandStr("clmrsusdss"), ["c", "l", "m", "r", "su", "sd", "ss"])
# Do a whole bunch of tests with random no-argument commands with random whitespace.
random.seed(42)
for i in range(100):
commands = []
commands.extend(["c"] * random.randint(0, 9))
commands.extend(["l"] * random.randint(0, 9))
commands.extend(["m"] * random.randint(0, 9))
commands.extend(["r"] * random.randint(0, 9))
commands.extend(["su"] * random.randint(0, 9))
commands.extend(["sd"] * random.randint(0, 9))
commands.extend(["ss"] * random.randint(0, 9))
random.shuffle(commands)
commandStr = []
for command in commands:
commandStr.append(command)
commandStr.append(" " * random.randint(0, 9))
commandStr = "".join(commandStr)
self.assertEqual(pyautogui._tokenizeCommandStr(commandStr), commands)
self.assertEqual(pyautogui._tokenizeCommandStr("g10,10"), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g 10,10"), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g10 ,10"), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g10, 10"), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g 10 , 10 "), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g 10 , 10 "), ["g", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g+10,+10"), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g +10,+10"), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g+10 ,+10"), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g+10, +10"), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g +10 , +10 "), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g +10 , +10 "), ["g", "+10", "+10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g-10,-10"), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g -10,-10"), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g-10 ,-10"), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr("g-10, -10"), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g -10 , -10 "), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr(" g -10 , -10 "), ["g", "-10", "-10"])
self.assertEqual(pyautogui._tokenizeCommandStr("d10,10"), ["d", "10", "10"])
self.assertEqual(pyautogui._tokenizeCommandStr("d1,2g3,4"), ["d", "1", "2", "g", "3", "4"])
self.assertEqual(pyautogui._tokenizeCommandStr("w'hello'"), ["w", "hello"])
self.assertEqual(
pyautogui._tokenizeCommandStr("d1,2w'hello'g3,4"), ["d", "1", "2", "w", "hello", "g", "3", "4"]
)
self.assertEqual(pyautogui._tokenizeCommandStr("s42"), ["s", "42"])
self.assertEqual(pyautogui._tokenizeCommandStr("s42.3"), ["s", "42.3"])
self.assertEqual(pyautogui._tokenizeCommandStr("f10(c)"), ["f", "10", ["c"]])
self.assertEqual(pyautogui._tokenizeCommandStr("f10(lmr)"), ["f", "10", ["l", "m", "r"]])
self.assertEqual(pyautogui._tokenizeCommandStr("f10(f5(cc))"), ["f", "10", ["f", "5", ["c", "c"]]])
# TODO add negative cases
# TODO add mocks for pyautogui for this.
class TypewriteThread(threading.Thread):
def __init__(self, msg, interval=0.0):
super(TypewriteThread, self).__init__()
self.msg = msg
self.interval = interval
def run(self):
time.sleep(0.25) # NOTE: BE SURE TO ACCOUNT FOR THIS QUARTER SECOND FOR TIMING TESTS!
pyautogui.typewrite(self.msg, self.interval)
class PressThread(threading.Thread):
def __init__(self, keysArg):
super(PressThread, self).__init__()
self.keysArg = keysArg
def run(self):
time.sleep(0.25) # NOTE: BE SURE TO ACCOUNT FOR THIS QUARTER SECOND FOR TIMING TESTS!
pyautogui.press(self.keysArg)
class TestKeyboard(unittest.TestCase):
# NOTE: The terminal window running this script must be in focus during the keyboard tests.
# You cannot run this as a scheduled task or remotely.
def setUp(self):
self.oldFailsafeSetting = pyautogui.FAILSAFE
pyautogui.FAILSAFE = False
pyautogui.moveTo(42, 42) # make sure failsafe isn't triggered during this test
pyautogui.FAILSAFE = True
def tearDown(self):
pyautogui.FAILSAFE = self.oldFailsafeSetting
def test_typewrite(self):
# 'Hello world!\n' test
t = TypewriteThread("Hello world!\n")
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "Hello world!")
# 'Hello world!\n' as a list argument
t = TypewriteThread(list("Hello world!\n"))
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "Hello world!")
# All printable ASCII characters test
allKeys = []
for c in range(32, 127):
allKeys.append(chr(c))
allKeys = "".join(allKeys)
t = TypewriteThread(allKeys + "\n")
t.start()
response = INPUT_FUNC()
self.assertEqual(response, allKeys)
def checkForValidCharacters(self, msg):
for c in msg:
self.assertTrue(pyautogui.isValidKey(c), '"%c" is not a valid key on platform %s' % (c, sys.platform))
def test_typewrite_slow(self):
# Test out the interval parameter to make sure it adds pauses.
t = TypewriteThread("Hello world!\n", 0.1)
startTime = time.time()
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "Hello world!")
elapsed = time.time() - startTime
self.assertTrue(1.0 < elapsed < 2.0, "Took %s seconds, expected 1.0 < x 2.0 seconds." % (elapsed))
def test_typewrite_editable(self):
# Backspace test
t = TypewriteThread(["a", "b", "c", "\b", "backspace", "x", "y", "z", "\n"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "axyz")
# TODO - Currently the arrow keys don't seem to work entirely correctly on OS X.
if sys.platform != "darwin":
# Arrow key test
t = TypewriteThread(["a", "b", "c", "left", "left", "right", "x", "\n"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "abxc")
# Del key test
t = TypewriteThread(["a", "b", "c", "left", "left", "left", "del", "delete", "\n"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "c")
# Home and end key test
t = TypewriteThread(["a", "b", "c", "home", "x", "end", "z", "\n"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "xabcz")
def test_press(self):
# '' test
t = PressThread("enter")
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "")
# 'a' test, also test sending list of 1- and multi-length strings
t = PressThread(["a", "enter"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "a")
# 'ba' test, also test sending list of 1- and multi-length strings
t = PressThread(["a", "left", "b", "enter"])
t.start()
response = INPUT_FUNC()
self.assertEqual(response, "ba")
def test_typewrite_space(self):
# Backspace test
t = TypewriteThread(["space", " ", "\n"]) # test both 'space' and ' '
t.start()
response = INPUT_FUNC()
self.assertEqual(response, " ")
def test_isShiftCharacter(self):
for char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + '~!@#$%^&*()_+{}|:"<>?':
self.assertTrue(pyautogui.isShiftCharacter(char))
for char in "abcdefghijklmnopqrstuvwxyz" + " `1234567890-=,./;'[]\\":
self.assertFalse(pyautogui.isShiftCharacter(char))
class TestFailSafe(unittest.TestCase):
def test_failsafe(self):
self.oldFailsafeSetting = pyautogui.FAILSAFE
pyautogui.moveTo(1, 1) # make sure mouse is not in failsafe position to begin with
for x, y in pyautogui.FAILSAFE_POINTS:
pyautogui.FAILSAFE = True
# When move(), moveTo(), drag(), or dragTo() moves the mouse to a
# failsafe point, it shouldn't raise the fail safe. (This would
# be annoying. Only a human moving the mouse to a failsafe point
# should trigger the failsafe.)
pyautogui.moveTo(x, y)
pyautogui.FAILSAFE = False
pyautogui.moveTo(1, 1) # make sure mouse is not in failsafe position to begin with (for the next iteration)
pyautogui.moveTo(1, 1) # make sure mouse is not in failsafe position to begin with
for x, y in pyautogui.FAILSAFE_POINTS:
pyautogui.FAILSAFE = True
pyautogui.moveTo(x, y) # This line should not cause the fail safe exception to be raised.
# A second pyautogui function call to do something while the cursor is in a fail safe point SHOULD raise the failsafe:
self.assertRaises(pyautogui.FailSafeException, pyautogui.press, "esc")
pyautogui.FAILSAFE = False
pyautogui.moveTo(1, 1) # make sure mouse is not in failsafe position to begin with (for the next iteration)
for x, y in pyautogui.FAILSAFE_POINTS:
pyautogui.FAILSAFE = False
pyautogui.moveTo(x, y) # This line should not cause the fail safe exception to be raised.
# This line shouldn't cause a failsafe to trigger because FAILSAFE is set to False.
pyautogui.press("esc")
pyautogui.FAILSAFE = self.oldFailsafeSetting
class TestPyScreezeFunctions(unittest.TestCase):
def test_locateFunctions(self):
# TODO - for now, we only test that the "return None" and "raise pyautogui.ImageNotFoundException" is raised.
pyautogui.useImageNotFoundException()
with self.assertRaises(pyautogui.ImageNotFoundException):
pyautogui.locate("100x100blueimage.png", "100x100redimage.png")
# Commenting out the locateAll*() functions because they return generators, even if the image can't be found. Should they instead raise an exception? This is a question for pyscreeze's design.
# with self.assertRaises(pyautogui.ImageNotFoundException):
# pyautogui.locateAll('100x100blueimage.png', '100x100redimage.png')
# with self.assertRaises(pyautogui.ImageNotFoundException):
# pyautogui.locateAllOnScreen('100x100blueimage.png') # NOTE: This test fails if there is a blue square visible on the screen.
with self.assertRaises(pyautogui.ImageNotFoundException):
pyautogui.locateOnScreen(
"100x100blueimage.png"
) # NOTE: This test fails if there is a blue square visible on the screen.
with self.assertRaises(pyautogui.ImageNotFoundException):
pyautogui.locateCenterOnScreen(
"100x100blueimage.png"
) # NOTE: This test fails if there is a blue square visible on the screen.
pyautogui.useImageNotFoundException(False)
self.assertEqual(pyautogui.locate("100x100blueimage.png", "100x100redimage.png"), None)
# self.assertEqual(pyautogui.locateAll('100x100blueimage.png', '100x100redimage.png'), None)
# self.assertEqual(pyautogui.locateAllOnScreen('100x100blueimage.png'), None) # NOTE: This test fails if there is a blue square visible on the screen.
self.assertEqual(
pyautogui.locateOnScreen("100x100blueimage.png"), None
) # NOTE: This test fails if there is a blue square visible on the screen.
self.assertEqual(
pyautogui.locateCenterOnScreen("100x100blueimage.png"), None
) # NOTE: This test fails if there is a blue square visible on the screen.
if __name__ == "__main__":
unittest.main()