Tree.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Client\Element;
  7. use Magento\Mtf\Client\ElementInterface;
  8. use Magento\Mtf\Client\Locator;
  9. /**
  10. * General class for tree elements. Holds general implementation of methods, which overrides in child classes.
  11. */
  12. abstract class Tree extends SimpleElement
  13. {
  14. /**
  15. * Selected checkboxes.
  16. *
  17. * @var string
  18. */
  19. protected $selectedLabels;
  20. /**
  21. * Pattern for child element node.
  22. *
  23. * @var string
  24. */
  25. protected $pattern;
  26. /**
  27. * Selector for child loader.
  28. *
  29. * @var string
  30. */
  31. protected $childLoader = 'ul';
  32. /**
  33. * Selector for input.
  34. *
  35. * @var string
  36. */
  37. protected $input;
  38. /**
  39. * Selector for parent element.
  40. *
  41. * @var string
  42. */
  43. protected $parentElement;
  44. /**
  45. * Display children.
  46. *
  47. * @param string $element
  48. * @return void
  49. */
  50. abstract protected function displayChildren($element);
  51. /**
  52. * Get element label.
  53. *
  54. * @param ElementInterface $element
  55. * @return string
  56. */
  57. abstract protected function getElementLabel(ElementInterface $element);
  58. /**
  59. * Drag and drop element to(between) another element(s).
  60. *
  61. * @param ElementInterface $target
  62. * @throws \Exception
  63. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  64. */
  65. public function dragAndDrop(ElementInterface $target)
  66. {
  67. throw new \Exception('Not applicable for this class of elements (TreeElement)');
  68. }
  69. /**
  70. * keys method is not accessible in this class.
  71. * Throws exception if used.
  72. *
  73. * @param array $keys
  74. * @throws \Exception
  75. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  76. */
  77. public function keys(array $keys)
  78. {
  79. throw new \Exception('Not applicable for this class of elements (TreeElement)');
  80. }
  81. /**
  82. * Click a tree element by its path (Node names) in tree.
  83. *
  84. * @param string $path
  85. */
  86. public function setValue($path)
  87. {
  88. $this->eventManager->dispatchEvent(['set_value'], [(string)$this->getAbsoluteSelector()]);
  89. $elementSelector = $this->prepareElementSelector($path);
  90. $elements = $this->getElements('.' . $elementSelector . $this->input, Locator::SELECTOR_XPATH);
  91. foreach ($elements as $element) {
  92. $element->click();
  93. }
  94. }
  95. /**
  96. * Get the value.
  97. *
  98. * @return array
  99. */
  100. public function getValue()
  101. {
  102. $this->eventManager->dispatchEvent(['get_value'], [(string)$this->getAbsoluteSelector()]);
  103. $checkboxes = $this->getElements($this->selectedLabels, Locator::SELECTOR_XPATH);
  104. return $this->prepareValues($checkboxes);
  105. }
  106. /**
  107. * Prepare values for checked checkboxes.
  108. *
  109. * @param ElementInterface[] $checkboxes
  110. * @return array
  111. */
  112. protected function prepareValues(array $checkboxes)
  113. {
  114. $values = [];
  115. foreach ($checkboxes as $checkbox) {
  116. $fullPath = $this->getFullPath($checkbox);
  117. $values[] = implode('/', array_reverse($fullPath));
  118. }
  119. return $values;
  120. }
  121. /**
  122. * Prepare element selector.
  123. *
  124. * @param string $path
  125. * @return string
  126. */
  127. protected function prepareElementSelector($path)
  128. {
  129. $pathArray = explode('/', $path);
  130. $elementSelector = '';
  131. foreach ($pathArray as $itemElement) {
  132. $this->displayChildren($itemElement);
  133. $elementSelector .= sprintf($this->pattern, $itemElement);
  134. }
  135. return $elementSelector;
  136. }
  137. /**
  138. * Check visible element.
  139. *
  140. * @param string $path
  141. * @return bool
  142. */
  143. public function isElementVisible($path)
  144. {
  145. $elementSelector = $this->prepareElementSelector($path);
  146. return $this->find($elementSelector, Locator::SELECTOR_XPATH)->isVisible();
  147. }
  148. /**
  149. * Waiter for load children.
  150. *
  151. * @param ElementInterface $element
  152. * @return void
  153. */
  154. protected function waitLoadChildren(ElementInterface $element)
  155. {
  156. $selector = $this->childLoader;
  157. $this->waitUntil(
  158. function () use ($element, $selector) {
  159. return $element->find($selector)->isVisible() ? true : null;
  160. }
  161. );
  162. }
  163. /**
  164. * Get full path for element.
  165. *
  166. * @param ElementInterface $element
  167. * @return string[]
  168. */
  169. protected function getFullPath(ElementInterface $element)
  170. {
  171. $fullPath[] = $this->getElementLabel($element);
  172. $parentElement = $element->find($this->parentElement, Locator::SELECTOR_XPATH);
  173. if ($parentElement->isVisible()) {
  174. $fullPath = array_merge($fullPath, $this->getFullPath($parentElement));
  175. }
  176. return $fullPath;
  177. }
  178. }