-
Notifications
You must be signed in to change notification settings - Fork 4
/
WebDriverBackedSelenium.php
1379 lines (1203 loc) · 56.7 KB
/
WebDriverBackedSelenium.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
/*
Copyright 2011 3e software house & interactive agency
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Modified from original CWebDriverTestCase.php version...
*
* NOTE: This version is for use outside of PHPUnit, in regular
* PHP code or via a PHP shell session. This class is not for
* execution via PHPUnit directly (w/o wrappers, etc.). For that,
* use WebDriverBackedSeleniumTestCase.php.
*
* Base class for functional tests using WebDriver with PHP.
* It provides the same interface as PHPUnit's SeleniumTestCase,
* intending to allow users to more easily port existing PHP
* Selenium (RC) tests to Web Driver with minimal or no code
* changes. And/or export Selenium IDE tests (for Selenium RC)
* to PHP for WebDriver w/o much rework.
*
* NOTE: that this interface does not implement all the Selenium
* RC API methods and location strategies, though their method
* signatures are in place. If any of these get called, a
* special exception is thrown to alert the user that their
* test will need rework to run in WebDriver with PHP. See code
* below for details about the exception message returned.
*
* @author kolec
*
*/
require_once 'WebDriver.php';
class WebDriverBackedSelenium{
public $webdriver;
public $baseUrl;
public $browser;
private $seleniumSpeed; //in ms
private $confirmationChoice;
private $promptAnswer;
private $seleniumTimeout;
protected $waiting_time;// = 0.5;
protected $max_waiting_time;// = 4;
function __construct( $host="localhost", $port="4444", $browser="firefox" ) {
$this->webdriver = new WebDriver( $host, $port );
$this->browser = $browser;
$this->baseUrl = "";
$this->seleniumSpeed = 0; //default no delay
$this->confirmationChoice = "ok";
$this->promptAnswer = "";
$this->seleniumTimeout = 30000; //default of 30 sec
$this->waiting_time = 0.5;
$this->max_waiting_time = 4;
}
public function __call( $name, $arguments ) {
if( method_exists( $this->webdriver, $name ) ) {
return call_user_func_array( array( $this->webdriver, $name ), $arguments );
}
}
public function refresh(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->refresh();
}
public function back(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->back();
}
public function forward(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->forward();
}
private function parseLocator($locator){
$strategy = null;
$locatorValue = null;
switch ($locator) {
case $this->startsWith($locator,"identifier="):
$strategy = LocatorStrategy::id;
$locatorValue = substr($locator,strlen("identifier="));
break;
case $this->startsWith($locator,"id="):
$strategy = LocatorStrategy::id;
$locatorValue = substr($locator,strlen("id="));
break;
case $this->startsWith($locator,"name="):
$strategy = LocatorStrategy::name;
$locatorValue = substr($locator,strlen("name="));
break;
case $this->startsWith($locator,"link="):
$strategy = LocatorStrategy::linkText;
$locatorValue = substr($locator,strlen("link="));
break;
case $this->startsWith($locator,"//"):
$strategy = LocatorStrategy::xpath;
$locatorValue = $locator;
break;
case $this->startsWith($locator,"xpath="):
$strategy = LocatorStrategy::xpath;
$locatorValue = substr($locator,strlen("xpath="));
break;
case $this->startsWith($locator,"css="):
$strategy = LocatorStrategy::cssSelector;
$locatorValue = substr($locator,strlen("css="));
break;
case $this->startsWith($locator,"dom="):
throw new Exception("\nSelenium RC DOM locator strategy not implemented in ".get_class($this).".\n");
break;
case $this->startsWith($locator,"document."):
throw new Exception("\nSelenium RC DOM locator strategy not implemented in ".get_class($this).".\n");
break;
case $this->startsWith($locator,"ui="):
throw new Exception("\nSelenium RC UI specifier string / UI-Element locator strategy not implemented in ".get_class($this).".\n");
break;
default:
$strategy = LocatorStrategy::id;
$locatorValue = $locator;
}
return $this->getElement($strategy,$locatorValue);
}
private function parseLocatorForElements($locator){
$strategy = null;
$locatorValue = null;
switch ($locator) {
case $this->startsWith($locator,"identifier="):
$strategy = LocatorStrategy::id;
$locatorValue = substr($locator,strlen("identifier="));
break;
case $this->startsWith($locator,"id="):
$strategy = LocatorStrategy::id;
$locatorValue = substr($locator,strlen("id="));
break;
case $this->startsWith($locator,"name="):
$strategy = LocatorStrategy::name;
$locatorValue = substr($locator,strlen("name="));
break;
case $this->startsWith($locator,"link="):
$strategy = LocatorStrategy::linkText;
$locatorValue = substr($locator,strlen("link="));
break;
case $this->startsWith($locator,"//"):
$strategy = LocatorStrategy::xpath;
$locatorValue = $locator;
break;
case $this->startsWith($locator,"xpath="):
$strategy = LocatorStrategy::xpath;
$locatorValue = substr($locator,strlen("xpath="));
break;
case $this->startsWith($locator,"css="):
$strategy = LocatorStrategy::cssSelector;
$locatorValue = substr($locator,strlen("css="));
break;
case $this->startsWith($locator,"dom="):
throw new Exception("\nSelenium RC DOM locator strategy not implemented in ".get_class($this).".\n");
break;
case $this->startsWith($locator,"document."):
throw new Exception("\nSelenium RC DOM locator strategy not implemented in ".get_class($this).".\n");
break;
case $this->startsWith($locator,"ui="):
throw new Exception("\nSelenium RC UI specifier string / UI-Element locator strategy not implemented in ".get_class($this).".\n");
break;
default:
$strategy = LocatorStrategy::id;
$locatorValue = $locator;
}
return $this->getElements($strategy,$locatorValue);
}
public function focusFrame($frameId){
print "\n".__FUNCTION__.": ".$frameId."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->focusFrame($frameId);
}
public function selectFrame($frameId){
print "\n".__FUNCTION__.": ".$frameId."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->focusFrame($frameId);
}
public function setBrowserUrl( $url ) {
print "\n".__FUNCTION__.": ".$url."\n"; //log commands called like SeleniumTestCase
$this->baseUrl = $url;
}
public function open($url) {
print "\n".__FUNCTION__.": ".$url."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
if($this->startsWith($url,"/")) $urlToOpen = $this->baseUrl . $url;
else $urlToOpen = $url;
$this->webdriver->get( $urlToOpen );
$this->waitForPageToLoad();
}
public function getBodyText() {
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$html = $this->webdriver->getPageSource();
$body = preg_replace( "/.*<body[^>]*>|<\/body>.*/si", "", $html );
return $body;
}
public function isTextPresent( $text ) {
print "\n".__FUNCTION__.": ".$text."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//this may be better solution from
//http://groups.google.com/group/selenium-users/browse_thread/thread/8d15fb3a0bb95992
return $this->isElementPresent("//*[contains(.,'".$text."')]");
/* original code from CWebDriverTestCase
$found = false;
$i = 0;
do {
$html = $this->webdriver->getPageSource();
if( is_string( $html ) ) {
$found = !(strpos( $html, $text ) === false);
}
if( !$found ) {
sleep( $this->waiting_time );
$i += $this->waiting_time;
}
} while( !$found && $i <= $this->max_waiting_time );
return $found;
*/
}
//public function getAttribute( $xpath ) {
public function getAttribute( $attributeLocator ) {
print "\n".__FUNCTION__.": ".$attributeLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
list($locator,$attribute) = explode("@",$attributeLocator);
return $this->parseLocator($locator)->getAttribute($attribute);
/* less desirable method to get attribute
* we want it to work for all locators not just XPath
$body = $this->getBodyText();
$xml = new SimpleXMLElement( $body );
$nodes = $xml->xpath( "$xpath" );
return $nodes[0][0];
*/
}
public function type($locator,$value) {
print "\n".__FUNCTION__.": ".$locator." : ".$value."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset( $element ) ) {
///usleep(100*1000);
$element->sendKeys( array( $value ) );
//usleep(500*1000);
}
}
public function clear( $locator ) {
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset($element) ) {
$element->clear();
}
}
public function submit( $locator ) {
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset( $element ) ) {
$element->submit();
usleep( 500 * 1000 );
}
}
public function click($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset( $element ) ) {
$element->click();
usleep( 500 * 1000 );
}
}
public function close() {
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->closeWindow(); //not $this->webdriver->close(); to close browser session
}
/**
select item (option) in combobox
@param $select_id id of SELECT element
@param @option_text option text to select
*/
public function select( $selectLocator, $optionLocator ) {
print "\n".__FUNCTION__.": ".$selectLocator." : ".$optionLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($selectLocator);
$option = $element->findOptionElementByText( $optionLocator );
$option->click();
}
private function getElement( $strategy, $name ) {
$i = 0;
do {
try {
$element = $this->webdriver->findElementBy( $strategy, $name );
} catch( NoSuchElementException $e ) {
print_r( "\nWaiting for \"" . $name . "\" element to appear...\n" );
sleep( $this->waiting_time );
$i += $this->waiting_time;
}
} while( !isset( $element ) && $i <= $this->max_waiting_time );
if( !isset( $element ) )
throw new Exception("Element has not appeared after " . $this->max_waiting_time . " seconds.");
return $element;
}
private function getElements( $strategy, $name ) {
$i = 0;
do {
try {
$elements = $this->webdriver->findElementsBy( $strategy, $name );
} catch( NoSuchElementException $e ) {
print_r( "\nWaiting for \"" . $name . "\" elements to appear...\n" );
sleep( $this->waiting_time );
$i += $this->waiting_time;
}
} while( !isset( $elements ) && $i <= $this->max_waiting_time );
if( !isset( $elements ) )
throw new Exception("Elements have not appeared after " . $this->max_waiting_time . " seconds.");
return $elements;
}
private function startsWith($haystack, $needle){
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
/* The following methods below were added to the original class implementation
* to fully implement the SeleniumTestCase / Selenium RC.
*
* Class methods derived from Selenium command set and RC (Java) API,
* refer to these
* http://release.seleniumhq.org/selenium-core/1.0.1/reference.html
* http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/
*/
public function addLocationStrategy($strategyName,$functionDefinition){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function addScript($scriptContent,$scriptTagId){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function addSelection($locator,$optionLocator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function allowNativeXpath($allow=true){
//throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
//we'll just use whatever the default XPath support is for WebDriver for given browser, whether native or not
print "\nSelenium RC method \"".__FUNCTION__."\" not implemented. See http://code.google.com/p/selenium/wiki/XpathInWebDriver for reference.\n";
}
public function altKeyDown(){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function altKeyUp(){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function answerOnNextPrompt($answer){
print "\n".__FUNCTION__.": ".$answer."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->promptAnswer = $answer;
}
public function assignId($locator,$identifier){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function attachFile($fieldLocator,$fileLocator){
print "\n".__FUNCTION__.": ".$fieldLocator." : ".$fileLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//for this, we'll just simply type the file locator info to the input field
//and assume will work for file upload with the browser
$this->type($fieldLocator,$fileLocator);
}
public function captureEntirePageScreenshot($filename,$kwargs=''){
print "\n".__FUNCTION__.": ".$filename." : ".$kwargs."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//we ignore the kwargs for Web Driver
$this->webdriver->getScreenshotAndSaveToFile($filename);
}
public function captureEntirePageScreenshotToString($kwargs=''){
print "\n".__FUNCTION__.": ".$kwargs."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//we ignore the kwargs for Web Driver
return $this->webdriver->getScreenshot();
}
public function captureScreenshot($filename){
print "\n".__FUNCTION__.": ".$filename."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->getScreenshotAndSaveToFile($filename);
}
public function captureScreenshotToString(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->getScreenshot();
}
public function check($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
if(!$this->isChecked($locator)){
$this->click($locator);
}
}
public function chooseCancelOnNextConfirmation(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->confirmationChoice = "cancel";
}
public function chooseOkOnNextConfirmation(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->confirmationChoice = "ok";
}
public function clickAt($locator,$coordString=''){
print "\n".__FUNCTION__.": ".$locator." : ".$coordString."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//ignore coordinates if supplied, just click...
$this->click($locator);
}
public function contextMenu($locator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function contextMenuAt($locator,$coordString){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function controlKeyDown(){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function controlKeyUp(){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function createCookie($nameValuePair,$optionsString=''){
print "\n".__FUNCTION__.": ".$nameValuePair." : ".$optionsString."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
list($name,$value) = explode("=",$nameValuePair);
//set default cookie options
$cookie_path='/';
$domain='';
$expiry='';
//set cookie options if given...
if($optionsString != ''){
$pathFound = preg_match('@path=(/.+/)@',$optionsString,$pathMatches);
if($pathFound) $cookie_path = $pathMatches[1];
$domainFound = preg_match('/domain=(.+)/',$optionsString,$domainMatches);
if($domainFound) $domain = $domainMatches[1];
$expiryFound = preg_match('/max_age=(\d+)/',$optionsString,$expiryMatches);
if($expiryFound) $expiry = $expiryMatches[1];
}
$this->webdriver->setCookie($name, $value, $cookie_path, $domain, $secure=false, $expiry);
}
public function deleteAllVisibleCookies(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->deleteAllCookies();
}
public function deleteCookie($name,$optionsString=''){
print "\n".__FUNCTION__.": ".$name." : ".$optionsString."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//WebDriver doesn't use the optionString from Selenium RC, so we ignore.
$this->webdriver->deleteCookie($name);
}
public function deselectPopUp(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->selectWindow();
}
public function doubleClick($locator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function doubleClickAt($locator,$coordString=''){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function dragAndDrop($locator,$movementsString){
//throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
$offsets = explode(',',$movementsString); //as "x,y" like "+70,-300"
$this->webdriver->moveTo($this->parseLocator($locator));
$this->webdriver->buttonDown(); //drag
$this->webdriver->moveTo($this->parseLocator($locator),$offsets[0],$offsets[1]);
$this->webdriver->buttonUp(); //drop
}
public function dragAndDropToObject($locatorOfObjectToBeDragged,$locatorOfDragDestinationObject){
//throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
$this->webdriver->moveTo($this->parseLocator($locatorOfObjectToBeDragged));
$this->webdriver->buttonDown(); //drag
$this->webdriver->moveTo($this->parseLocator($locatorOfDragDestinationObject));
$this->webdriver->buttonUp(); //drop
}
public function dragDrop($locator,$movementsString){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function fireEvent($locator,$eventName){
//Explicitly simulate an event, to trigger the corresponding "onevent" handler.
//eventName - the event name, e.g. "focus" or "blur"
//todo
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function focus($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
$element->click(); //these commands will force focus to element
$element->clear(); //and not change state of element
}
public function getAlert(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$alertText = $this->webdriver->getAlertText();
$this->webdriver->acceptAlert();
return $alertText;
}
public function getAllButtons(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$buttons1 = $this->webdriver->findElementsBy(LocatorStrategy::xpath,"//input[@type='button']");
$buttons2 = $this->webdriver->findElementsBy(LocatorStrategy::xpath,"//input[@type='image']");
$buttons3 = $this->webdriver->findElementsBy(LocatorStrategy::xpath,"//input[@type='submit']");
$buttons4 = $this->webdriver->findElementsBy(LocatorStrategy::xpath,"//input[@type='reset']");
$buttonIds = array();
foreach ($buttons1 as $button) {
$buttonIds[] = $button->getAttribute("id");
}
foreach ($buttons2 as $button) {
$buttonIds[] = $button->getAttribute("id");
}
foreach ($buttons3 as $button) {
$buttonIds[] = $button->getAttribute("id");
}
foreach ($buttons4 as $button) {
$buttonIds[] = $button->getAttribute("id");
}
return $buttonIds;
}
public function getAllFields(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$inputs = $this->webdriver->findElementsBy(LocatorStrategy::tagName,"input");
$inputIds = array();
foreach ($inputs as $input) {
$inputIds[] = $input->getAttribute("id");
}
return $inputIds;
}
public function getAllLinks(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$links = $this->webdriver->findElementsBy(LocatorStrategy::tagName,"a");
$linkIds = array();
foreach ($links as $link) {
$linkIds[] = $link->getAttribute("id");
}
return $linkIds;
}
public function getAllWindowIds(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//we will use the Web Driver window handle "IDs" as a substitute for Selenium RC window IDs
return $this->webdriver->getWindowHandles();
}
public function getAllWindowNames(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//not an ideal solution but a hack, some windows will have no "name" associated with it
$currHdl = $this->webdriver->getWindowHandle();
$hdls = $this->webdriver->getWindowHandles();
$names = array();
foreach ($hdls as $hdl) {
$this->webdriver->selectWindow($hdl); //switch to window
$names[] = $this->webdriver->execute("return window.name;",array()); //get its name
}
$this->webdriver->selectWindow($currHdl); //go back to current window
return $names;
}
public function getAllWindowTitles(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//not an ideal solution but a hack
//also refer to http://groups.google.com/group/webdriver/browse_thread/thread/ac88dcc6f4571bb0
$currHdl = $this->webdriver->getWindowHandle();
$hdls = $this->webdriver->getWindowHandles();
$titles = array();
foreach ($hdls as $hdl) {
$this->webdriver->selectWindow($hdl); //switch to window
$titles[] = $this->webdriver->getTitle(); //get its title
}
$this->webdriver->selectWindow($currHdl); //go back to current window
return $titles;
}
public function getAttributeFromAllWindows($attributeName){
print "\n".__FUNCTION__.": ".$attributeName."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//not an ideal solution but a hack
//I'm also interpreting attribute as a window attribute, not an element attribute
//for an element on page within window. Selenium API docs don't make this clear.
$currHdl = $this->webdriver->getWindowHandle();
$hdls = $this->webdriver->getWindowHandles();
$attributes = array();
foreach ($hdls as $hdl) {
$this->webdriver->selectWindow($hdl); //switch to window, then get window attribute
$attributes[] = $this->webdriver->execute("return window.".$attributeName.";",array());
}
$this->webdriver->selectWindow($currHdl); //go back to current window
return $attributes;
}
public function getConfirmation(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$confirmationText = $this->webdriver->getAlertText();
if($this->confirmationChoice == "cancel"){
$this->webdriver->dismissAlert();
}else{ //$this->confirmationChoice == "ok"
$this->webdriver->acceptAlert();
}
return $confirmationText;
}
public function getCookie(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->getAllCookies();
}
public function getCookieByName($name){
print "\n".__FUNCTION__.": ".$name."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$cookies = $this->webdriver->getAllCookies();
foreach ($cookies as $cookie) {
if($cookie->name == $name) return $cookie->value;
}
throw new Exception("\nCookie with name \"".$name."\" not found.\n");
}
public function getCursorPosition($locator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getElementHeight($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
return $element->getAttribute("clientHeight");
//or offsetHeight, scrollHeight, etc.
//or $element->getSize(); and parse out height
}
public function getElementIndex($locator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getElementPositionLeft($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
return $element->getAttribute("offsetLeft");
//or scrollLeft, etc.
//or $element->getLocation(); and parse out x,y coordinates
}
public function getElementPositionTop($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
return $element->getAttribute("offsetTop");
//or scrollTop, etc.
//or $element->getLocation(); and parse out x,y coordinates
}
public function getElementWidth($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
return $element->getAttribute("clientWidth");
//or offsetWidth, scrollWidth, etc.
//or $element->getSize(); and parse out width
}
public function getEval($script){
print "\n".__FUNCTION__.": ".$script."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->execute($script,array()); //execute script w/ no arguments
}
public function getExpression($expression){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getHtmlSource(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->getPageSource();
}
public function getLocation(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->getCurrentUrl();
}
public function getMouseSpeed(){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getPrompt(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$promptText = $this->webdriver->getAlertText();
$this->webdriver->sendAlertText($this->promptAnswer);
$this->webdriver->acceptAlert();
return $promptText;
}
public function getSelectedId($selectLocator){
print "\n".__FUNCTION__.": ".$selectLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($selectLocator);
$optionValue = $element->getValue();
$option = $element->findOptionElementByValue( $optionValue );
return $option->getAttribute("id");
}
public function getSelectedIds($selectLocator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getSelectedIndex($selectLocator){
print "\n".__FUNCTION__.": ".$selectLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($selectLocator);
$optionValue = $element->getValue();
$option = $element->findOptionElementByValue( $optionValue );
return $option->getAttribute("selectedIndex");
}
public function getSelectedIndexes($selectLocator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getSelectedLabel($selectLocator){
print "\n".__FUNCTION__.": ".$selectLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($selectLocator);
$optionValue = $element->getValue();
$option = $element->findOptionElementByValue( $optionValue );
return $option->getText();
}
public function getSelectedLabels($selectLocator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getSelectedValue($selectLocator){
print "\n".__FUNCTION__.": ".$selectLocator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($selectLocator);
$optionValue = $element->getValue();
$option = $element->findOptionElementByValue( $optionValue );
return $option->getValue();
}
public function getSelectOptions($selectLocator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getSpeed(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->seleniumSpeed;
}
public function getTable($tableCellAddress){
print "\n".__FUNCTION__.": ".$tableCellAddress."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
list($tableLocator,$rowNum,$colNum) = explode(".",$tableCellAddress);
$table = $this->parseLocator($tableLocator);
$row = $table->findElementBy("xpath","//tr[".$rowNum."]");
$col = $table->findElementBy("xpath","//td[".$colNum."]");
return $col->getText();
//if row & col #s are off due to diff browser support,
//or between Web Driver vs Selenium RC,
//can do findElementsBy("tag name","tr"); and then one for td
//then iterate over them until hit row # & col #
}
public function getText($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset( $element ) ) return $element->getText();
else return "";
}
public function getTitle(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->webdriver->getTitle();
}
public function getValue($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$element = $this->parseLocator($locator);
if( isset( $element ) ) return $element->getValue();
else return "";
}
public function getWhetherThisFrameMatchFrameExpression($currentFrameString,$target){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getWhetherThisWindowMatchWindowExpression($currentWindowString,$target){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function getXpathCount($xpath){
print "\n".__FUNCTION__.": ".$xpath."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return count($this->parseLocatorForElements($xpath));
}
public function goBack(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$this->webdriver->back();
}
public function highlight($locator){
throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
}
public function ignoreAttributesWithoutValue($ignore){
//throw new Exception("\nSelenium RC method \"".__FUNCTION__."\" not yet implemented in ".get_class($this).".\n");
//ignore, do nothing instead, as Web Driver should have native XPath support for browsers already
//and probably can't do much here? See this for reference...
//http://code.google.com/p/selenium/wiki/XpathInWebDriver
print "\nSelenium RC method \"".__FUNCTION__."\" not implemented. See http://code.google.com/p/selenium/wiki/XpathInWebDriver for reference.\n";
}
public function isAlertPresent(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
try{
$this->webdriver->getAlertText();
return true;
}catch(Exception $e){
return false; //NoAlertPresent error/exception - If there is no alert displayed.
}
}
public function isChecked($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->parseLocator($locator)->isSelected();
}
public function isConfirmationPresent(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
try{
$this->webdriver->getAlertText();
return true;
}catch(Exception $e){
return false; //NoAlertPresent error/exception - If there is no alert displayed.
}
}
public function isCookiePresent($name){
print "\n".__FUNCTION__.": ".$name."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
$cookies = $this->webdriver->getAllCookies();
foreach ($cookies as $cookie) {
if($cookie->name == $name) return true;
}
return false; //otherwise, not found
}
public function isEditable($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
return $this->parseLocator($locator)->isEnabled();
}
public function isElementPresent($locator){
print "\n".__FUNCTION__.": ".$locator."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
try{
$this->parseLocator($locator);
return true;
}catch(Exception $e){
//it has not been found
return false;
}
}
public function isOrdered($locator1, $locator2){
print "\n".__FUNCTION__.": ".$locator1." : ".$locator1."\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
//modeled after http://www.jarvana.com/jarvana/view/org/seleniumhq/selenium/selenium/2.0a7/selenium-2.0a7-sources.jar!/org/openqa/selenium/internal/seleniumemulation/IsOrdered.java?format=ok
$element1 = $this->parseLocator($locator1);
$element2 = $this->parseLocator($locator2);
$ordered = <<<ORDEREDSCRIPT
if(arguments[0] === arguments[1]) return false;
var previousSibling;
while((previousSibling = arguments[1].previousSibling) != null){
if(previousSibling === arguments[0]){
return true;
}
arguments[1] = previousSibling;
}
return false;
ORDEREDSCRIPT;
$result = $this->webdriver->executeScript($ordered, $element1, $element2);
return $result != null && $result;
}
public function isPromptPresent(){
print "\n".__FUNCTION__.":\n"; //log commands called like SeleniumTestCase
$this->seleniumSpeedDelay();
try{
$this->webdriver->getAlertText();
return true;
}catch(Exception $e){
return false; //NoAlertPresent error/exception - If there is no alert displayed.
}
}
public function isSomethingSelected($selectLocator){