Locator.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Data object for the information needed to locate an element in a web page
  24. * using Mink.
  25. *
  26. * Locators can be created directly using the constructor, or through a more
  27. * fluent interface with Locator::forThe().
  28. */
  29. class Locator {
  30. /**
  31. * @var string
  32. */
  33. private $description;
  34. /**
  35. * @var string
  36. */
  37. private $selector;
  38. /**
  39. * @var string|array
  40. */
  41. private $locator;
  42. /**
  43. * @var null|Locator|\Behat\Mink\Element\ElementInterface
  44. */
  45. private $ancestor;
  46. /**
  47. * Starting point for the fluent interface to create Locators.
  48. *
  49. * @return LocatorBuilder
  50. */
  51. public static function forThe() {
  52. return new LocatorBuilder();
  53. }
  54. /**
  55. * @param string $description
  56. * @param string $selector
  57. * @param string|array $locator
  58. * @param null|Locator|\Behat\Mink\Element\ElementInterface $ancestor
  59. */
  60. public function __construct($description, $selector, $locator, $ancestor = null) {
  61. $this->description = $description;
  62. $this->selector = $selector;
  63. $this->locator = $locator;
  64. $this->ancestor = $ancestor;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getDescription() {
  70. return $this->description;
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function getSelector() {
  76. return $this->selector;
  77. }
  78. /**
  79. * @return string|array
  80. */
  81. public function getLocator() {
  82. return $this->locator;
  83. }
  84. /**
  85. * @return null|Locator|\Behat\Mink\Element\ElementInterface
  86. */
  87. public function getAncestor() {
  88. return $this->ancestor;
  89. }
  90. }
  91. class LocatorBuilder {
  92. /**
  93. * @param string $selector
  94. * @param string|array $locator
  95. * @return LocatorBuilderSecondStep
  96. */
  97. public function customSelector($selector, $locator) {
  98. return new LocatorBuilderSecondStep($selector, $locator);
  99. }
  100. /**
  101. * @param string $cssExpression
  102. * @return LocatorBuilderSecondStep
  103. */
  104. public function css($cssExpression) {
  105. return $this->customSelector("css", $cssExpression);
  106. }
  107. /**
  108. * @param string $xpathExpression
  109. * @return LocatorBuilderSecondStep
  110. */
  111. public function xpath($xpathExpression) {
  112. return $this->customSelector("xpath", $xpathExpression);
  113. }
  114. /**
  115. * @param string $value
  116. * @return LocatorBuilderSecondStep
  117. */
  118. public function id($value) {
  119. return $this->customSelector("named_exact", array("id", $value));
  120. }
  121. /**
  122. * @param string $value
  123. * @return LocatorBuilderSecondStep
  124. */
  125. public function idOrName($value) {
  126. return $this->customSelector("named_exact", array("id_or_name", $value));
  127. }
  128. /**
  129. * @param string $value
  130. * @return LocatorBuilderSecondStep
  131. */
  132. public function link($value) {
  133. return $this->customSelector("named_exact", array("link", $value));
  134. }
  135. /**
  136. * @param string $value
  137. * @return LocatorBuilderSecondStep
  138. */
  139. public function button($value) {
  140. return $this->customSelector("named_exact", array("button", $value));
  141. }
  142. /**
  143. * @param string $value
  144. * @return LocatorBuilderSecondStep
  145. */
  146. public function linkOrButton($value) {
  147. return $this->customSelector("named_exact", array("link_or_button", $value));
  148. }
  149. /**
  150. * @param string $value
  151. * @return LocatorBuilderSecondStep
  152. */
  153. public function field($value) {
  154. return $this->customSelector("named_exact", array("field", $value));
  155. }
  156. /**
  157. * @param string $value
  158. * @return LocatorBuilderSecondStep
  159. */
  160. public function selectField($value) {
  161. return $this->customSelector("named_exact", array("select", $value));
  162. }
  163. /**
  164. * @param string $value
  165. * @return LocatorBuilderSecondStep
  166. */
  167. public function checkbox($value) {
  168. return $this->customSelector("named_exact", array("checkbox", $value));
  169. }
  170. /**
  171. * @param string $value
  172. * @return LocatorBuilderSecondStep
  173. */
  174. public function radioButton($value) {
  175. return $this->customSelector("named_exact", array("radio", $value));
  176. }
  177. /**
  178. * @param string $value
  179. * @return LocatorBuilderSecondStep
  180. */
  181. public function fileInput($value) {
  182. return $this->customSelector("named_exact", array("file", $value));
  183. }
  184. /**
  185. * @param string $value
  186. * @return LocatorBuilderSecondStep
  187. */
  188. public function optionGroup($value) {
  189. return $this->customSelector("named_exact", array("optgroup", $value));
  190. }
  191. /**
  192. * @param string $value
  193. * @return LocatorBuilderSecondStep
  194. */
  195. public function option($value) {
  196. return $this->customSelector("named_exact", array("option", $value));
  197. }
  198. /**
  199. * @param string $value
  200. * @return LocatorBuilderSecondStep
  201. */
  202. public function fieldSet($value) {
  203. return $this->customSelector("named_exact", array("fieldset", $value));
  204. }
  205. /**
  206. * @param string $value
  207. * @return LocatorBuilderSecondStep
  208. */
  209. public function table($value) {
  210. return $this->customSelector("named_exact", array("table", $value));
  211. }
  212. }
  213. class LocatorBuilderSecondStep {
  214. /**
  215. * @var string
  216. */
  217. private $selector;
  218. /**
  219. * @var string|array
  220. */
  221. private $locator;
  222. /**
  223. * @param string $selector
  224. * @param string|array $locator
  225. */
  226. public function __construct($selector, $locator) {
  227. $this->selector = $selector;
  228. $this->locator = $locator;
  229. }
  230. /**
  231. * @param Locator|\Behat\Mink\Element\ElementInterface $ancestor
  232. * @return LocatorBuilderThirdStep
  233. */
  234. public function descendantOf($ancestor) {
  235. return new LocatorBuilderThirdStep($this->selector, $this->locator, $ancestor);
  236. }
  237. /**
  238. * @param string $description
  239. * @return Locator
  240. */
  241. public function describedAs($description) {
  242. return new Locator($description, $this->selector, $this->locator);
  243. }
  244. }
  245. class LocatorBuilderThirdStep {
  246. /**
  247. * @var string
  248. */
  249. private $selector;
  250. /**
  251. * @var string|array
  252. */
  253. private $locator;
  254. /**
  255. * @var Locator|\Behat\Mink\Element\ElementInterface
  256. */
  257. private $ancestor;
  258. /**
  259. * @param string $selector
  260. * @param string|array $locator
  261. * @param Locator|\Behat\Mink\Element\ElementInterface $ancestor
  262. */
  263. public function __construct($selector, $locator, $ancestor) {
  264. $this->selector = $selector;
  265. $this->locator = $locator;
  266. $this->ancestor = $ancestor;
  267. }
  268. /**
  269. * @param string $description
  270. * @return Locator
  271. */
  272. public function describedAs($description) {
  273. return new Locator($description, $this->selector, $this->locator, $this->ancestor);
  274. }
  275. }