PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/frapi/tests/phpunit/PHPUnit/Extensions/SeleniumTestCase.php

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