/SaunterPHP/Framework/TestCase/WebDriver.php

https://github.com/perzeababa/saunter.php · PHP · 218 lines · 89 code · 15 blank · 114 comment · 16 complexity · a4692306a40ff1982adbea1a3ee572e7 MD5 · raw file

  1. <?php
  2. /**
  3. * @package SaunterPHP
  4. * @subpackage Framework_TestCase
  5. */
  6. namespace WebDriver;
  7. require_once 'SaunterPHP/Framework/SeleniumConnection.php';
  8. require_once 'SaunterPHP/Framework/SuiteIdentifier.php';
  9. require_once 'PHPUnit/Framework/TestCase.php';
  10. require_once 'Log.php';
  11. abstract class SaunterPHP_Framework_SaunterTestCase extends \PHPUnit_Framework_TestCase {
  12. static public $log;
  13. static public $verificationErrors;
  14. public function setUp() {
  15. self::$verificationErrors = array();
  16. self::$log = \Log::singleton('file', $GLOBALS['settings']['logname'], $this->getName());
  17. if ($GLOBALS['settings']['sauce.ondemand']) {
  18. $command_executor = "http://" . $GLOBALS['saucelabs']['username'] . ":" . $GLOBALS['saucelabs']['key'] . "@ondemand.saucelabs.com:80/wd/hub";
  19. } else {
  20. $command_executor = "http://" . $GLOBALS['settings']['seleniumserver'] . ":" . $GLOBALS['settings']['seleniumport'] . "/wd/hub";
  21. }
  22. $this->driver = new \SaunterPHP_Framework_Bindings_SaunterWebDriver($command_executor);
  23. // var_dump($this->driver);
  24. // this is inefficient, but...
  25. $decoded = json_decode($GLOBALS['settings']['browser'], true);
  26. if ($decoded) {
  27. $browser = $decoded["browser"];
  28. } else {
  29. $browser = $GLOBALS['settings']['browser'];
  30. }
  31. // since the config can be shared between, take out the rc *
  32. if (substr($browser, 0, 1) === "*") {
  33. $browser = substr($browser, 1);
  34. }
  35. if ($GLOBALS['settings']['sauce.ondemand']) {
  36. $additional_capabilities = array();
  37. switch ($decoded["os"]) {
  38. case 'Windows 2003':
  39. $additional_capabilities["platform"] = "XP";
  40. break;
  41. case 'Windows 2008':
  42. $additional_capabilities["platform"] = "VISTA";
  43. break;
  44. default:
  45. $additional_capabilities["platform"] = "LINUX";
  46. }
  47. if (substr($decoded["browser-version"], -1, 1) === ".") {
  48. $additional_capabilities["version"] = substr($decoded["browser-version"], 0, -1);
  49. } else {
  50. $additional_capabilities["version"] = $decoded["browser-version"];
  51. }
  52. } else {
  53. $additional_capabilities = array();
  54. }
  55. $this->session = $this->driver->session($browser, $additional_capabilities);
  56. // var_dump($this->session);
  57. $this->sessionId = substr($this->session->getURL(), strrpos($this->session->getURL(), "/") + 1);
  58. // var_dump($this->sessionId);
  59. }
  60. // fired after the test run but before teardown
  61. public function assertPostConditions() {
  62. $this->assertEmpty(self::$verificationErrors, implode("\n", self::$verificationErrors));
  63. }
  64. public function tearDown() { }
  65. // /**
  66. // * Verifies that the requested cookie has been set
  67. // *
  68. // * @param string $want
  69. // * @access public
  70. // * @return void
  71. // */
  72. // public function verifyCookiePresent($want) {
  73. // try {
  74. // $this->assertTrue(self::$selenium->isCookiePresent($want), $want . ' cookie is not present.');
  75. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  76. // array_push(self::$verificationErrors, $e->toString());
  77. // }
  78. // }
  79. //
  80. // public function verifyCookieNotPresent($want)
  81. // {
  82. // try {
  83. // $this->assertTrue(!self::$selenium->isCookiePresent($want), $want . ' cookie is present.');
  84. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  85. // array_push(self::$verificationErrors, $e->toString());
  86. // }
  87. // }
  88. //
  89. // public function verifyElementAvailable($element) {
  90. // try {
  91. // $this->assertTrue(self::$selenium->isElementPresent($element), $element . ' element is not present.');
  92. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  93. // array_push(self::$verificationErrors, $e->toString());
  94. // }
  95. // if (self::$selenium->isElementPresent($element)) {
  96. // try {
  97. // $this->assertTrue(self::$selenium->isVisible($element), $element . ' element is not available.');
  98. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  99. // array_push(self::$verificationErrors, $e->toString());
  100. // }
  101. // }
  102. // }
  103. //
  104. // public function verifyElementNotPresent($element) {
  105. // try {
  106. // $this->assertTrue(!self::$selenium->isElementPresent($element), $element . ' element is present.');
  107. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  108. // array_push(self::$verificationErrors, $e->toString());
  109. // }
  110. // }
  111. //
  112. public function verifyEquals($want, $got, $message = "") {
  113. try {
  114. $this->assertEquals($want, $got);
  115. } catch (PHPUnit_Framework_AssertionFailedError $e) {
  116. if ($message) {
  117. array_push(self::$verificationErrors, $message);
  118. } else {
  119. array_push(self::$verificationErrors, $e->toString());
  120. }
  121. }
  122. }
  123. // public function verifyLocation($relativeURL) {
  124. // try {
  125. // $this->assertEquals($GLOBALS['settings']['webserver'] . $relativeURL, self::$selenium->getLocation(), "URLs don't match with, " . $relativeURL);
  126. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  127. // array_push(self::$verificationErrors, $e->toString());
  128. // }
  129. // }
  130. //
  131. // public function verifyNotEquals($want, $got) {
  132. // try {
  133. // $this->assertNotEquals($want, $got);
  134. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  135. // array_push(self::$verificationErrors, $e->toString());
  136. // }
  137. // }
  138. //
  139. // public function verifyNotLocation($relativeURL)
  140. // {
  141. // try {
  142. // $this->assertNotEquals($GLOBALS['settings']['webserver'] . $relativeURL, self::$selenium->getLocation(), "URLs still match with, " . $relativeURL);
  143. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  144. // array_push(self::$verificationErrors, $e->toString());
  145. // }
  146. // }
  147. //
  148. // public function verifyNotTextRegEx($element,$pattern) {
  149. // try {
  150. // $this->assertTrue(!(bool)preg_match($pattern,self::$selenium->getText($element)));
  151. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  152. // array_push(self::$verificationErrors, $e->toString());
  153. // }
  154. // }
  155. //
  156. // public function verifyTextPresent($want) {
  157. // try {
  158. // $this->assertTrue(self::$selenium->isTextPresent($want), $want . ' Text is not present.');
  159. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  160. // array_push(self::$verificationErrors, $e->toString());
  161. // }
  162. // }
  163. //
  164. // public function verifyTextNotPresent($want) {
  165. // try {
  166. // $this->assertTrue(!self::$selenium->isTextPresent($want), $want . ' Text is present.');
  167. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  168. // array_push(self::$verificationErrors, $e->toString());
  169. // }
  170. // }
  171. //
  172. // public function verifyTextRegEx($element,$pattern) {
  173. // try {
  174. // $this->assertTrue((bool)preg_match($pattern,self::$selenium->getText($element)));
  175. // } catch (PHPUnit_Framework_AssertionFailedError $e) {
  176. // array_push(self::$verificationErrors, $e->toString());
  177. // }
  178. // }
  179. //
  180. public function verifyTrue($condition, $message = "") {
  181. try {
  182. $this->assertTrue($condition);
  183. } catch (PHPUnit_Framework_AssertionFailedError $e) {
  184. if ($message) {
  185. array_push(self::$verificationErrors, $message);
  186. } else {
  187. array_push(self::$verificationErrors, $e->toString());
  188. }
  189. }
  190. }
  191. public function verifyFalse($condition, $message = "") {
  192. try {
  193. $this->assertFalse($condition);
  194. } catch (PHPUnit_Framework_AssertionFailedError $e) {
  195. if ($message) {
  196. array_push(self::$verificationErrors, $message);
  197. } else {
  198. array_push(self::$verificationErrors, $e->toString());
  199. }
  200. }
  201. }
  202. }
  203. ?>