PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/functional/lib/Magento/Mtf/Client/Element/MultiselectgrouplistElement.php

https://gitlab.com/axeltizon/magento-demopoweraccess
PHP | 327 lines | 213 code | 22 blank | 92 comment | 8 complexity | f68e9c43c46d64a64ba34bd53685cee4 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Client\Element;
  7. use Magento\Mtf\Client\Locator;
  8. use Magento\Mtf\Client\ElementInterface;
  9. /**
  10. * Class MultiselectgrouplistElement
  11. * Typified element class for multiselect with group
  12. */
  13. class MultiselectgrouplistElement extends MultiselectElement
  14. {
  15. /**
  16. * Indent length
  17. */
  18. const INDENT_LENGTH = 4;
  19. /**
  20. * Locator for search optgroup by label
  21. *
  22. * @var string
  23. */
  24. protected $optgroupByLabel = './/optgroup[@label="%s"]';
  25. /**
  26. * Locator for search optgroup by number
  27. *
  28. * @var string
  29. */
  30. protected $optgroupByNumber = './/optgroup[%d]';
  31. /**
  32. * Locator for search next optgroup
  33. *
  34. * @var string
  35. */
  36. protected $nextOptgroup = './/following-sibling::optgroup[%d]';
  37. /**
  38. * Locator for search child optgroup
  39. *
  40. * @var string
  41. */
  42. protected $childOptgroup = ".//following-sibling::optgroup[%d][@label='%s']";
  43. /**
  44. * Locator for search parent optgroup
  45. *
  46. * @var string
  47. */
  48. protected $parentOptgroup = 'optgroup[option[text()="%s"]]';
  49. /**
  50. * Locator for search preceding sibling optgroup
  51. *
  52. * @var string
  53. */
  54. protected $precedingOptgroup = '/preceding-sibling::optgroup[1][substring(@label,1,%d)="%s"]';
  55. /**
  56. * Locator for option
  57. *
  58. * @var string
  59. */
  60. protected $option = './/option[text()="%s"]';
  61. /**
  62. * Locator search for option by number
  63. *
  64. * @var string
  65. */
  66. protected $childOptionByNumber = './/optgroup[%d]/option[%d]';
  67. /**
  68. * Locator search for option by data-text attribute
  69. *
  70. * @var string
  71. */
  72. protected $uiOptionText = './/option[@data-title="%s"]';
  73. /**
  74. * Locator for search parent option
  75. *
  76. * @var string
  77. */
  78. protected $optionByNumber = './option[%d]';
  79. /**
  80. * Indent, four symbols non breaking space
  81. *
  82. * @var string
  83. */
  84. protected $indent = "\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0";
  85. /**
  86. * Trim symbols
  87. *
  88. * @var string
  89. */
  90. protected $trim = "\xC2\xA0 ";
  91. /**
  92. * Set values
  93. *
  94. * @param array|string $values
  95. * @return void
  96. */
  97. public function setValue($values)
  98. {
  99. $this->deselectAll();
  100. $values = is_array($values) ? $values : [$values];
  101. foreach ($values as $value) {
  102. $this->selectOption($value);
  103. }
  104. }
  105. /**
  106. * Select option
  107. *
  108. * @param string $option
  109. * @return void
  110. * @throws \Exception
  111. */
  112. protected function selectOption($option)
  113. {
  114. $optionElement = $this->find(sprintf($this->uiOptionText, $option), Locator::SELECTOR_XPATH);
  115. if ($optionElement->isVisible()) {
  116. if (!$optionElement->isSelected()) {
  117. $optionElement->click();
  118. }
  119. return;
  120. }
  121. $isOptgroup = false;
  122. $optgroupIndent = '';
  123. $values = explode('/', $option);
  124. $context = $this;
  125. foreach ($values as $value) {
  126. $optionIndent = $isOptgroup ? $this->indent : '';
  127. $optionElement = $context->find(sprintf($this->option, $optionIndent . $value), Locator::SELECTOR_XPATH);
  128. if ($optionElement->isVisible()) {
  129. if (!$optionElement->isSelected()) {
  130. $optionElement->click();
  131. }
  132. return;
  133. }
  134. $value = $optgroupIndent . $value;
  135. $optgroupIndent .= $this->indent;
  136. if ($isOptgroup) {
  137. $context = $this->getChildOptgroup($value, $context);
  138. } else {
  139. $context = $this->getOptgroup($value, $context);
  140. $isOptgroup = true;
  141. }
  142. }
  143. throw new \Exception("Can't find option \"{$option}\".");
  144. }
  145. /**
  146. * Get optgroup
  147. *
  148. * @param string $value
  149. * @param ElementInterface $context
  150. * @return ElementInterface
  151. * @throws \Exception
  152. */
  153. protected function getOptgroup($value, ElementInterface $context)
  154. {
  155. $optgroup = $context->find(sprintf($this->optgroupByLabel, $value), Locator::SELECTOR_XPATH);
  156. if (!$optgroup->isVisible()) {
  157. throw new \Exception("Can't find group \"{$value}\".");
  158. }
  159. return $optgroup;
  160. }
  161. /**
  162. * Get child optgroup
  163. *
  164. * @param string $value
  165. * @param ElementInterface $context
  166. * @return ElementInterface
  167. * @throws \Exception
  168. */
  169. protected function getChildOptgroup($value, ElementInterface $context)
  170. {
  171. $childOptgroup = null;
  172. $count = 1;
  173. while (!$childOptgroup) {
  174. $optgroup = $context->find(sprintf($this->nextOptgroup, $count), Locator::SELECTOR_XPATH);
  175. if (!$optgroup->isVisible()) {
  176. throw new \Exception("Can't find child group \"{$value}\"");
  177. }
  178. $childOptgroup = $context->find(
  179. sprintf($this->childOptgroup, $count, $value),
  180. Locator::SELECTOR_XPATH
  181. );
  182. if (!$childOptgroup->isVisible()) {
  183. $childOptgroup = null;
  184. }
  185. ++$count;
  186. }
  187. return $childOptgroup;
  188. }
  189. /**
  190. * Get value
  191. *
  192. * @return array
  193. * @SuppressWarnings(PHPMD.NPathComplexity)
  194. */
  195. public function getValue()
  196. {
  197. $values = [];
  198. $indentOption = str_repeat(' ', self::INDENT_LENGTH);
  199. foreach ($this->getSelectedOptions() as $option) {
  200. $value = [];
  201. /** @var ElementInterface $option */
  202. $optionText = $option->getText();
  203. $optionValue = trim($optionText, $this->trim);
  204. $value[] = $optionValue;
  205. if (0 !== strpos($optionText, $indentOption)) {
  206. $values[] = implode('/', $value);
  207. continue;
  208. }
  209. $pathOptgroup = sprintf($this->parentOptgroup, $this->indent . $optionValue);
  210. $optgroup = $this->find($pathOptgroup, Locator::SELECTOR_XPATH);
  211. $optgroupText = $optgroup->getAttribute('label');
  212. $optgroupValue = trim($optgroupText, $this->trim);
  213. $amountIndent = strlen($optgroupText) - strlen($optgroupValue);
  214. $amountIndent = $amountIndent ? ($amountIndent / strlen($this->indent)) : 0;
  215. $value[] = $optgroupValue;
  216. if (0 == $amountIndent) {
  217. $values[] = implode('/', $value);
  218. continue;
  219. }
  220. --$amountIndent;
  221. $indent = $amountIndent ? str_repeat($this->indent, $amountIndent) : '';
  222. $pathOptgroup .= sprintf($this->precedingOptgroup, $amountIndent * self::INDENT_LENGTH, $indent);
  223. while (0 <= $amountIndent && $this->find($pathOptgroup, Locator::SELECTOR_XPATH)->isVisible()) {
  224. $optgroup = $this->find($pathOptgroup, Locator::SELECTOR_XPATH);
  225. $optgroupText = $optgroup->getAttribute('label');
  226. $optgroupValue = trim($optgroupText, $this->trim);
  227. $value[] = $optgroupValue;
  228. --$amountIndent;
  229. $indent = (0 < $amountIndent) ? str_repeat($this->indent, $amountIndent) : '';
  230. $pathOptgroup .= sprintf($this->precedingOptgroup, $amountIndent * self::INDENT_LENGTH, $indent);
  231. }
  232. $values[] = implode('/', array_reverse($value));
  233. }
  234. return $values;
  235. }
  236. /**
  237. * Get options
  238. *
  239. * @return ElementInterface[]
  240. */
  241. protected function getOptions()
  242. {
  243. $options = [];
  244. $countOption = 1;
  245. $option = $this->find(sprintf($this->optionByNumber, $countOption), Locator::SELECTOR_XPATH);
  246. while ($option->isVisible()) {
  247. $options[] = $option;
  248. ++$countOption;
  249. $option = $this->find(sprintf($this->optionByNumber, $countOption), Locator::SELECTOR_XPATH);
  250. }
  251. $countOptgroup = 1;
  252. $optgroup = $this->find(sprintf($this->optgroupByNumber, $countOptgroup), Locator::SELECTOR_XPATH);
  253. while ($optgroup->isVisible()) {
  254. $countOption = 1;
  255. $option = $this->find(
  256. sprintf($this->childOptionByNumber, $countOptgroup, $countOption),
  257. Locator::SELECTOR_XPATH
  258. );
  259. while ($option->isVisible()) {
  260. $options[] = $option;
  261. ++$countOption;
  262. $option = $this->find(
  263. sprintf($this->childOptionByNumber, $countOptgroup, $countOption),
  264. Locator::SELECTOR_XPATH
  265. );
  266. }
  267. ++$countOptgroup;
  268. $optgroup = $this->find(sprintf($this->optgroupByNumber, $countOptgroup), Locator::SELECTOR_XPATH);
  269. }
  270. return $options;
  271. }
  272. /**
  273. * Get selected options
  274. *
  275. * @return array
  276. */
  277. protected function getSelectedOptions()
  278. {
  279. $options = [];
  280. foreach ($this->getOptions() as $option) {
  281. if ($option->isSelected()) {
  282. $options[] = $option;
  283. }
  284. }
  285. return $options;
  286. }
  287. }