LiselectstoreElement.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\Locator;
  8. /**
  9. * Class LiselectstoreElement
  10. * Typified element class for lists selectors
  11. */
  12. class LiselectstoreElement extends SimpleElement
  13. {
  14. /**
  15. * Template for each element of option
  16. *
  17. * @var string
  18. */
  19. protected $optionMaskElement = 'li[*[contains(text(), "%s")]]';
  20. /**
  21. * Additional part for each child element of option
  22. *
  23. * @var string
  24. */
  25. protected $optionMaskFollowing = '/following-sibling::';
  26. /**
  27. * Website selector
  28. *
  29. * @var string
  30. */
  31. protected $websiteSelector = '(.//li[a[@data-role="website-id"]])[%d]';
  32. /**
  33. * Store selector
  34. *
  35. * @var string
  36. */
  37. protected $storeSelector = '(.//li[@class = "store-switcher-store disabled"])[%d]';
  38. /**
  39. * StoreView selector
  40. *
  41. * @var string
  42. */
  43. protected $storeViewSelector = './/li[a[@data-role="store-view-id"]]';
  44. /**
  45. * Toggle element selector
  46. *
  47. * @var string
  48. */
  49. protected $toggleSelector = '.admin__action-dropdown[data-toggle="dropdown"]';
  50. /**
  51. * Select value in liselect dropdown
  52. *
  53. * @param string $value
  54. * @throws \Exception
  55. */
  56. public function setValue($value)
  57. {
  58. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  59. $this->context->find($this->toggleSelector)->click();
  60. $value = explode('/', $value);
  61. $optionSelector = [];
  62. foreach ($value as $key => $option) {
  63. $optionSelector[] = sprintf($this->optionMaskElement, $value[$key]);
  64. }
  65. $optionSelector = './/' . implode($this->optionMaskFollowing, $optionSelector) . '/a';
  66. $option = $this->context->find($optionSelector, Locator::SELECTOR_XPATH);
  67. if (!$option->isVisible()) {
  68. throw new \Exception('[' . implode('/', $value) . '] option is not visible in store switcher.');
  69. }
  70. $option->click();
  71. }
  72. /**
  73. * Get all li elements from dropdown
  74. *
  75. * @return array
  76. */
  77. protected function getLiElements()
  78. {
  79. $this->find($this->toggleSelector)->click();
  80. $elements = $this->driver->getElements($this, 'li', Locator::SELECTOR_TAG_NAME);
  81. $dropdownData = [];
  82. foreach ($elements as $element) {
  83. $class = $element->getAttribute('class');
  84. $dropdownData[] = [
  85. 'element' => $element,
  86. 'storeView' => $this->isSubstring($class, "store-switcher-store-view"),
  87. 'store' => $this->isSubstring($class, "store-switcher-store "),
  88. 'website' => $this->isSubstring($class, "store-switcher-website"),
  89. 'current' => $this->isSubstring($class, "current"),
  90. 'default_config' => $this->isSubstring($class, "store-switcher-all"),
  91. ];
  92. }
  93. return $dropdownData;
  94. }
  95. /**
  96. * Get all available store views
  97. *
  98. * @return array
  99. */
  100. public function getValues()
  101. {
  102. $dropdownData = $this->getLiElements();
  103. $data = [];
  104. foreach ($dropdownData as $key => $dropdownElement) {
  105. if ($dropdownElement['storeView']) {
  106. $data[] = $this->findNearestElement('website', $key, $dropdownData) . "/"
  107. . $this->findNearestElement('store', $key, $dropdownData) . "/"
  108. . $dropdownElement['element']->getText();
  109. }
  110. }
  111. return $data;
  112. }
  113. /**
  114. * Check if string contains substring
  115. *
  116. * @param string $haystack
  117. * @param string $pattern
  118. * @return bool
  119. */
  120. protected function isSubstring($haystack, $pattern)
  121. {
  122. return preg_match("/$pattern/", $haystack) != 0 ? true : false;
  123. }
  124. /**
  125. * Return nearest elements name according to criteria
  126. *
  127. * @param string $criteria
  128. * @param string $key
  129. * @param array $elements
  130. * @return bool
  131. */
  132. protected function findNearestElement($criteria, $key, array $elements)
  133. {
  134. $elementText = false;
  135. while ($elementText == false) {
  136. $elementText = $elements[$key][$criteria] == true ? $elements[$key]['element']->getText() : false;
  137. $key--;
  138. }
  139. return $elementText;
  140. }
  141. /**
  142. * Get selected store value
  143. *
  144. * @throws \Exception
  145. * @return string
  146. */
  147. public function getValue()
  148. {
  149. $this->eventManager->dispatchEvent(['get_value'], [$this->getAbsoluteSelector()]);
  150. $elements = $this->getLiElements();
  151. foreach ($elements as $key => $element) {
  152. if ($element['current'] == true) {
  153. if ($element['default_config'] == true) {
  154. return $element['element']->getText();
  155. }
  156. $path = $this->findNearestElement('website', $key, $elements) . "/"
  157. . $this->findNearestElement('store', $key, $elements) . "/"
  158. . $element['element']->getText();
  159. return $path;
  160. }
  161. }
  162. return '';
  163. }
  164. }