GlobalsearchElement.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. use Magento\Mtf\Client\ElementInterface;
  9. /**
  10. * Typified element class for global search element.
  11. */
  12. class GlobalsearchElement extends SimpleElement
  13. {
  14. /**
  15. * Search icon selector.
  16. *
  17. * @var string
  18. */
  19. protected $searchIcon = '[for="search-global"]';
  20. /**
  21. * Locator for initialized suggest container.
  22. *
  23. * @var string
  24. */
  25. protected $initializedSuggest = './/*[contains(@class,"search-global-field") and .//*[@class="mage-suggest"]]';
  26. /**
  27. * Selector for search input element.
  28. *
  29. * @var string
  30. */
  31. protected $searchInput = '#search-global';
  32. /**
  33. * Result dropdown selector.
  34. *
  35. * @var string
  36. */
  37. protected $searchResult = '.autocomplete-results';
  38. /**
  39. * Item selector of search result.
  40. *
  41. * @var string
  42. */
  43. protected $resultItem = 'li';
  44. /**
  45. * Set value.
  46. *
  47. * @param string $value
  48. * @return void
  49. */
  50. public function setValue($value)
  51. {
  52. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  53. $this->waitInitElement();
  54. if (!$this->find($this->searchInput)->isVisible()) {
  55. $this->find($this->searchIcon)->click();
  56. }
  57. $this->find($this->searchInput)->keys(str_split($value));
  58. $this->waitResult();
  59. }
  60. /**
  61. * Wait init search suggest container.
  62. *
  63. * @return void
  64. * @throws \Exception
  65. */
  66. protected function waitInitElement()
  67. {
  68. $selector = $this->initializedSuggest;
  69. $browser = $this->driver;
  70. $this->driver->waitUntil(
  71. function () use ($browser, $selector) {
  72. return $browser->find($selector, Locator::SELECTOR_XPATH)->isVisible() ? true : null;
  73. }
  74. );
  75. }
  76. /**
  77. * Wait for search result is visible.
  78. *
  79. * @return void
  80. */
  81. public function waitResult()
  82. {
  83. $selector = $this->searchResult;
  84. $browser = $this->driver;
  85. $this->driver->waitUntil(
  86. function () use ($browser, $selector) {
  87. return $browser->find($selector)->isVisible() ? true : null;
  88. }
  89. );
  90. }
  91. /**
  92. * Get value.
  93. *
  94. * @throws \BadMethodCallException
  95. */
  96. public function getValue()
  97. {
  98. throw new \BadMethodCallException('Not applicable for this class of elements (GlobalSearch)');
  99. }
  100. /**
  101. * Checking exist value in search result.
  102. *
  103. * @param string $value
  104. * @return bool
  105. */
  106. public function isExistValueInSearchResult($value)
  107. {
  108. $searchResult = $this->find($this->searchResult);
  109. if (!$searchResult->isVisible()) {
  110. return false;
  111. }
  112. $searchResults = $this->getSearchResults();
  113. return in_array($value, $searchResults);
  114. }
  115. /**
  116. * Get search results.
  117. *
  118. * @return array
  119. */
  120. protected function getSearchResults()
  121. {
  122. /** @var ElementInterface $searchResult */
  123. $searchResult = $this->find($this->searchResult);
  124. $resultItems = $searchResult->getElements($this->resultItem);
  125. $resultArray = [];
  126. /** @var ElementInterface $resultItem */
  127. foreach ($resultItems as $resultItem) {
  128. $resultItemLink = $resultItem->find('a');
  129. $resultText = $resultItemLink->isVisible()
  130. ? trim($resultItemLink->getText())
  131. : trim($resultItem->getText());
  132. $resultArray[] = $resultText;
  133. }
  134. return $resultArray;
  135. }
  136. }