-
Notifications
You must be signed in to change notification settings - Fork 8
/
PHPSikuli.php
1530 lines (1230 loc) · 37 KB
/
PHPSikuli.php
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'Exceptions.inc';
/**
* PHPSikuli is a PHP wrapper for Sikuli.
*
* @category PHP
* @package php-sikuli
* @copyright 2013 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/php-sikuli/blob/master/licence.txt BSD Licence
* @link https://github.com/squizlabs/php-sikuli
*/
class PHPSikuli
{
/**
* Interactive Sikuli Jython session resouce handle.
*
* @var resource
*/
private $_sikuliHandle = NULL;
/**
* Sikuli input resource handle.
*
* @var resource
*/
private $_sikuliInput = NULL;
/**
* Sikuli output resource handle.
*
* @var resource
*/
private $_sikuliOutput = NULL;
/**
* Sikuli error resource handle.
*
* @var resource
*/
private $_sikuliError = NULL;
/**
* Sikuli command timeout in seconds.
*
* @var integer
*/
private $_sikuliCMDTimeout = 15;
/**
* Number of variables created.
*
* @var resource
*/
private $_varCount = 0;
/**
* Set to TRUE when interactive Sikuli Jython session is created.
*
* @var boolean
*/
private $_connected = FALSE;
/**
* Name of the Operating System the PHP is running on.
*
* @var string
*/
private $_os = NULL;
/**
* Default region to use for find commands.
*
* @var string
*/
private $_defaultRegion = NULL;
/**
* If TRUE then debugging output will be printed.
*
* @var boolean
*/
private $_debugging = FALSE;
/**
* Constructor.
*/
public function __construct()
{
$this->connect();
}//end __construct()
/**
* Find a particular GUI element.
*
* @param string $ps A Pattern object or Path to an image file or text.
* @param string $region Region or a Match object.
* @param float $similarity The similarity setting.
*
* @return string
*/
public function find($ps, $region=NULL, $similarity=NULL)
{
if ($region === NULL) {
$region = $this->_defaultRegion;
} else if ($region < 0) {
$region = NULL;
}
if ($similarity !== NULL && file_exists($ps) === TRUE) {
// If ps is not a file then ignore this setting.
$pattern = $this->createPattern($ps);
$pattern = $this->similar($pattern, $similarity);
$ps = $pattern;
}
$var = $this->callFunc('find', array($ps), $region, TRUE);
return $var;
}//end find()
/**
* Find GUI elements.
*
* @param string $ps A Pattern object or Path to an image file or text.
* @param string $region Region or a Match object.
* @param float $similarity The similarity setting.
*
* @return array
*/
public function findAll($ps, $region=NULL, $similarity=NULL)
{
if ($region === NULL) {
$region = $this->_defaultRegion;
} else if ($region < 0) {
$region = NULL;
}
if ($similarity !== NULL && file_exists($ps) === TRUE) {
// If ps is not a file then ignore this setting.
$pattern = $this->createPattern($ps);
$pattern = $this->similar($pattern, $similarity);
$ps = $pattern;
}
$foundObjects = array();
$match = $this->callFunc('findAll', array($ps), $region, TRUE);
do {
$var = $this->callFunc('next', array(), $match, TRUE);
if (empty($var) === FALSE) {
$this->sendCmd('True if '.$var.' is None else False');
if ($this->_getStreamOutput() === 'True') {
break;
}
$foundObjects[] = $var;
}
} while (empty($var) === FALSE);
return $foundObjects;
}//end findAll()
/**
* Sets the auto timeout.
*
* @param float $timeout The timeout in seconds.
* @param string $region Region object.
*
* @return void
*/
public function setAutoWaitTimeout($timeout, $region=NULL)
{
if ($region === NULL) {
$region = $this->_defaultRegion;
}
$this->callFunc('setAutoWaitTimeout', array($timeout, '_noQuotes' => TRUE), $region);
}//end setAutoWaitTimeout()
/*
Mouse & Keyboard
*/
/**
* Returns the location of the mouse pointer.
*
* @return string
*/
public function getMouseLocation()
{
return $this->callFunc('getMouseLocation', array(), 'Env', TRUE);
}//end getMouseLocation()
/**
* Clicks the specified location.
*
* @param string $psmrl A Pattern, String, Match, Region or Location.
* @param string $modifiers One or more key modifiers.
*
* @return void
*/
public function click($psmrl, $modifiers=NULL)
{
$this->callFunc('click', array($psmrl, $modifiers), NULL, FALSE);
}//end click()
/**
* Double clicks the specified location.
*
* @param string $psmrl A Pattern, String, Match, Region or Location.
* @param string $modifiers One or more key modifiers.
*
* @return void
*/
public function doubleClick($psmrl, $modifiers=NULL)
{
$this->callFunc('doubleClick', array($psmrl, $modifiers), NULL, FALSE);
}//end doubleClick()
/**
* Triple clicks the specified location.
*
* @param string $psmrl A Pattern, String, Match, Region or Location.
* @param string $modifiers One or more key modifiers.
*
* @return void
*/
public function tripleClick($psmrl, $modifiers=NULL)
{
$this->doubleClick($psmrl, $modifiers);
usleep(100);
$this->mouseDown('Button.LEFT');
$this->mouseUp('Button.LEFT');
}//end tripleClick()
/**
* Right clicks the specified location.
*
* @param string $psmrl A Pattern, String, Match, Region or Location.
* @param string $modifiers One or more key modifiers.
*
* @return void
*/
public function rightClick($psmrl, $modifiers=NULL)
{
$this->callFunc('rightClick', array($psmrl, $modifiers), NULL, FALSE);
}//end rightClick()
/**
* Perform a drag & drop from a start to end point.
*
* @param string $start The start PSMRL.
* @param string $end The end PSMRL.
*
* @return void
*/
public function dragDrop($start, $end)
{
$this->callFunc('dragDrop', array($start, $end));
}//end dragDrop()
/**
* Start a drag operation.
*
* @param string $start The start PSMRL.
*
* @return void
*/
public function drag($start)
{
$this->callFunc('drag', array($start));
}//end drag()
/**
* Complete a drag and drop operation by dropping at the given point.
*
* @param string $end The end PSMRL.
*
* @return void
*/
public function dropAt($end)
{
$this->callFunc('dropAt', array($end));
}//end dropAt()
/**
* Move the mouse pointer to a location indicated by PSRML.
*
* @param string $psmrl A Pattern, String, Match, Region or Location.
*
* @return void
*/
public function mouseMove($psmrl)
{
$this->callFunc('mouseMove', array($psmrl));
}//end mouseMove()
/**
* Press the specified mouse button down.
*
* @param string $button Button.LEFT, Button.MIDDLE or Button.RIGHT.
*
* @return void
*/
public function mouseDown($button)
{
$this->callFunc('mouseDown', array($button, '_noQuotes' => TRUE));
}//end mouseDown()
/**
* Releases the specified mouse button.
*
* @param string $button If not specified the previous buttons that were pressed are released.
*
* @return void
*/
public function mouseUp($button=NULL)
{
$this->callFunc('mouseUp', array($button, '_noQuotes' => TRUE));
}//end mouseUp()
/**
* Move the mouse pointer by given X and Y offsets.
*
* @param integer $offsetX The X offset.
* @param integer $offsetY The Y offset.
*
* @return string
*/
public function mouseMoveOffset($offsetX, $offsetY)
{
$mouseLocation = $this->getMouseLocation();
$this->setLocation(
$mouseLocation,
($this->getX($mouseLocation) + $offsetX),
($this->getY($mouseLocation) + $offsetY)
);
$this->mouseMove($mouseLocation);
return $mouseLocation;
}//end mouseMoveOffset()
/**
* Turns the mouse wheel.
*
* @param integer $steps Number of steps. A positive value will scroll down
* and a negative value will scroll up.
* @param string $psmrl A Pattern, String, Match, Region or Location.
*
* @return void
*/
public function wheel($steps, $psmrl=NULL)
{
$dir = NULL;
if ($steps > 0) {
$dir = 'WHEEL_DOWN';
} else {
$dir = 'WHEEL_UP';
}
$psmrl = $this->_defaultRegion;
$args = array(
'_noQuotes' => TRUE,
$psmrl,
$dir,
abs($steps),
);
$this->callFunc('wheel', $args);
}//end wheel()
/**
* Returns a valid Sikuli key combination string.
*
* @param string $keysStr Keys combination.
*
* @return string
*/
private function _extractKeys($keysStr)
{
if (empty($keysStr) === TRUE) {
return NULL;
}
$str = array();
$keys = explode('+', $keysStr);
foreach ($keys as $key) {
$key = trim($key);
if (strpos($key, 'Key.') === 0) {
if ($key === 'Key.CMD' && $this->getOS() === 'windows') {
$key = 'Key.CTRL';
}
// Special key.
$str[] = $key;
} else {
$str[] = '"'.$key.'"';
}
}
$str = implode('+', $str);
return $str;
}//end _extractKeys()
/**
* Executes keyDown event.
*
* @param string $keysStr Keys to press.
* @param boolean $holdDown If TRUE then keyUp() event will not be executed right
* after keyDown.
*
* @return void
*/
public function keyDown($keysStr, $holdDown=FALSE)
{
$keys = $this->_extractKeys($keysStr);
$this->callFunc('keyDown', array($keys, '_noQuotes' => TRUE));
if ($holdDown === FALSE) {
$this->callFunc('keyUp');
}
}//end keyDown()
/**
* Executes keyUp event.
*
* @param string $keysStr Keys to release, if none specified all keys are released.
*
* @return void
*/
public function keyUp($keysStr=NULL)
{
$keys = $this->_extractKeys($keysStr);
$this->callFunc('keyUp', array($keys, '_noQuotes' => TRUE));
}//end keyUp()
/**
* Pastes the given content.
*
* @param string $text The text to paste.
*
* @return void
*/
public function paste($text)
{
$this->callFunc('paste', array($text));
}//end paste()
/**
* Returns the contents of the clipboard.
*
* @return string
*/
public function getClipboard()
{
return $this->callFunc('getClipboard', array(), 'Env');
}//end getClipboard()
/**
* Extract the text contained in the region using OCR.
*
* @param string $region The region variable to use.
*
* @return string
*/
public function text($region=NULL)
{
return $this->callFunc('text', array(), $region);
}//end text()
/**
* Type the text at the current focused input field or at a click point specified by PSMRL.
*
* @param string $text The text to type.
* @param string $modifiers Key modifiers.
* @param string $psmrl PSMRL variable.
*
* @return integer
*/
public function type($text, $modifiers=NULL, $psmrl=NULL)
{
$retval = NULL;
$modifiers = $this->_extractKeys($modifiers);
if (is_numeric($text) === TRUE) {
$text = "'".$text."'";
$retval = $this->callFunc('type', array($psmrl, $text, $modifiers, '_noQuotes' => TRUE));
} else {
$retval = $this->callFunc('type', array($psmrl, $text, $modifiers));
}
return $retval;
}//end type()
/*
Pattern, Region, Location.
*/
/**
* Creates a Pattern object using the given image.
*
* @param string $image Path to the image.
*
* @return string
* @throws PHPSikuliException If the image does not exist.
*/
public function createPattern($image)
{
if (file_exists($image) === FALSE) {
throw new PHPSikuliException('Image file does not exist: '.$image);
}
$var = $this->callFunc('Pattern', array($image), NULL, TRUE);
return $var;
}//end createPattern()
/**
* Creates a new Region object.
*
* @param integer $x The X position of the region.
* @param integer $y The Y position of the region.
* @param integer $w The width of the region.
* @param integer $h The height of the region.
*
* @return string
*/
public function createRegion($x, $y, $w, $h)
{
$var = $this->callFunc('Region', array($x, $y, $w, $h), NULL, TRUE);
return $var;
}//end createRegion()
/**
* Creates a new Location object.
*
* @param integer $x The X position of the new location.
* @param integer $y The Y position of the new location.
*
* @return string
*/
public function createLocation($x, $y)
{
$var = $this->callFunc('Location', array($x, $y), NULL, TRUE);
return $var;
}//end createLocation()
/**
* Sets the default region to use for find commands if not specified.
*
* @param string $region The region variable.
*
* @return void
*/
public function setDefaultRegion($region)
{
$this->_defaultRegion = $region;
}//end setDefaultRegion()
/**
* Creates a new Pattern object with the specified minimum similarity set.
*
* @param string $patternObj The pattern variable.
* @param float $similarity The similarity value between 0 and 1.
*
* @return string
*/
public function similar($patternObj, $similarity=0.7)
{
if ($similarity === NULL) {
$similarity = 0.7;
}
$var = $this->callFunc('similar', array($similarity), $patternObj, TRUE);
return $var;
}//end similar()
/**
* Sets the region's X attribute.
*
* @param string $obj The region object.
* @param integer $val The new value.
*
* @return void
*/
public function setX($obj, $val)
{
$ret = NULL;
$this->callFunc('setX', array($val), $obj);
}//end setX()
/**
* Sets the region's Y attribute.
*
* @param string $obj The region object.
* @param integer $val The new value.
*
* @return void
*/
public function setY($obj, $val)
{
$this->callFunc('setY', array($val), $obj);
}//end setY()
/**
* Sets the region's W attribute.
*
* @param string $obj The region object.
* @param integer $val The new value.
*
* @return void
*/
public function setW($obj, $val)
{
$this->callFunc('setW', array($val), $obj);
}//end setW()
/**
* Sets the region's H attribute.
*
* @param string $obj The region object.
* @param integer $val The new value.
*
* @return void
*/
public function setH($obj, $val)
{
$this->callFunc('setH', array($val), $obj);
}//end setH()
/**
* Returns the region's X attribute.
*
* @param string $obj The region object.
*
* @return integer
*/
public function getX($obj)
{
$ret = (int) $this->callFunc('getX', array(), $obj);
return $ret;
}//end getX()
/**
* Returns the region's Y attribute.
*
* @param string $obj The region object.
*
* @return integer
*/
public function getY($obj)
{
$ret = (int) $this->callFunc('getY', array(), $obj);
return $ret;
}//end getY()
/**
* Returns the region's W attribute.
*
* @param string $obj The region object.
*
* @return integer
*/
public function getW($obj)
{
$ret = (int) $this->callFunc('getW', array(), $obj);
return $ret;
}//end getW()
/**
* Returns the region's H attribute.
*
* @param string $obj The region object.
*
* @return integer
*/
public function getH($obj)
{
$ret = (int) $this->callFunc('getH', array(), $obj);
return $ret;
}//end getH()
/**
* Returns the top left location.
*
* @param string $obj The region object.
*
* @return Location
*/
public function getTopLeft($obj)
{
$loc = $this->callFunc('getTopLeft', array(), $obj, TRUE);
return $loc;
}//end getTopLeft()
/**
* Returns the top right location.
*
* @param string $obj The region object.
*
* @return Location
*/
public function getTopRight($obj)
{
$loc = $this->callFunc('getTopRight', array(), $obj, TRUE);
return $loc;
}//end getTopRight()
/**
* Returns the bottom left location.
*
* @param string $obj The region object.
*
* @return Location
*/
public function getBottomLeft($obj)
{
$loc = $this->callFunc('getBottomLeft', array(), $obj, TRUE);
return $loc;
}//end getBottomLeft()
/**
* Returns the bottom right location.
*
* @param string $obj The region object.
*
* @return Location
*/
public function getBottomRight($obj)
{
$loc = $this->callFunc('getBottomRight', array(), $obj, TRUE);
return $loc;
}//end getBottomRight()
/**
* Returns the center location.
*
* @param string $obj The region object.
*
* @return Location
*/
public function getCenter($obj)
{
$loc = $this->callFunc('getCenter', array(), $obj, TRUE);
return $loc;
}//end getCenter()
/**
* Extends the given region to the right.
*
* @param string $obj The region object.
* @param integer $range Number of pixels to extend by.
*
* @return Region
*/
public function extendRight($obj, $range=NULL)
{
return $this->callFunc('right', array($range), $obj, TRUE);
}//end extendRight()
/**
* Sets the location of a Location object.
*
* @param string $locObj The Location object var name.
* @param integer $x The new X position.
* @param integer $y The new Y position.
*
* @return string
*/
public function setLocation($locObj, $x, $y)
{
$loc = $this->callFunc('setLocation', array($x, $y), $locObj);
return $loc;
}//end setLocation()
/**
* Captures the given region and returns the created image path.
*
* @param mixed $psmrl A Pattern, String, Match, Region or Location..
* @param string $obj The object var name.
*
* @return string
*/
public function capture($psmrl=array(), $obj=NULL)
{
if (is_array($psmrl) === FALSE) {
$psmrl = array($psmrl);
} else if (empty($psmrl) === TRUE) {
$psmrl = array(
'SCREEN.getBounds()',
'_noQuotes' => TRUE,
);
}
$imagePath = $this->callFunc('capture', $psmrl, $obj);
$matches = array();
preg_match('#u\'(.+)\'#', $imagePath, $matches);
$imagePath = $matches[1];
return $imagePath;
}//end capture()
/**
* Sets a Sikuli setting.
*
* @param string $setting Name of the setting.
* @param string $value Value of the setting.
*
* @return void
*/
public function setSetting($setting, $value)
{
$this->sendCmd('Settings.'.$setting.' = '.$value);
$this->_getStreamOutput();
}//end setSetting()
/*
Tests.
*/
/**
* Checks that given pattern exists on the screen or specified region.
*
* @param string $ps The pattern or text.
* @param string $obj The region object.
* @param integer $seconds The number of seconds to wait.
*
* @return boolean
*/
public function exists($ps, $obj=NULL, $seconds=NULL)
{
if ($obj === NULL) {
$obj = $this->_defaultRegion;
}
$ret = $this->callFunc('exists', array($ps, $seconds), $obj);
if (strpos($ret, 'Match[') === 0) {
return TRUE;
}
return FALSE;
}//end exists()
/**
* Switches to the specifed application.
*
* @param string $name The name of the application to switch to.
*
* @return string
*/
public function switchApp($name)
{
if ($this->getOS() === 'windows') {
$app = $this->callFunc('switchApp', array($name), NULL, TRUE);
sleep(2);
return $this->callFunc('App.focusedWindow', array(), NULL, TRUE);
} else {
$app = $this->callFunc('App', array($name), NULL, TRUE);
return $this->callFunc('focus', array(), $app, TRUE);
}
}//end switchApp()
/**
* Print the given string.
*
* @param string $str The string to print.
*
* @return void
*/
public function printVar($str)
{
echo $this->sendCmd('print '.$str);
}//end printVar()
/**
* Returns the free memory remaining in Java.
*
* @return integer
*/
public function getMemoryAvailable()
{
$this->sendCmd('from java.lang import Runtime;print Runtime.getRuntime().freeMemory()');
$memory = (int) $this->_getStreamOutput();
return $memory;
}//end getMemoryAvailable()
/**
* Highlights the specified region for given seconds.
*
* @param string $region The region variable.
* @param integer $seconds The number of seconds to highlight.
*
* @return void
*/
public function highlight($region=NULL, $seconds=1)