SuggestElement.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * General class for suggest elements.
  10. */
  11. class SuggestElement extends SimpleElement
  12. {
  13. /**
  14. * "Backspace" key code.
  15. */
  16. const BACKSPACE = "\xEE\x80\x83";
  17. /**
  18. * Selector for advanced select element.
  19. *
  20. * @var string
  21. */
  22. protected $advancedSelect = '[data-role="advanced-select"]';
  23. /**
  24. * Selector for select input element.
  25. *
  26. * @var string
  27. */
  28. protected $selectInput = '[data-role="advanced-select-text"]';
  29. /**
  30. * Selector search result.
  31. *
  32. * @var string
  33. */
  34. protected $searchResult = '.mage-suggest-dropdown';
  35. /**
  36. * Selector item of search result.
  37. *
  38. * @var string
  39. */
  40. protected $resultItem = './/ul/li/a[text()="%s"]';
  41. /**
  42. * Search label.
  43. *
  44. * @var string
  45. */
  46. protected $searchLabel = '[data-action="advanced-select-search"]';
  47. /**
  48. * Close button.
  49. *
  50. * @var string
  51. */
  52. protected $closeButton = '[data-action="close-advanced-select"]';
  53. /**
  54. * Searched count.
  55. *
  56. * @var string
  57. */
  58. protected $searchedCount = '[class*=search-count]';
  59. /**
  60. * Set value.
  61. *
  62. * @param string $value
  63. * @return void
  64. */
  65. public function setValue($value)
  66. {
  67. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  68. $this->clear();
  69. if ($value == '') {
  70. return;
  71. }
  72. $this->keys([$value]);
  73. $searchedItem = $this->find(sprintf($this->resultItem, $value), Locator::SELECTOR_XPATH);
  74. $searchedCountElements = $this->find($this->searchedCount);
  75. $this->waitUntil(
  76. function () use ($searchedCountElements) {
  77. return $searchedCountElements->isVisible() ? true : null;
  78. }
  79. );
  80. $searchedItem->click();
  81. $closeButton = $this->find($this->closeButton);
  82. if ($closeButton->isVisible()) {
  83. $closeButton->click();
  84. }
  85. }
  86. /**
  87. * Send keys.
  88. *
  89. * @param array $keys
  90. * @return void
  91. */
  92. public function keys(array $keys)
  93. {
  94. if (!$this->find($this->selectInput)->isVisible()) {
  95. $this->find($this->advancedSelect)->click();
  96. }
  97. $input = $this->find($this->selectInput);
  98. $input->click();
  99. $input->keys($keys);
  100. $this->searchResult();
  101. }
  102. /**
  103. * Clear value of element.
  104. *
  105. * @return void
  106. */
  107. protected function clear()
  108. {
  109. $element = $this->find($this->advancedSelect);
  110. while ($element->getValue() != '') {
  111. $element->keys([self::BACKSPACE]);
  112. }
  113. }
  114. /**
  115. * Search category result.
  116. *
  117. * @return void
  118. */
  119. public function searchResult()
  120. {
  121. $this->find($this->searchLabel)->click();
  122. }
  123. /**
  124. * Get value.
  125. *
  126. * @return string
  127. */
  128. public function getValue()
  129. {
  130. $this->eventManager->dispatchEvent(['get_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  131. return $this->find($this->advancedSelect)->getValue();
  132. }
  133. /**
  134. * Checking exist value in search result.
  135. *
  136. * @param string $value
  137. * @return bool
  138. */
  139. public function isExistValueInSearchResult($value)
  140. {
  141. $needle = $this->find($this->searchResult)->find(sprintf($this->resultItem, $value), Locator::SELECTOR_XPATH);
  142. $keys = str_split($value);
  143. $this->keys($keys);
  144. if ($needle->isVisible()) {
  145. try {
  146. return true;
  147. } catch (\Exception $e) {
  148. // In parallel run on windows change the focus is lost on element
  149. // that causes disappearing of attribute suggest list.
  150. }
  151. }
  152. return false;
  153. }
  154. }