PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPUnit/Testcase/Selenium2.php

https://github.com/andreaswolf/Menta
PHP | 178 lines | 76 code | 23 blank | 79 comment | 8 complexity | 36cf927c2e602462baea8186b4b46749 MD5 | raw file
  1. <?php
  2. /**
  3. * Selenium1_Testcase
  4. * Base class for selenium tests that takes care setup up the connection to the
  5. * selenium server.
  6. * Unlike other base classes (e.g. PHPUnit_Extensions_SeleniumTestCase) this method
  7. * won't act as procy and passing commands to the webdriver, but focus on its main
  8. * purposes.
  9. * To interact with the selenium server use getSession() and/or getWebDriver()
  10. *
  11. * @author Fabrizio Branca
  12. * @since 2011-11-18
  13. */
  14. abstract class Menta_PHPUnit_Testcase_Selenium2 extends PHPUnit_Framework_TestCase implements Menta_Interface_ScreenshotTestcase {
  15. /**
  16. * @var bool
  17. */
  18. protected $captureScreenshotOnFailure = false;
  19. /**
  20. * @var array collected screenshots
  21. */
  22. protected $screenshots = array();
  23. /**
  24. * @var bool
  25. */
  26. protected $freshSessionForEachTestMethod = false;
  27. /**
  28. * @var Menta_Interface_Configuration
  29. */
  30. protected $configuration;
  31. /**
  32. * @param string $name
  33. * @param array $data
  34. * @param string $dataName
  35. * @throws InvalidArgumentException
  36. */
  37. public function __construct($name = NULL, array $data = array(), $dataName = '') {
  38. parent::__construct($name, $data, $dataName);
  39. $this->testId = md5(uniqid(rand(), TRUE));
  40. if ($this->getConfiguration()->issetKey('testing.selenium.captureScreenshotOnFailure')) {
  41. $this->captureScreenshotOnFailure = (bool)$this->getConfiguration()->getValue('testing.selenium.captureScreenshotOnFailure');
  42. }
  43. $GLOBALS['current_testcase'] = $this;
  44. }
  45. /**
  46. * Get configuration
  47. *
  48. * @return Menta_Interface_Configuration
  49. */
  50. public function getConfiguration() {
  51. if (is_null($this->configuration)) {
  52. $this->configuration = Menta_ConfigurationPhpUnitVars::getInstance();
  53. }
  54. return $this->configuration;
  55. }
  56. /**
  57. * Setup
  58. *
  59. * @return void
  60. */
  61. public function setUp() {
  62. if ($this->freshSessionForEachTestMethod) {
  63. // close previous session if exits
  64. // new session will be started on first call of
  65. // Menta_SessionManager::getSession()
  66. // or $this->getSession();
  67. Menta_SessionManager::closeSession();
  68. }
  69. parent::setUp();
  70. }
  71. /**
  72. * Get webdriver session
  73. *
  74. * @param bool $forceNew
  75. * @return WebDriver_Session
  76. */
  77. public function getSession($forceNew=false) {
  78. try {
  79. return Menta_SessionManager::getSession($forceNew);
  80. } catch (WebDriver_Exception_CurlExec $e) {
  81. $this->markTestSkipped($e->getMessage()); // couldn't connect to host
  82. }
  83. }
  84. /**
  85. * On not successful
  86. * In case of an exeception $this->tearDown() will be called before processing this method anyways
  87. *
  88. * @throws PHPUnit_Framework_SyntheticError
  89. * @param Exception $e
  90. * @return void
  91. */
  92. protected function onNotSuccessfulTest(Exception $e) {
  93. if ($this->captureScreenshotOnFailure) {
  94. try {
  95. $this->takeScreenshot(
  96. get_class($e),
  97. PHPUnit_Framework_TestFailure::exceptionToString($e),
  98. Menta_Util_Screenshot::TYPE_ERROR,
  99. $e->getTrace()
  100. );
  101. } catch (Exception $screenshotException) {
  102. // if there's an exception while taking a screenshot because a test was not successful. That's bad luck :)
  103. throw new PHPUnit_Framework_SyntheticError(
  104. $e->getMessage() . ' (AND: Exception while taking screenshot: '.$screenshotException->getMessage().')',
  105. $e->getCode(),
  106. $e->getFile(),
  107. $e->getLine(),
  108. $e->getTrace()
  109. );
  110. }
  111. }
  112. parent::onNotSuccessfulTest($e);
  113. }
  114. /**
  115. * METHODS IMPLEMENTING INTERFACE:
  116. * Menta_Interface_ScreenshotTestcase
  117. */
  118. /**
  119. * Take a screenshot
  120. *
  121. * @param string $title
  122. * @param string $description
  123. * @param string $type
  124. * @param array $trace
  125. * @return bool|Menta_Util_Screenshot
  126. */
  127. public function takeScreenshot($title=NULL, $description=NULL, $type=NULL, array $trace=NULL) {
  128. // don't init a new session if there is none
  129. if (!Menta_SessionManager::activeSessionExists()) {
  130. return false;
  131. }
  132. $time = time();
  133. $screenshotHelper = Menta_ComponentManager::get('Menta_Component_Helper_Screenshot'); /* @var $screenshotHelper Menta_Component_Helper_Screenshot */
  134. $base64Image = $screenshotHelper->takeScreenshotToString();
  135. // put data into the screenshot object
  136. $screenshot = new Menta_Util_Screenshot();
  137. $screenshot->setBase64Image($base64Image);
  138. $screenshot->setTime($time);
  139. if (!is_null($title)) { $screenshot->setTitle($title); }
  140. if (!is_null($description)) { $screenshot->setDescription($description); }
  141. if (!is_null($type)) { $screenshot->setType($type); }
  142. $screenshot->setTrace(!is_null($trace) ? $trace : debug_backtrace());
  143. $screenshot->setLocation($this->getSession()->url());
  144. $this->screenshots[] = $screenshot;
  145. return $screenshot;
  146. }
  147. /**
  148. * Get all screenshots that were taken so far
  149. *
  150. * @return array array of Menta_Util_Screenshot
  151. */
  152. public function getScreenshots() {
  153. return $this->screenshots;
  154. }
  155. }