MultisuggestElement.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * Typified element class for multi suggest element.
  10. */
  11. class MultisuggestElement extends SuggestElement
  12. {
  13. /**
  14. * Selector category choice
  15. *
  16. * @var string
  17. */
  18. protected $categoryChoice = '//div[contains(@data-index, "category_ids")]';
  19. /**
  20. * Selector choice item
  21. *
  22. * @var string
  23. */
  24. protected $choice = './/li/div[text()="%s"]/..';
  25. /**
  26. * Selector choice value
  27. *
  28. * @var string
  29. */
  30. protected $choiceValue = './/span[contains(@class,"admin__action-multiselect-crumb")]/span';
  31. /**
  32. * Selector remove choice item
  33. *
  34. * @var string
  35. */
  36. protected $choiceClose = '[data-action="remove-selected-item"]';
  37. /**
  38. * Set value
  39. *
  40. * @param array|string $values
  41. * @return void
  42. */
  43. public function setValue($values)
  44. {
  45. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  46. $this->clear();
  47. foreach ((array)$values as $value) {
  48. if (!$this->isChoice($value)) {
  49. parent::setValue($value);
  50. }
  51. }
  52. }
  53. /**
  54. * Get value
  55. *
  56. * @return array
  57. */
  58. public function getValue()
  59. {
  60. $this->eventManager->dispatchEvent(['get_value'], [(string) $this->getAbsoluteSelector()]);
  61. $categoryChoice = $this->find($this->categoryChoice, Locator::SELECTOR_XPATH);
  62. $choices = $categoryChoice->getElements($this->choiceValue, Locator::SELECTOR_XPATH);
  63. $values = [];
  64. foreach ($choices as $choice) {
  65. /** @var \Magento\Mtf\Client\ElementInterface $choice */
  66. $values[] = $choice->getText();
  67. }
  68. return $values;
  69. }
  70. /**
  71. * Check exist selected item
  72. *
  73. * @param string $value
  74. * @return bool
  75. */
  76. protected function isChoice($value)
  77. {
  78. return $this->find(sprintf($this->choice, $value), Locator::SELECTOR_XPATH)->isVisible();
  79. }
  80. /**
  81. * Clear element
  82. *
  83. * @return void
  84. */
  85. protected function clear()
  86. {
  87. $choiceClose = $this->find($this->choiceClose);
  88. while ($choiceClose->isVisible()) {
  89. $choiceClose->click();
  90. $choiceClose = $this->find($this->choiceClose);
  91. }
  92. }
  93. }