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

/Component/Selenium1Facade.php

https://github.com/andreaswolf/Menta
PHP | 456 lines | 206 code | 51 blank | 199 comment | 30 complexity | 7e2b85af0004446db205c808b649e82c MD5 | raw file
  1. <?php
  2. /**
  3. * Facade for methods that were available in PHPUnit_Extensions_SeleniumTestCase.
  4. * This component is intendet to be used from the magic __call() method of the
  5. * Menta_Testcase_Selenium1, which aims to be a drop-in-replacement for
  6. * PHPUnit_Extensions_SeleniumTestCase.
  7. *
  8. * This also can be used by developers that are familiar with the Selenium 1 api implemented
  9. * in PHPUnit_Extensions_SeleniumTestCase, not wanting to switch to Selenium 2
  10. *
  11. * @throws Exception
  12. */
  13. class Menta_Component_Selenium1Facade extends Menta_Component_AbstractTest {
  14. /**
  15. * @var int default timeout
  16. */
  17. protected $timeout = 60;
  18. /**
  19. * @var string base url (was needed in Selenium 1 and wil be used here as a prefix for all requests)
  20. */
  21. protected $browserUrl;
  22. public function setBrowserUrl($browserUrl) {
  23. $this->browserUrl = $browserUrl;
  24. }
  25. /**
  26. * Assert title
  27. *
  28. * @param string $title
  29. * @return void
  30. */
  31. public function assertTitle($title) {
  32. $this->getHelperAssert()->assertTitle($title);
  33. }
  34. /**
  35. * Check if element is present
  36. *
  37. * @param string|array|WebDriver_Element $element
  38. * @return bool
  39. */
  40. public function isElementPresent($element) {
  41. return $this->getHelperCommon()->isElementPresent($element);
  42. }
  43. /**
  44. * Check if text is present
  45. *
  46. * @param string $text
  47. * @return bool
  48. */
  49. public function isTextPresent($text) {
  50. return $this->getHelperCommon()->isTextPresent($text);
  51. }
  52. /**
  53. * Wait for element present
  54. *
  55. * @param string|array|WebDriver_Element $locator
  56. * @param int $timeout
  57. * @param string $message
  58. * @return void
  59. */
  60. public function waitForElementPresent($locator, $timeout=null, $message=null) {
  61. if (is_null($message)) {
  62. $message = sprintf("Waiting for element '%s' timed out", $this->getHelperCommon()->element2String($locator));
  63. if (!is_null($timeout)) {
  64. $message .= " after $timeout seconds";
  65. }
  66. }
  67. if (!$this->getHelperWait()->waitForElementPresent($locator, $timeout)) {
  68. $this->getTest()->fail($message);
  69. }
  70. }
  71. /**
  72. * Wait for element not present
  73. *
  74. * @param string|array|WebDriver_Element $locator
  75. * @param int $timeout
  76. * @param string $message
  77. * @return void
  78. */
  79. public function waitForElementNotPresent($locator, $timeout=null, $message=null) {
  80. if (is_null($message)) {
  81. $message = sprintf("Waiting for element '%s' disappearing timed out", $this->getHelperCommon()->element2String($locator));
  82. if (!is_null($timeout)) {
  83. $message .= " after $timeout seconds";
  84. }
  85. }
  86. if (!$this->getHelperWait()->waitForElementNotPresent($locator, $timeout)) {
  87. $this->getTest()->fail($message);
  88. }
  89. }
  90. /**
  91. * Wait for text present
  92. *
  93. * @param string $text
  94. * @param int $timeout
  95. * @param string $message
  96. * @return void
  97. */
  98. public function waitForTextPresent($text, $timeout=null, $message=null) {
  99. if (is_null($message)) {
  100. $message = sprintf("Waiting for text '%s' timed out", $text);
  101. if (!is_null($timeout)) {
  102. $message .= " after $timeout seconds";
  103. }
  104. }
  105. if (!$this->getHelperWait()->waitForTextPresent($text, $timeout)) {
  106. $this->getTest()->fail($message);
  107. }
  108. }
  109. /**
  110. * Wait for text not present
  111. *
  112. * @param string $text
  113. * @param int $timeout
  114. * @param string $message
  115. * @return void
  116. */
  117. public function waitForTextNotPresent($text, $timeout=null, $message=NULL) {
  118. if (is_null($message)) {
  119. $message = sprintf("Waiting for text '%s' disappearing timed out", $text);
  120. if (!is_null($timeout)) {
  121. $message .= " after $timeout seconds";
  122. }
  123. }
  124. if (!$this->getHelperWait()->waitForTextNotPresent($text, $timeout)) {
  125. $this->getTest()->fail($message);
  126. }
  127. }
  128. /**
  129. * Wait for condition
  130. *
  131. * @param string $jsSnippet
  132. * @param int $timeout
  133. * @param string $message
  134. * @return void
  135. */
  136. public function waitForCondition($jsSnippet, $timeout=NULL, $message=NULL) {
  137. if (is_null($message)) {
  138. $message = sprintf("Waiting for condition '%s' timed out", $jsSnippet);
  139. if (!is_null($timeout)) {
  140. $message .= " after $timeout seconds";
  141. }
  142. }
  143. if (!$this->getHelperWait()->waitForCondition($jsSnippet, $timeout)) {
  144. $this->getTest()->fail($message);
  145. }
  146. }
  147. public function waitForVisible($locator, $timeout=NULL, $message=NULL) {#
  148. if (is_null($message)) {
  149. $message = sprintf("Waiting for element '%s' visible timed out", $this->getHelperCommon()->element2String($locator));
  150. if (!is_null($timeout)) {
  151. $message .= " after $timeout seconds";
  152. }
  153. }
  154. if (!$this->getHelperWait()->waitForElementVisible($locator, $timeout)) {
  155. $this->getTest()->fail($message);
  156. }
  157. }
  158. public function waitForNotVisible($locator, $timeout=NULL, $message=NULL) {
  159. if (is_null($message)) {
  160. $message = sprintf("Waiting for element '%s' not visible timed out", $this->getHelperCommon()->element2String($locator));
  161. if (!is_null($timeout)) {
  162. $message .= " after $timeout seconds";
  163. }
  164. }
  165. if (!$this->getHelperWait()->waitForElementNotVisible($locator, $timeout)) {
  166. $this->getTest()->fail($message);
  167. }
  168. }
  169. /**
  170. * Auto-detect element
  171. *
  172. * @param string|array|WebDriver_Element $element
  173. * @return WebDriver_Element
  174. */
  175. public function getElement($element) {
  176. return $this->getHelperCommon()->getElement($element);
  177. }
  178. /**
  179. * Get page title
  180. *
  181. * @return string
  182. */
  183. public function getTitle() {
  184. return $this->getHelperCommon()->getTitle();
  185. }
  186. /**
  187. * Get text
  188. *
  189. * @param string|array|WebDriver_Element $element
  190. * @return string
  191. */
  192. public function getText($element) {
  193. return $this->getHelperCommon()->getText($element);
  194. }
  195. /**
  196. * Get browser url
  197. *
  198. * @return string
  199. * @throws Exception
  200. */
  201. public function getBrowserUrl() {
  202. if (empty($this->browserUrl)) {
  203. // $this->browserUrl = Menta_ConfigurationPhpUnitVars::getInstance()->getValue('testing.maindomain');
  204. throw new Exception('browserUrl has not been set.');
  205. }
  206. return $this->browserUrl;
  207. }
  208. /**
  209. * Open an url prefixed with the previously configured browserUrl
  210. *
  211. * @param string $url
  212. * @return WebDriver_Base
  213. */
  214. public function open($url) {
  215. if (!preg_match('/^https?:/i', $url)) {
  216. $url = $this->getBrowserUrl() . $url;
  217. }
  218. return $this->getSession()->open($url);
  219. }
  220. public function start() {
  221. // session handling is controlled different now
  222. }
  223. public function stop() {
  224. // session handling is controlled different now
  225. }
  226. public function windowFocus() {
  227. $this->getHelperCommon()->focusWindow();
  228. }
  229. public function windowMaximize() {
  230. $this->getHelperCommon()->resizeBrowserWindow();
  231. }
  232. public function waitForPageToLoad() {
  233. // TODO: find an appropriate solution to this
  234. // http://seleniumhq.org/docs/appendix_migrating_from_rc_to_webdriver.html#waitforpagetoload-returns-too-soon
  235. sleep(5);
  236. }
  237. public function getEval($jsSnippet) {
  238. return $this->getHelperCommon()->getEval($jsSnippet);
  239. }
  240. /**
  241. * Click on an element
  242. *
  243. * @param string|array|WebDriver_Element $element
  244. * @return void
  245. */
  246. public function click($element) {
  247. return $this->getHelperCommon()->click($element);
  248. }
  249. /**
  250. * Type something
  251. *
  252. * @param string|array|WebDriver_Element $element
  253. * @param $text
  254. * @return void
  255. */
  256. public function type($element, $text) {
  257. return $this->getHelperCommon()->type($element, $text);
  258. }
  259. /**
  260. * Type text into an input field
  261. * Reset first and click outside afterwards
  262. *
  263. * @param string|array|WebDriver_Element $element
  264. * @param string $text
  265. * @return void
  266. */
  267. public function typeAndLeave($element, $text) {
  268. return $this->getHelperCommon()->type($element, $text, true, true);
  269. }
  270. /**
  271. * Select an option of a select box
  272. * Option can be specified via
  273. * - "value=<value>" -or-
  274. * - "label=<label>"
  275. *
  276. * @throws Exception
  277. * @param string|array|WebDriver_Element $element
  278. * @param string $option
  279. * @return void
  280. */
  281. public function select($element, $option) {
  282. $this->getHelperCommon()->select($element, $option);
  283. }
  284. public function fireEvent($element, $event) {
  285. // do nothing?!
  286. // TODO
  287. }
  288. /**
  289. * Get value
  290. *
  291. * @param string|array|WebDriver_Element $element
  292. * @return string
  293. */
  294. public function getValue($element) {
  295. $element = $this->getElement($element);
  296. return $element->getAttribute('value');
  297. }
  298. /**
  299. * Check if element is visible
  300. *
  301. * @param string|array|WebDriver_Element $element
  302. * @return bool
  303. */
  304. public function isVisible($element) {
  305. return $this->getHelperCommon()->isVisible($element);
  306. }
  307. /**
  308. * Get selected label
  309. *
  310. * @param string|array|WebDriver_Element $element
  311. * @return bool|string
  312. */
  313. public function getSelectedLabel($element) {
  314. return $this->getHelperCommon()->getSelectedLabel($element);
  315. }
  316. /**
  317. * Get selected value
  318. *
  319. * @param string|array|WebDriver_Element $element
  320. * @return bool|string
  321. */
  322. public function getSelectedValue($element) {
  323. return $this->getHelperCommon()->getSelectedValue($element);
  324. }
  325. /**
  326. * Get first selected option
  327. *
  328. * @param string|array|WebDriver_Element $element
  329. * @return bool|Webdriver_Element
  330. */
  331. public function getFirstSelectedOption($element) {
  332. return $this->getHelperCommon()->getFirstSelectedOption($element);
  333. }
  334. public function getXpathCount($xpath) {
  335. return $this->getElementCount($xpath); // xpath is supported by parseLocator() and will be autodetected
  336. }
  337. public function getElementCount($locator) {
  338. return $this->getHelperCommon()->getElementCount($locator);
  339. }
  340. public function assertTextPresent($text, $message='') {
  341. $this->getHelperAssert()->assertTextPresent($text, $message);
  342. }
  343. public function assertTextNotPresent($text, $message='') {
  344. $this->getHelperAssert()->assertTextNotPresent($text, $message);
  345. }
  346. /**
  347. * @param string $element
  348. */
  349. public function clickAndWait($element) {
  350. $this->click($element);
  351. $this->waitForPageToLoad();
  352. }
  353. public function assertElementPresent($element, $message='') {
  354. $this->getHelperAssert()->assertElementPresent($element, $message);
  355. }
  356. public function assertElementNotPresent($element, $message='') {
  357. $this->getHelperAssert()->assertElementNotPresent($element, $message);
  358. }
  359. public function waitForAjaxCompletedJquery() {
  360. $this->getHelperWait()->waitForCondition('return (jQuery.active == 0)');
  361. }
  362. public function waitForAjaxCompletedPrototype() {
  363. $this->getHelperWait()->waitForCondition('return (Ajax.activeRequestCount == 0)');
  364. }
  365. /**
  366. * @param string $clickElement
  367. * @param string $waitForElementAfterClick
  368. * @param int $timeout timeout in seconds
  369. */
  370. public function clickAndWaitAjax($clickElement, $waitForElementAfterClick, $timeout=NULL) {
  371. $this->getTest()->assertTrue($this->isElementPresent($clickElement), 'element: ' . $clickElement . ' is not present' );
  372. $this->click($clickElement);
  373. $this->waitForElementPresent($waitForElementAfterClick, $timeout);
  374. }
  375. /**
  376. * Assert element containts text
  377. *
  378. * @param string|array|WebDriver_Element $element
  379. * @param string $text
  380. * @param string $message
  381. * @return void
  382. */
  383. public function assertElementContainsText($element, $text, $message='') {
  384. $this->getHelperAssert()->assertElementContainsText($element, $text, $message);
  385. }
  386. /* Convenience methods for code completion */
  387. /**
  388. * @return Menta_Component_Helper_Common
  389. */
  390. public function getHelperCommon() {
  391. return Menta_ComponentManager::get('Menta_Component_Helper_Common');
  392. }
  393. /**
  394. * @return Menta_Component_Helper_Wait
  395. */
  396. public function getHelperWait() {
  397. return Menta_ComponentManager::get('Menta_Component_Helper_Wait');
  398. }
  399. /**
  400. * @return Menta_Component_Helper_Assert
  401. */
  402. public function getHelperAssert() {
  403. return Menta_ComponentManager::get('Menta_Component_Helper_Assert');
  404. }
  405. }