AbstractWebDriverCheckboxOrRadio.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. // Copyright 2004-present Facebook. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. namespace Facebook\WebDriver;
  16. use Facebook\WebDriver\Exception\NoSuchElementException;
  17. use Facebook\WebDriver\Exception\UnexpectedTagNameException;
  18. use Facebook\WebDriver\Exception\WebDriverException;
  19. use Facebook\WebDriver\Support\XPathEscaper;
  20. /**
  21. * Provides helper methods for checkboxes and radio buttons.
  22. */
  23. abstract class AbstractWebDriverCheckboxOrRadio implements WebDriverSelectInterface
  24. {
  25. /** @var WebDriverElement */
  26. protected $element;
  27. /** @var string */
  28. protected $type;
  29. /** @var string */
  30. protected $name;
  31. public function __construct(WebDriverElement $element)
  32. {
  33. $tagName = $element->getTagName();
  34. if ($tagName !== 'input') {
  35. throw new UnexpectedTagNameException('input', $tagName);
  36. }
  37. $this->name = $element->getAttribute('name');
  38. if ($this->name === null) {
  39. throw new WebDriverException('The input does not have a "name" attribute.');
  40. }
  41. $this->element = $element;
  42. }
  43. public function getOptions()
  44. {
  45. return $this->getRelatedElements();
  46. }
  47. public function getAllSelectedOptions()
  48. {
  49. $selectedElement = [];
  50. foreach ($this->getRelatedElements() as $element) {
  51. if ($element->isSelected()) {
  52. $selectedElement[] = $element;
  53. if (!$this->isMultiple()) {
  54. return $selectedElement;
  55. }
  56. }
  57. }
  58. return $selectedElement;
  59. }
  60. public function getFirstSelectedOption()
  61. {
  62. foreach ($this->getRelatedElements() as $element) {
  63. if ($element->isSelected()) {
  64. return $element;
  65. }
  66. }
  67. throw new NoSuchElementException(
  68. sprintf('No %s are selected', 'radio' === $this->type ? 'radio buttons' : 'checkboxes')
  69. );
  70. }
  71. public function selectByIndex($index)
  72. {
  73. $this->byIndex($index);
  74. }
  75. public function selectByValue($value)
  76. {
  77. $this->byValue($value);
  78. }
  79. public function selectByVisibleText($text)
  80. {
  81. $this->byVisibleText($text);
  82. }
  83. public function selectByVisiblePartialText($text)
  84. {
  85. $this->byVisibleText($text, true);
  86. }
  87. /**
  88. * Selects or deselects a checkbox or a radio button by its value.
  89. *
  90. * @param string $value
  91. * @param bool $select
  92. * @throws NoSuchElementException
  93. */
  94. protected function byValue($value, $select = true)
  95. {
  96. $matched = false;
  97. foreach ($this->getRelatedElements($value) as $element) {
  98. $select ? $this->selectOption($element) : $this->deselectOption($element);
  99. if (!$this->isMultiple()) {
  100. return;
  101. }
  102. $matched = true;
  103. }
  104. if (!$matched) {
  105. throw new NoSuchElementException(
  106. sprintf('Cannot locate %s with value: %s', $this->type, $value)
  107. );
  108. }
  109. }
  110. /**
  111. * Selects or deselects a checkbox or a radio button by its index.
  112. *
  113. * @param int $index
  114. * @param bool $select
  115. * @throws NoSuchElementException
  116. */
  117. protected function byIndex($index, $select = true)
  118. {
  119. $elements = $this->getRelatedElements();
  120. if (!isset($elements[$index])) {
  121. throw new NoSuchElementException(sprintf('Cannot locate %s with index: %d', $this->type, $index));
  122. }
  123. $select ? $this->selectOption($elements[$index]) : $this->deselectOption($elements[$index]);
  124. }
  125. /**
  126. * Selects or deselects a checkbox or a radio button by its visible text.
  127. *
  128. * @param string $text
  129. * @param bool $partial
  130. * @param bool $select
  131. */
  132. protected function byVisibleText($text, $partial = false, $select = true)
  133. {
  134. foreach ($this->getRelatedElements() as $element) {
  135. $normalizeFilter = sprintf(
  136. $partial ? 'contains(normalize-space(.), %s)' : 'normalize-space(.) = %s',
  137. XPathEscaper::escapeQuotes($text)
  138. );
  139. $xpath = 'ancestor::label';
  140. $xpathNormalize = sprintf('%s[%s]', $xpath, $normalizeFilter);
  141. $id = $element->getAttribute('id');
  142. if ($id !== null) {
  143. $idFilter = sprintf('@for = %s', XPathEscaper::escapeQuotes($id));
  144. $xpath .= sprintf(' | //label[%s]', $idFilter);
  145. $xpathNormalize .= sprintf(' | //label[%s and %s]', $idFilter, $normalizeFilter);
  146. }
  147. try {
  148. $element->findElement(WebDriverBy::xpath($xpathNormalize));
  149. } catch (NoSuchElementException $e) {
  150. if ($partial) {
  151. continue;
  152. }
  153. try {
  154. // Since the mechanism of getting the text in xpath is not the same as
  155. // webdriver, use the expensive getText() to check if nothing is matched.
  156. if ($text !== $element->findElement(WebDriverBy::xpath($xpath))->getText()) {
  157. continue;
  158. }
  159. } catch (NoSuchElementException $e) {
  160. continue;
  161. }
  162. }
  163. $select ? $this->selectOption($element) : $this->deselectOption($element);
  164. if (!$this->isMultiple()) {
  165. return;
  166. }
  167. }
  168. }
  169. /**
  170. * Gets checkboxes or radio buttons with the same name.
  171. *
  172. * @param string|null $value
  173. * @return WebDriverElement[]
  174. */
  175. protected function getRelatedElements($value = null)
  176. {
  177. $valueSelector = $value ? sprintf(' and @value = %s', XPathEscaper::escapeQuotes($value)) : '';
  178. $formId = $this->element->getAttribute('form');
  179. if ($formId === null) {
  180. $form = $this->element->findElement(WebDriverBy::xpath('ancestor::form'));
  181. $formId = $form->getAttribute('id');
  182. if ($formId === '') {
  183. return $form->findElements(WebDriverBy::xpath(
  184. sprintf('.//input[@name = %s%s]', XPathEscaper::escapeQuotes($this->name), $valueSelector)
  185. ));
  186. }
  187. }
  188. // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#form
  189. return $this->element->findElements(
  190. WebDriverBy::xpath(sprintf(
  191. '//form[@id = %1$s]//input[@name = %2$s%3$s'
  192. . ' and ((boolean(@form) = true() and @form = %1$s) or boolean(@form) = false())]'
  193. . ' | //input[@form = %1$s and @name = %2$s%3$s]',
  194. XPathEscaper::escapeQuotes($formId),
  195. XPathEscaper::escapeQuotes($this->name),
  196. $valueSelector
  197. ))
  198. );
  199. }
  200. /**
  201. * Selects a checkbox or a radio button.
  202. */
  203. protected function selectOption(WebDriverElement $element)
  204. {
  205. if (!$element->isSelected()) {
  206. $element->click();
  207. }
  208. }
  209. /**
  210. * Deselects a checkbox or a radio button.
  211. */
  212. protected function deselectOption(WebDriverElement $element)
  213. {
  214. if ($element->isSelected()) {
  215. $element->click();
  216. }
  217. }
  218. }