/tests/system/webdriver/SeleniumClient/WebElement.php

https://github.com/dextercowley/joomla-cms · PHP · 255 lines · 87 code · 49 blank · 119 comment · 12 complexity · e0cdc8c215319503289c609b89f2b8f3 MD5 · raw file

  1. <?php
  2. // Copyright 2012-present Nearsoft, Inc
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS,
  9. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. // See the License for the specific language governing permissions and
  11. // limitations under the License.
  12. namespace SeleniumClient;
  13. use SeleniumClient\Http\SeleniumStaleElementReferenceException;
  14. class WebElement
  15. {
  16. private $_driver = null;
  17. private $_elementId = null;
  18. function __construct(WebDriver $driver, $elementId)
  19. {
  20. $this->_driver = $driver;
  21. $this->_elementId = $elementId;
  22. }
  23. /**
  24. * Gets element's id
  25. * @return Integer
  26. */
  27. public function getElementId() { return $this->_elementId; }
  28. /**
  29. * Send text to element
  30. * @param String $text
  31. */
  32. public function sendKeys($text) { $this->_driver->webElementSendKeys($this->_elementId,$text); }
  33. /**
  34. * Gets element's visible text
  35. * @return String
  36. */
  37. public function getText() { return $this->_driver->webElementGetText($this->_elementId); }
  38. /**
  39. * Gets element's tag name
  40. * @return String
  41. */
  42. public function getTagName() { return $this->_driver->webElementGetTagName($this->_elementId); }
  43. /**
  44. * Gets element's specified attribute's value
  45. * @param String $attributeName
  46. * @return String
  47. */
  48. public function getAttribute($attributeName)
  49. {
  50. //attributeName should be string
  51. return $this->_driver->webElementGetAttribute($this->_elementId, $attributeName);
  52. }
  53. /**
  54. * Gets whether element is selected
  55. * @return Boolean
  56. */
  57. public function isSelected() { return $this->_driver->webElementIsSelected($this->_elementId); }
  58. /**
  59. * Gets whether element is displayed
  60. * @return Boolean
  61. */
  62. public function isDisplayed() { return $this->_driver->webElementIsDisplayed($this->_elementId); }
  63. /**
  64. * Gets whether element is enabled
  65. * @return Boolean
  66. */
  67. public function isEnabled() { return $this->_driver->webElementIsEnabled($this->_elementId); }
  68. /**
  69. * Clear current element's text
  70. */
  71. public function clear() { return $this->_driver->webElementClear($this->_elementId); }
  72. /**
  73. * Click on element
  74. */
  75. public function click() { $this->_driver->webElementClick($this->_elementId); }
  76. /**
  77. * Submit form from element
  78. */
  79. public function submit() { $this->_driver->webElementSubmit($this->_elementId); }
  80. /**
  81. * Gets element's description
  82. * @return Array
  83. */
  84. public function describe() { return $this->_driver->webElementDescribe($this->_elementId); }
  85. /**
  86. * Get element's coordinates
  87. * @return Array
  88. */
  89. public function getCoordinates() {return $this->_driver->webElementGetCoordinates($this->_elementId);}
  90. /**
  91. * Get element's coordinates after scrolling
  92. * @return Array
  93. */
  94. public function getLocationOnScreenOnceScrolledIntoView() {return $this->_driver->webElementGetLocationOnScreenOnceScrolledIntoView($this->_elementId);}
  95. /**
  96. * Find element within current element
  97. * @param By $locator
  98. * @param Boolean $polling
  99. * @return SeleniumClient\WebElement
  100. */
  101. public function findElement(By $locator, $polling = false) { return $this->_driver->webElementFindElement($this->_elementId, $locator, $polling); }
  102. /**
  103. * Find elements within current element
  104. * @param By $locator
  105. * @param Bolean $polling
  106. * @return Array SeleniumClient\WebElement
  107. */
  108. public function findElements(By $locator, $polling = false) { return $this->_driver->webElementFindElements($this->_elementId, $locator, $polling); }
  109. /**
  110. * Wait for expected element to be present within current element
  111. * @param By $locator
  112. * @param Integer $timeOutSeconds
  113. * @return Ambiguous
  114. */
  115. public function waitForElementUntilIsPresent(By $locator, $timeOutSeconds = 5)
  116. {
  117. //We have to validate that timeOutSeconds is int, we have to add a new exception into the selenium exceptions and not use the exceptions that are outsite of the library
  118. //if ( !is_int($timeOutSeconds) ) { throw new Not_Int_Exception("wait_for_element_until_is_present", "time_out_seconds"); }
  119. $wait = new WebDriverWait($timeOutSeconds);
  120. $dynamicElement = $wait->until($this, "findElement", array($locator, TRUE));
  121. return $dynamicElement;
  122. }
  123. /**
  124. * Wait for current element to be displayed
  125. * @param Integer $timeOutSeconds
  126. * @return \SeleniumClient\WebElement
  127. */
  128. public function waitForElementUntilIsDisplayed($timeOutSeconds = 5)
  129. {
  130. //We have to validate that timeOutSeconds is int, we have to add a new exception into the selenium exceptions and not use the exceptions that are outsite of the library
  131. //if ( !is_int($timeOutSeconds) ) { throw new Not_Int_Exception("wait_for_element_until_is_present", "time_out_seconds"); }
  132. $wait = new WebDriverWait($timeOutSeconds);
  133. $element = $wait->until($this, "isDisplayed", array());
  134. return $this;
  135. }
  136. /**
  137. * Wait for current element to be enabled
  138. * @param Integer $timeOutSeconds
  139. * @return \SeleniumClient\WebElement
  140. */
  141. public function waitForElementUntilIsEnabled($timeOutSeconds = 5)
  142. {
  143. //We have to validate that timeOutSeconds is int, we have to add a new exception into the selenium exceptions and not use the exceptions that are outsite of the library
  144. //if ( !is_int($timeOutSeconds) ) { throw new Not_Int_Exception("wait_for_element_until_is_present", "time_out_seconds"); }
  145. $wait = new WebDriverWait($timeOutSeconds);
  146. $element = $wait->until($this, "isEnabled", array());
  147. return $this;
  148. }
  149. /**
  150. * Wait until current element's text has changed
  151. * @param String $targetText
  152. * @param Integer $timeOutSeconds
  153. * @throws WebDriverWaitTimeoutException
  154. * @return \SeleniumClient\WebElement
  155. */
  156. public function waitForElementUntilTextIsChanged($targetText, $timeOutSeconds = 5)
  157. {
  158. //We have to validate that timeOutSeconds is int, we have to add a new exception into the selenium exceptions and not use the exceptions that are outsite of the library
  159. //if ( !is_int($timeOutSeconds) ) { throw new Not_Int_Exception("wait_for_element_until_is_present", "time_out_seconds"); }
  160. $wait = true;
  161. while ($wait)
  162. {
  163. $currentText = $this->getText();
  164. if ($currentText == $targetText) { $wait = false; }
  165. else if ($timeOutSeconds <= 0) { throw new WebDriverWaitTimeoutException ("Timeout for waitForElementUntilTextIsChange." ); }
  166. sleep(1);
  167. $timeOutSeconds = $timeOutSeconds - 1;
  168. }
  169. return $this;
  170. }
  171. /**
  172. * Wait until current element's text equals specified
  173. * @param By $locator
  174. * @param String $targetText
  175. * @param Integer $timeOutSeconds
  176. * @throws WebDriverWaitTimeoutException
  177. * @return \SeleniumClient\WebElement
  178. */
  179. public function waitForElementUntilIsPresentWithSpecificText(By $locator, $targetText, $timeOutSeconds = 5)
  180. {
  181. //We have to validate that timeOutSeconds is int, we have to add a new exception into the selenium exceptions and not use the exceptions that are outsite of the library
  182. //if ( !is_int($timeOutSeconds) ) { throw new Not_Int_Exception("wait_for_element_until_is_present", "time_out_seconds"); }
  183. $dynamicElement = null;
  184. $wait = true;
  185. $attempts = $timeOutSeconds;
  186. while ($wait)
  187. {
  188. $currentText = null;
  189. $webDriverWait = new WebDriverWait($timeOutSeconds);
  190. $dynamicElement = $webDriverWait->until($this, "findElement", array($locator, TRUE));
  191. try
  192. {
  193. $currentText = $dynamicElement->getText();
  194. }
  195. catch(SeleniumStaleElementReferenceException $ex)
  196. {
  197. //echo "\nError The Objet Disappear, Wait For Element Until Is Present With Specific Text\n";
  198. }
  199. if ($currentText == $targetText) { $wait = false; }
  200. else if ($attempts <= 0) { throw new WebDriverWaitTimeoutException ("Timeout for waitForElementUntilIsPresentAndTextIsChange." ); }
  201. sleep(1);
  202. $attempts = $attempts - 1;
  203. }
  204. return $dynamicElement;
  205. }
  206. }