]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/Extensions/SeleniumTestCase.php
Added unit tests.
[Github/sugarcrm.git] / tests / PHPUnit / Extensions / SeleniumTestCase.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
15  *   * Redistributions in binary form must reproduce the above copyright
16  *     notice, this list of conditions and the following disclaimer in
17  *     the documentation and/or other materials provided with the
18  *     distribution.
19  *
20  *   * Neither the name of Sebastian Bergmann nor the names of his
21  *     contributors may be used to endorse or promote products derived
22  *     from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * @category   Testing
38  * @package    PHPUnit
39  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
43  * @link       http://www.phpunit.de/
44  * @since      File available since Release 3.0.0
45  */
46
47 require_once 'PHPUnit/Framework.php';
48 require_once 'PHPUnit/Util/Log/Database.php';
49 require_once 'PHPUnit/Util/Filter.php';
50 require_once 'PHPUnit/Util/Test.php';
51 require_once 'PHPUnit/Util/XML.php';
52 require_once 'PHPUnit/Extensions/SeleniumTestCase/Driver.php';
53
54 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
55
56 /**
57  * TestCase class that uses Selenium to provide
58  * the functionality required for web testing.
59  *
60  * @category   Testing
61  * @package    PHPUnit
62  * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
63  * @copyright  2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
64  * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
65  * @version    Release: 3.3.17
66  * @link       http://www.phpunit.de/
67  * @since      Class available since Release 3.0.0
68  */
69 abstract class PHPUnit_Extensions_SeleniumTestCase extends PHPUnit_Framework_TestCase
70 {
71     /**
72      * @var    array
73      */
74     public static $browsers = array();
75
76     /**
77      * @var    boolean
78      */
79     protected $autoStop = TRUE;
80
81     /**
82      * @var    string
83      */
84     protected $browserName;
85
86     /**
87      * @var    boolean
88      */
89     protected $collectCodeCoverageInformation = FALSE;
90
91     /**
92      * @var    string
93      */
94     protected $coverageScriptUrl = '';
95
96     /**
97      * @var    PHPUnit_Extensions_SeleniumTestCase_Driver[]
98      */
99     protected $drivers = array();
100
101     /**
102      * @var    boolean
103      */
104     protected $inDefaultAssertions = FALSE;
105
106     /**
107      * @var    string
108      */
109     protected $testId;
110
111     /**
112      * @var    array
113      * @access protected
114      */
115     protected $verificationErrors = array();
116
117     /**
118      * @param  string $name
119      * @param  array  $data
120      * @param  string $dataName
121      * @param  array  $browser
122      * @throws InvalidArgumentException
123      */
124     public function __construct($name = NULL, array $data = array(), $dataName = '', array $browser = array())
125     {
126         parent::__construct($name, $data, $dataName);
127         $this->testId = md5(uniqid(rand(), TRUE));
128         $this->getDriver($browser);
129     }
130
131     /**
132      * @param  string $className
133      * @return PHPUnit_Framework_TestSuite
134      */
135     public static function suite($className)
136     {
137         $suite = new PHPUnit_Framework_TestSuite;
138         $suite->setName($className);
139
140         $class            = new ReflectionClass($className);
141         $classGroups      = PHPUnit_Util_Test::getGroups($class->getDocComment());
142         $staticProperties = $class->getStaticProperties();
143
144         // Create tests from Selenese/HTML files.
145         if (isset($staticProperties['seleneseDirectory']) &&
146             is_dir($staticProperties['seleneseDirectory'])) {
147             $files = array_merge(
148               self::getSeleneseFiles($staticProperties['seleneseDirectory'], '.htm'),
149               self::getSeleneseFiles($staticProperties['seleneseDirectory'], '.html')
150             );
151
152             // Create tests from Selenese/HTML files for multiple browsers.
153             if (!empty($staticProperties['browsers'])) {
154                 foreach ($staticProperties['browsers'] as $browser) {
155                     $browserSuite = new PHPUnit_Framework_TestSuite;
156                     $browserSuite->setName($className . ': ' . $browser['name']);
157
158                     foreach ($files as $file) {
159                         $browserSuite->addTest(
160                           new $className($file, array(), '', $browser),
161                           $classGroups
162                         );
163                     }
164
165                     $suite->addTest($browserSuite);
166                 }
167             }
168
169             // Create tests from Selenese/HTML files for single browser.
170             else {
171                 foreach ($files as $file) {
172                     $suite->addTest(new $className($file), $classGroups);
173                 }
174             }
175         }
176
177         // Create tests from test methods for multiple browsers.
178         if (!empty($staticProperties['browsers'])) {
179             foreach ($staticProperties['browsers'] as $browser) {
180                 $browserSuite = new PHPUnit_Framework_TestSuite;
181                 $browserSuite->setName($className . ': ' . $browser['name']);
182
183                 foreach ($class->getMethods() as $method) {
184                     if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
185                         $name             = $method->getName();
186                         $methodDocComment = $method->getDocComment();
187                         $data             = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
188                         $groups           = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
189
190                         // Test method with @dataProvider.
191                         if (is_array($data) || $data instanceof Iterator) {
192                             $dataSuite = new PHPUnit_Framework_TestSuite(
193                               $className . '::' . $name
194                             );
195
196                             foreach ($data as $_dataName => $_data) {
197                                 $dataSuite->addTest(
198                                   new $className($name, $_data, $_dataName, $browser),
199                                   $groups
200                                 );
201                             }
202
203                             $browserSuite->addTest($dataSuite);
204                         }
205
206                         // Test method without @dataProvider.
207                         else {
208                             $browserSuite->addTest(
209                               new $className($name, array(), '', $browser), $groups
210                             );
211                         }
212                     }
213                 }
214
215                 $suite->addTest($browserSuite);
216             }
217         }
218
219         // Create tests from test methods for single browser.
220         else {
221             foreach ($class->getMethods() as $method) {
222                 if (PHPUnit_Framework_TestSuite::isPublicTestMethod($method)) {
223                     $name             = $method->getName();
224                     $methodDocComment = $method->getDocComment();
225                     $data             = PHPUnit_Util_Test::getProvidedData($className, $name, $methodDocComment);
226                     $groups           = PHPUnit_Util_Test::getGroups($methodDocComment, $classGroups);
227
228                     // Test method with @dataProvider.
229                     if (is_array($data) || $data instanceof Iterator) {
230                         $dataSuite = new PHPUnit_Framework_TestSuite(
231                           $className . '::' . $name
232                         );
233
234                         foreach ($data as $_dataName => $_data) {
235                             $dataSuite->addTest(
236                               new $className($name, $_data, $_dataName),
237                               $groups
238                             );
239                         }
240
241                         $suite->addTest($dataSuite);
242                     }
243
244                     // Test method without @dataProvider.
245                     else {
246                         $suite->addTest(
247                           new $className($name), $groups
248                         );
249                     }
250                 }
251             }
252         }
253
254         return $suite;
255     }
256
257     /**
258      * Runs the test case and collects the results in a TestResult object.
259      * If no TestResult object is passed a new one will be created.
260      *
261      * @param  PHPUnit_Framework_TestResult $result
262      * @return PHPUnit_Framework_TestResult
263      * @throws InvalidArgumentException
264      */
265     public function run(PHPUnit_Framework_TestResult $result = NULL)
266     {
267         if ($result === NULL) {
268             $result = $this->createResult();
269         }
270
271         $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
272
273         foreach ($this->drivers as $driver) {
274             $driver->setCollectCodeCoverageInformation(
275               $this->collectCodeCoverageInformation
276             );
277         }
278
279         $result->run($this);
280
281         if ($this->collectCodeCoverageInformation) {
282             $result->appendCodeCoverageInformation(
283               $this, $this->getCodeCoverage()
284             );
285         }
286
287         return $result;
288     }
289
290     /**
291      * @param  array $browser
292      * @return PHPUnit_Extensions_SeleniumTestCase_Driver
293      * @since  Method available since Release 3.3.0
294      */
295     protected function getDriver(array $browser)
296     {
297         if (isset($browser['name'])) {
298             if (!is_string($browser['name'])) {
299                 throw new InvalidArgumentException;
300             }
301         } else {
302             $browser['name'] = '';
303         }
304
305         if (isset($browser['browser'])) {
306             if (!is_string($browser['browser'])) {
307                 throw new InvalidArgumentException;
308             }
309         } else {
310             $browser['browser'] = '';
311         }
312
313         if (isset($browser['host'])) {
314             if (!is_string($browser['host'])) {
315                 throw new InvalidArgumentException;
316             }
317         } else {
318             $browser['host'] = 'localhost';
319         }
320
321         if (isset($browser['port'])) {
322             if (!is_int($browser['port'])) {
323                 throw new InvalidArgumentException;
324             }
325         } else {
326             $browser['port'] = 4444;
327         }
328
329         if (isset($browser['timeout'])) {
330             if (!is_int($browser['timeout'])) {
331                 throw new InvalidArgumentException;
332             }
333         } else {
334             $browser['timeout'] = 30000;
335         }
336
337         $driver = new PHPUnit_Extensions_SeleniumTestCase_Driver;
338         $driver->setName($browser['name']);
339         $driver->setBrowser($browser['browser']);
340         $driver->setHost($browser['host']);
341         $driver->setPort($browser['port']);
342         $driver->setTimeout($browser['timeout']);
343         $driver->setTestCase($this);
344         $driver->setTestId($this->testId);
345
346         $this->drivers[] = $driver;
347
348         return $driver;
349     }
350
351     /**
352      */
353     protected function runTest()
354     {
355         $this->start();
356
357         if (!is_file($this->name)) {
358             parent::runTest();
359         } else {
360             $this->runSelenese($this->name);
361         }
362
363         if ($this->autoStop) {
364             try {
365                 $this->stop();
366             }
367
368             catch (RuntimeException $e) {
369             }
370         }
371
372         if (!empty($this->verificationErrors)) {
373             $this->fail(implode("\n", $this->verificationErrors));
374         }
375     }
376
377     /**
378      * If you want to override tearDown() make sure to either call stop() or
379      * parent::tearDown(). Otherwise the Selenium RC session will not be
380      * closed upon test failure.
381      *
382      */
383     protected function tearDown()
384     {
385         if ($this->autoStop) {
386             try {
387                 $this->stop();
388             }
389
390             catch (RuntimeException $e) {
391             }
392         }
393     }
394
395     /**
396      * Returns a string representation of the test case.
397      *
398      * @return string
399      */
400     public function toString()
401     {
402         $buffer = parent::toString();
403
404         if (!empty($this->browserName)) {
405             $buffer .= ' with browser ' . $this->browserName;
406         }
407
408         return $buffer;
409     }
410
411     /**
412      * @param  boolean $autoStop
413      * @throws InvalidArgumentException
414      */
415     public function setAutoStop($autoStop)
416     {
417         if (!is_bool($autoStop)) {
418             throw new InvalidArgumentException;
419         }
420
421         $this->autoStop = $autoStop;
422     }
423
424     /**
425      * Runs a test from a Selenese (HTML) specification.
426      *
427      * @param string $filename
428      */
429     public function runSelenese($filename)
430     {
431         $document = PHPUnit_Util_XML::loadFile($filename, TRUE);
432         $xpath    = new DOMXPath($document);
433         $rows     = $xpath->query('body/table/tbody/tr');
434
435         foreach ($rows as $row)
436         {
437             $action    = NULL;
438             $arguments = array();
439             $columns   = $xpath->query('td', $row);
440
441             foreach ($columns as $column)
442             {
443                 if ($action === NULL) {
444                     $action = $column->nodeValue;
445                 } else {
446                     $arguments[] = $column->nodeValue;
447                 }
448             }
449
450             if (method_exists($this, $action)) {
451                 call_user_func_array(array($this, $action), $arguments);
452             } else {
453                 $this->__call($action, $arguments);
454             }
455         }
456     }
457
458     /**
459      * Delegate method calls to the driver.
460      *
461      * @param  string $command
462      * @param  array  $arguments
463      * @return mixed
464      * @method unknown  addLocationStrategy()
465      * @method unknown  addSelection()
466      * @method unknown  addSelectionAndWait()
467      * @method unknown  allowNativeXpath()
468      * @method unknown  altKeyDown()
469      * @method unknown  altKeyDownAndWait()
470      * @method unknown  altKeyUp()
471      * @method unknown  altKeyUpAndWait()
472      * @method unknown  answerOnNextPrompt()
473      * @method unknown  assignId()
474      * @method unknown  break()
475      * @method unknown  captureEntirePageScreenshot()
476      * @method unknown  captureScreenshot()
477      * @method unknown  check()
478      * @method unknown  chooseCancelOnNextConfirmation()
479      * @method unknown  chooseOkOnNextConfirmation()
480      * @method unknown  click()
481      * @method unknown  clickAndWait()
482      * @method unknown  clickAt()
483      * @method unknown  clickAtAndWait()
484      * @method unknown  close()
485      * @method unknown  contextMenu()
486      * @method unknown  contextMenuAndWait()
487      * @method unknown  contextMenuAt()
488      * @method unknown  contextMenuAtAndWait()
489      * @method unknown  controlKeyDown()
490      * @method unknown  controlKeyDownAndWait()
491      * @method unknown  controlKeyUp()
492      * @method unknown  controlKeyUpAndWait()
493      * @method unknown  createCookie()
494      * @method unknown  createCookieAndWait()
495      * @method unknown  deleteAllVisibleCookies()
496      * @method unknown  deleteAllVisibleCookiesAndWait()
497      * @method unknown  deleteCookie()
498      * @method unknown  deleteCookieAndWait()
499      * @method unknown  doubleClick()
500      * @method unknown  doubleClickAndWait()
501      * @method unknown  doubleClickAt()
502      * @method unknown  doubleClickAtAndWait()
503      * @method unknown  dragAndDrop()
504      * @method unknown  dragAndDropAndWait()
505      * @method unknown  dragAndDropToObject()
506      * @method unknown  dragAndDropToObjectAndWait()
507      * @method unknown  dragDrop()
508      * @method unknown  dragDropAndWait()
509      * @method unknown  echo()
510      * @method unknown  fireEvent()
511      * @method unknown  fireEventAndWait()
512      * @method unknown  focus()
513      * @method string   getAlert()
514      * @method array    getAllButtons()
515      * @method array    getAllFields()
516      * @method array    getAllLinks()
517      * @method array    getAllWindowIds()
518      * @method array    getAllWindowNames()
519      * @method array    getAllWindowTitles()
520      * @method string   getAttribute()
521      * @method array    getAttributeFromAllWindows()
522      * @method string   getBodyText()
523      * @method string   getConfirmation()
524      * @method string   getCookie()
525      * @method integer  getCursorPosition()
526      * @method integer  getElementHeight()
527      * @method integer  getElementIndex()
528      * @method integer  getElementPositionLeft()
529      * @method integer  getElementPositionTop()
530      * @method integer  getElementWidth()
531      * @method string   getEval()
532      * @method string   getExpression()
533      * @method string   getHtmlSource()
534      * @method string   getLocation()
535      * @method string   getLogMessages()
536      * @method integer  getMouseSpeed()
537      * @method string   getPrompt()
538      * @method array    getSelectOptions()
539      * @method string   getSelectedId()
540      * @method array    getSelectedIds()
541      * @method string   getSelectedIndex()
542      * @method array    getSelectedIndexes()
543      * @method string   getSelectedLabel()
544      * @method array    getSelectedLabels()
545      * @method string   getSelectedValue()
546      * @method array    getSelectedValues()
547      * @method unknown  getSpeed()
548      * @method unknown  getSpeedAndWait()
549      * @method string   getTable()
550      * @method string   getText()
551      * @method string   getTitle()
552      * @method string   getValue()
553      * @method boolean  getWhetherThisFrameMatchFrameExpression()
554      * @method boolean  getWhetherThisWindowMatchWindowExpression()
555      * @method integer  getXpathCount()
556      * @method unknown  goBack()
557      * @method unknown  goBackAndWait()
558      * @method unknown  highlight()
559      * @method unknown  highlightAndWait()
560      * @method unknown  ignoreAttributesWithoutValue()
561      * @method boolean  isAlertPresent()
562      * @method boolean  isChecked()
563      * @method boolean  isConfirmationPresent()
564      * @method boolean  isEditable()
565      * @method boolean  isElementPresent()
566      * @method boolean  isOrdered()
567      * @method boolean  isPromptPresent()
568      * @method boolean  isSomethingSelected()
569      * @method boolean  isTextPresent()
570      * @method boolean  isVisible()
571      * @method unknown  keyDown()
572      * @method unknown  keyDownAndWait()
573      * @method unknown  keyPress()
574      * @method unknown  keyPressAndWait()
575      * @method unknown  keyUp()
576      * @method unknown  keyUpAndWait()
577      * @method unknown  metaKeyDown()
578      * @method unknown  metaKeyDownAndWait()
579      * @method unknown  metaKeyUp()
580      * @method unknown  metaKeyUpAndWait()
581      * @method unknown  mouseDown()
582      * @method unknown  mouseDownAndWait()
583      * @method unknown  mouseDownAt()
584      * @method unknown  mouseDownAtAndWait()
585      * @method unknown  mouseMove()
586      * @method unknown  mouseMoveAndWait()
587      * @method unknown  mouseMoveAt()
588      * @method unknown  mouseMoveAtAndWait()
589      * @method unknown  mouseOut()
590      * @method unknown  mouseOutAndWait()
591      * @method unknown  mouseOver()
592      * @method unknown  mouseOverAndWait()
593      * @method unknown  mouseUp()
594      * @method unknown  mouseUpAndWait()
595      * @method unknown  mouseUpAt()
596      * @method unknown  mouseUpAtAndWait()
597      * @method unknown  mouseUpRight()
598      * @method unknown  mouseUpRightAndWait()
599      * @method unknown  mouseUpRightAt()
600      * @method unknown  mouseUpRightAtAndWait()
601      * @method unknown  open()
602      * @method unknown  openWindow()
603      * @method unknown  openWindowAndWait()
604      * @method unknown  pause()
605      * @method unknown  refresh()
606      * @method unknown  refreshAndWait()
607      * @method unknown  removeAllSelections()
608      * @method unknown  removeAllSelectionsAndWait()
609      * @method unknown  removeSelection()
610      * @method unknown  removeSelectionAndWait()
611      * @method unknown  runScript()
612      * @method unknown  select()
613      * @method unknown  selectAndWait()
614      * @method unknown  selectFrame()
615      * @method unknown  selectWindow()
616      * @method unknown  setBrowserLogLevel()
617      * @method unknown  setContext()
618      * @method unknown  setCursorPosition()
619      * @method unknown  setCursorPositionAndWait()
620      * @method unknown  setMouseSpeed()
621      * @method unknown  setMouseSpeedAndWait()
622      * @method unknown  setSpeed()
623      * @method unknown  setSpeedAndWait()
624      * @method unknown  shiftKeyDown()
625      * @method unknown  shiftKeyDownAndWait()
626      * @method unknown  shiftKeyUp()
627      * @method unknown  shiftKeyUpAndWait()
628      * @method unknown  store()
629      * @method unknown  storeAlert()
630      * @method unknown  storeAlertPresent()
631      * @method unknown  storeAllButtons()
632      * @method unknown  storeAllFields()
633      * @method unknown  storeAllLinks()
634      * @method unknown  storeAllWindowIds()
635      * @method unknown  storeAllWindowNames()
636      * @method unknown  storeAllWindowTitle()s
637      * @method unknown  storeAttribute()
638      * @method unknown  storeAttributeFromAllWindows()
639      * @method unknown  storeBodyText()
640      * @method unknown  storeChecked()
641      * @method unknown  storeConfirmation()
642      * @method unknown  storeConfirmationPresent()
643      * @method unknown  storeCookie()
644      * @method unknown  storeCookieByName()
645      * @method unknown  storeCookiePresent()
646      * @method unknown  storeCursorPosition()
647      * @method unknown  storeEditable()
648      * @method unknown  storeElementHeight()
649      * @method unknown  storeElementIndex()
650      * @method unknown  storeElementPositionLeft()
651      * @method unknown  storeElementPositionTop()
652      * @method unknown  storeElementPresent()
653      * @method unknown  storeElementWidth()
654      * @method unknown  storeEval()
655      * @method unknown  storeExpression()
656      * @method unknown  storeHtmlSource()
657      * @method unknown  storeLocation()
658      * @method unknown  storeMouseSpeed()
659      * @method unknown  storeOrdered()
660      * @method unknown  storePrompt()
661      * @method unknown  storePromptPresent()
662      * @method unknown  storeSelectOptions()
663      * @method unknown  storeSelectedId()
664      * @method unknown  storeSelectedIds()
665      * @method unknown  storeSelectedIndex()
666      * @method unknown  storeSelectedIndexes()
667      * @method unknown  storeSelectedLabel()
668      * @method unknown  storeSelectedLabels()
669      * @method unknown  storeSelectedValue()
670      * @method unknown  storeSelectedValues()
671      * @method unknown  storeSomethingSelected()
672      * @method unknown  storeSpeed()
673      * @method unknown  storeTable()
674      * @method unknown  storeText()
675      * @method unknown  storeTextPresent()
676      * @method unknown  storeTitle()
677      * @method unknown  storeValue()
678      * @method unknown  storeVisible()
679      * @method unknown  storeWhetherThisFrameMatchFrameExpression()
680      * @method unknown  storeWhetherThisWindowMatchWindowExpression()
681      * @method unknown  storeXpathCount()
682      * @method unknown  submit()
683      * @method unknown  submitAndWait()
684      * @method unknown  type()
685      * @method unknown  typeAndWait()
686      * @method unknown  typeKeys()
687      * @method unknown  typeKeysAndWait()
688      * @method unknown  uncheck()
689      * @method unknown  uncheckAndWait()
690      * @method unknown  waitForCondition()
691      * @method unknown  waitForPageToLoad()
692      * @method unknown  waitForPopUp()
693      * @method unknown  windowFocus()
694      * @method unknown  windowMaximize()
695      */
696     public function __call($command, $arguments)
697     {
698         return call_user_func_array(
699           array($this->drivers[0], $command), $arguments
700         );
701     }
702
703     /**
704      * Asserts that an alert is present.
705      *
706      * @param  string $message
707      */
708     public function assertAlertPresent($message = 'No alert present.')
709     {
710         $this->assertTrue($this->isAlertPresent(), $message);
711     }
712
713     /**
714      * Asserts that no alert is present.
715      *
716      * @param  string $message
717      */
718     public function assertNoAlertPresent($message = 'Alert present.')
719     {
720         $this->assertFalse($this->isAlertPresent(), $message);
721     }
722
723     /**
724      * Asserts that an option is checked.
725      *
726      * @param  string $locator
727      * @param  string $message
728      */
729     public function assertChecked($locator, $message = '')
730     {
731         if ($message == '') {
732             $message = sprintf(
733               '"%s" not checked.',
734               $locator
735             );
736         }
737
738         $this->assertTrue($this->isChecked($locator), $message);
739     }
740
741     /**
742      * Asserts that an option is not checked.
743      *
744      * @param  string $locator
745      * @param  string $message
746      */
747     public function assertNotChecked($locator, $message = '')
748     {
749         if ($message == '') {
750             $message = sprintf(
751               '"%s" checked.',
752               $locator
753             );
754         }
755
756         $this->assertFalse($this->isChecked($locator), $message);
757     }
758
759     /**
760      * Assert that a confirmation is present.
761      *
762      * @param  string $message
763      */
764     public function assertConfirmationPresent($message = 'No confirmation present.')
765     {
766         $this->assertTrue($this->isConfirmationPresent(), $message);
767     }
768
769     /**
770      * Assert that no confirmation is present.
771      *
772      * @param  string $message
773      */
774     public function assertNoConfirmationPresent($message = 'Confirmation present.')
775     {
776         $this->assertFalse($this->isConfirmationPresent(), $message);
777     }
778
779     /**
780      * Asserts that an input field is editable.
781      *
782      * @param  string $locator
783      * @param  string $message
784      */
785     public function assertEditable($locator, $message = '')
786     {
787         if ($message == '') {
788             $message = sprintf(
789               '"%s" not editable.',
790               $locator
791             );
792         }
793
794         $this->assertTrue($this->isEditable($locator), $message);
795     }
796
797     /**
798      * Asserts that an input field is not editable.
799      *
800      * @param  string $locator
801      * @param  string $message
802      */
803     public function assertNotEditable($locator, $message = '')
804     {
805         if ($message == '') {
806             $message = sprintf(
807               '"%s" editable.',
808               $locator
809             );
810         }
811
812         $this->assertFalse($this->isEditable($locator), $message);
813     }
814
815     /**
816      * Asserts that an element's value is equal to a given string.
817      *
818      * @param  string $locator
819      * @param  string $text
820      * @param  string $message
821      */
822     public function assertElementValueEquals($locator, $text, $message = '')
823     {
824         $this->assertEquals($text, $this->getValue($locator), $message);
825     }
826
827     /**
828      * Asserts that an element's value is not equal to a given string.
829      *
830      * @param  string $locator
831      * @param  string $text
832      * @param  string $message
833      */
834     public function assertElementValueNotEquals($locator, $text, $message = '')
835     {
836         $this->assertNotEquals($text, $this->getValue($locator), $message);
837     }
838
839     /**
840      * Asserts that an element contains a given string.
841      *
842      * @param  string $locator
843      * @param  string $text
844      * @param  string $message
845      */
846     public function assertElementContainsText($locator, $text, $message = '')
847     {
848         $this->assertContains($text, $this->getValue($locator), $message);
849     }
850
851     /**
852      * Asserts that an element does not contain a given string.
853      *
854      * @param  string $locator
855      * @param  string $text
856      * @param  string $message
857      */
858     public function assertElementNotContainsText($locator, $text, $message = '')
859     {
860         $this->assertNotContains($text, $this->getValue($locator), $message);
861     }
862
863     /**
864      * Asserts than an element is present.
865      *
866      * @param  string $locator
867      * @param  string $message
868      */
869     public function assertElementPresent($locator, $message = '')
870     {
871         if ($message == '') {
872             $message = sprintf(
873               'Element "%s" not present.',
874               $locator
875             );
876         }
877
878         $this->assertTrue($this->isElementPresent($locator), $message);
879     }
880
881     /**
882      * Asserts than an element is not present.
883      *
884      * @param  string $locator
885      * @param  string $message
886      */
887     public function assertElementNotPresent($locator, $message = '')
888     {
889         if ($message == '') {
890             $message = sprintf(
891               'Element "%s" present.',
892               $locator
893             );
894         }
895
896         $this->assertFalse($this->isElementPresent($locator), $message);
897     }
898
899     /**
900      * Asserts that the location is equal to a specified one.
901      *
902      * @param  string $location
903      * @param  string $message
904      */
905     public function assertLocationEquals($location, $message = '')
906     {
907         $this->assertEquals($location, $this->getLocation(), $message);
908     }
909
910     /**
911      * Asserts that the location is not equal to a specified one.
912      *
913      * @param  string $location
914      * @param  string $message
915      */
916     public function assertLocationNotEquals($location, $message = '')
917     {
918         $this->assertNotEquals($location, $this->getLocation(), $message);
919     }
920
921     /**
922      * Asserts than a prompt is present.
923      *
924      * @param  string $message
925      */
926     public function assertPromptPresent($message = 'No prompt present.')
927     {
928         $this->assertTrue($this->isPromptPresent(), $message);
929     }
930
931     /**
932      * Asserts than no prompt is present.
933      *
934      * @param  string $message
935      */
936     public function assertNoPromptPresent($message = 'Prompt present.')
937     {
938         $this->assertFalse($this->isPromptPresent(), $message);
939     }
940
941     /**
942      * Asserts that a select element has a specific option.
943      *
944      * @param  string $selectLocator
945      * @param  string $option
946      * @param  string $message
947      * @since  Method available since Release 3.2.0
948      */
949     public function assertSelectHasOption($selectLocator, $option, $message = '')
950     {
951         $this->assertContains($option, $this->getSelectOptions($selectLocator), $message);
952     }
953
954     /**
955      * Asserts that a select element does not have a specific option.
956      *
957      * @param  string $selectLocator
958      * @param  string $option
959      * @param  string $message
960      * @since  Method available since Release 3.2.0
961      */
962     public function assertSelectNotHasOption($selectLocator, $option, $message = '')
963     {
964         $this->assertNotContains($option, $this->getSelectOptions($selectLocator), $message);
965     }
966
967     /**
968      * Asserts that a specific label is selected.
969      *
970      * @param  string $selectLocator
971      * @param  string $value
972      * @param  string $message
973      * @since  Method available since Release 3.2.0
974      */
975     public function assertSelected($selectLocator, $option, $message = '')
976     {
977         if ($message == '') {
978             $message = sprintf(
979               'Label "%s" not selected in "%s".',
980               $option,
981               $selectLocator
982             );
983         }
984
985         $this->assertEquals(
986           $option,
987           $this->getSelectedLabel($selectLocator),
988           $message
989         );
990     }
991
992     /**
993      * Asserts that a specific label is not selected.
994      *
995      * @param  string $selectLocator
996      * @param  string $value
997      * @param  string $message
998      * @since  Method available since Release 3.2.0
999      */
1000     public function assertNotSelected($selectLocator, $option, $message = '')
1001     {
1002         if ($message == '') {
1003             $message = sprintf(
1004               'Label "%s" selected in "%s".',
1005               $option,
1006               $selectLocator
1007             );
1008         }
1009
1010         $this->assertNotEquals(
1011           $option,
1012           $this->getSelectedLabel($selectLocator),
1013           $message
1014         );
1015     }
1016
1017     /**
1018      * Asserts that a specific value is selected.
1019      *
1020      * @param  string $selectLocator
1021      * @param  string $value
1022      * @param  string $message
1023      */
1024     public function assertIsSelected($selectLocator, $value, $message = '')
1025     {
1026         if ($message == '') {
1027             $message = sprintf(
1028               'Value "%s" not selected in "%s".',
1029               $value,
1030               $selectLocator
1031             );
1032         }
1033
1034         $this->assertEquals(
1035           $value, $this->getSelectedValue($selectLocator),
1036           $message
1037         );
1038     }
1039
1040     /**
1041      * Asserts that a specific value is not selected.
1042      *
1043      * @param  string $selectLocator
1044      * @param  string $value
1045      * @param  string $message
1046      */
1047     public function assertIsNotSelected($selectLocator, $value, $message = '')
1048     {
1049         if ($message == '') {
1050             $message = sprintf(
1051               'Value "%s" selected in "%s".',
1052               $value,
1053               $selectLocator
1054             );
1055         }
1056
1057         $this->assertNotEquals(
1058           $value,
1059           $this->getSelectedValue($selectLocator),
1060           $message
1061         );
1062     }
1063
1064     /**
1065      * Asserts that something is selected.
1066      *
1067      * @param  string $selectLocator
1068      * @param  string $message
1069      */
1070     public function assertSomethingSelected($selectLocator, $message = '')
1071     {
1072         if ($message == '') {
1073             $message = sprintf(
1074               'Nothing selected from "%s".',
1075               $selectLocator
1076             );
1077         }
1078
1079         $this->assertTrue($this->isSomethingSelected($selectLocator), $message);
1080     }
1081
1082     /**
1083      * Asserts that nothing is selected.
1084      *
1085      * @param  string $selectLocator
1086      * @param  string $message
1087      */
1088     public function assertNothingSelected($selectLocator, $message = '')
1089     {
1090         if ($message == '') {
1091             $message = sprintf(
1092               'Something selected from "%s".',
1093               $selectLocator
1094             );
1095         }
1096
1097         $this->assertFalse($this->isSomethingSelected($selectLocator), $message);
1098     }
1099
1100     /**
1101      * Asserts that a given text is present.
1102      *
1103      * @param  string $pattern
1104      * @param  string $message
1105      */
1106     public function assertTextPresent($pattern, $message = '')
1107     {
1108         if ($message == '') {
1109             $message = sprintf(
1110               '"%s" not present.',
1111               $pattern
1112             );
1113         }
1114
1115         $this->assertTrue($this->isTextPresent($pattern), $message);
1116     }
1117
1118     /**
1119      * Asserts that a given text is not present.
1120      *
1121      * @param  string $pattern
1122      * @param  string $message
1123      */
1124     public function assertTextNotPresent($pattern, $message = '')
1125     {
1126         if ($message == '') {
1127             $message = sprintf(
1128               '"%s" present.',
1129               $pattern
1130             );
1131         }
1132
1133         $this->assertFalse($this->isTextPresent($pattern), $message);
1134     }
1135
1136     /**
1137      * Asserts that the title is equal to a given string.
1138      *
1139      * @param  string $title
1140      * @param  string $message
1141      */
1142     public function assertTitleEquals($title, $message = '')
1143     {
1144         $this->assertEquals($title, $this->getTitle(), $message);
1145     }
1146
1147     /**
1148      * Asserts that the title is not equal to a given string.
1149      *
1150      * @param  string $title
1151      * @param  string $message
1152      */
1153     public function assertTitleNotEquals($title, $message = '')
1154     {
1155         $this->assertNotEquals($title, $this->getTitle(), $message);
1156     }
1157
1158     /**
1159      * Asserts that something is visible.
1160      *
1161      * @param  string $locator
1162      * @param  string $message
1163      */
1164     public function assertVisible($locator, $message = '')
1165     {
1166         if ($message == '') {
1167             $message = sprintf(
1168               '"%s" not visible.',
1169               $locator
1170             );
1171         }
1172
1173         $this->assertTrue($this->isVisible($locator), $message);
1174     }
1175
1176     /**
1177      * Asserts that something is not visible.
1178      *
1179      * @param  string $locator
1180      * @param  string $message
1181      */
1182     public function assertNotVisible($locator, $message = '')
1183     {
1184         if ($message == '') {
1185             $message = sprintf(
1186               '"%s" visible.',
1187               $locator
1188             );
1189         }
1190
1191         $this->assertFalse($this->isVisible($locator), $message);
1192     }
1193
1194     /**
1195      * Template Method that is called after Selenium actions.
1196      *
1197      * @param  string $action
1198      * @since  Method available since Release 3.1.0
1199      */
1200     protected function defaultAssertions($action)
1201     {
1202     }
1203
1204     /**
1205      * @return array
1206      * @since  Method available since Release 3.2.0
1207      */
1208     protected function getCodeCoverage()
1209     {
1210         if (!empty($this->coverageScriptUrl)) {
1211             $url = sprintf(
1212               '%s?PHPUNIT_SELENIUM_TEST_ID=%s',
1213               $this->coverageScriptUrl,
1214               $this->testId
1215             );
1216
1217             $buffer = @file_get_contents($url);
1218
1219             if ($buffer !== FALSE) {
1220                 return $this->matchLocalAndRemotePaths(unserialize($buffer));
1221             }
1222         }
1223
1224         return array();
1225     }
1226
1227     /**
1228      * @param  array $coverage
1229      * @return array
1230      * @author Mattis Stordalen Flister <mattis@xait.no>
1231      * @since  Method available since Release 3.2.9
1232      */
1233     protected function matchLocalAndRemotePaths(array $coverage)
1234     {
1235         $coverageWithLocalPaths = array();
1236
1237         foreach ($coverage as $originalRemotePath => $data) {
1238             $remotePath = $originalRemotePath;
1239             $separator  = $this->findDirectorySeparator($remotePath);
1240
1241             while (!($localpath = PHPUnit_Util_Filesystem::fileExistsInIncludePath($remotePath)) &&
1242                    strpos($remotePath, $separator) !== FALSE) {
1243                 $remotePath = substr($remotePath, strpos($remotePath, $separator) + 1);
1244             }
1245
1246             if ($localpath && md5_file($localpath) == $data['md5']) {
1247                 $coverageWithLocalPaths[$localpath] = $data['coverage'];
1248             }
1249         }
1250
1251         return $coverageWithLocalPaths;
1252     }
1253
1254     /**
1255      * @param  string $path
1256      * @return string
1257      * @author Mattis Stordalen Flister <mattis@xait.no>
1258      * @since  Method available since Release 3.2.9
1259      */
1260     protected function findDirectorySeparator($path)
1261     {
1262         if (strpos($path, '/') !== FALSE) {
1263             return '/';
1264         }
1265
1266         return '\\';
1267     }
1268
1269     /**
1270      * @param  string $path
1271      * @return array
1272      * @author Mattis Stordalen Flister <mattis@xait.no>
1273      * @since  Method available since Release 3.2.9
1274      */
1275     protected function explodeDirectories($path)
1276     {
1277         return explode($this->findDirectorySeparator($path), dirname($path));
1278     }
1279
1280     /**
1281      * @param  string $directory
1282      * @param  string $suffix
1283      * @return array
1284      * @since  Method available since Release 3.3.0
1285      */
1286     protected static function getSeleneseFiles($directory, $suffix)
1287     {
1288         $files = array();
1289
1290         $iterator = new PHPUnit_Util_FilterIterator(
1291           new RecursiveIteratorIterator(
1292             new RecursiveDirectoryIterator($directory)
1293           ),
1294           $suffix
1295         );
1296
1297         foreach ($iterator as $file) {
1298             $files[] = (string)$file;
1299         }
1300
1301         return $files;
1302     }
1303
1304     /**
1305      * @param  string $action
1306      * @since  Method available since Release 3.2.0
1307      */
1308     public function runDefaultAssertions($action)
1309     {
1310         if (!$this->inDefaultAssertions) {
1311             $this->inDefaultAssertions = TRUE;
1312             $this->defaultAssertions($action);
1313             $this->inDefaultAssertions = FALSE;
1314         }
1315     }
1316 }
1317 ?>