DropdownmultiselectElement.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 select with checkboxes.
  10. */
  11. class DropdownmultiselectElement extends MultiselectElement
  12. {
  13. /**
  14. * Selector for expanding dropdown.
  15. *
  16. * @var string
  17. */
  18. protected $toggle = 'div';
  19. /**
  20. * Selected option selector.
  21. *
  22. * @var string
  23. */
  24. protected $selectedValue = 'li._selected';
  25. /**
  26. * Option locator by value.
  27. *
  28. * @var string
  29. */
  30. protected $optionByValue = './/li//label[contains(normalize-space(.), %s)]';
  31. /**
  32. * Set values.
  33. *
  34. * @param array|string $values
  35. * @return void
  36. */
  37. public function setValue($values)
  38. {
  39. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  40. $this->find($this->toggle)->click();
  41. $this->deselectAll();
  42. $values = is_array($values) ? $values : [$values];
  43. foreach ($values as $value) {
  44. $this->find(
  45. sprintf($this->optionByValue, $this->escapeQuotes($value)),
  46. Locator::SELECTOR_XPATH
  47. )->click();
  48. }
  49. $this->find($this->toggle)->click();
  50. }
  51. /**
  52. * Get values.
  53. *
  54. * @return array
  55. */
  56. public function getValue()
  57. {
  58. $this->eventManager->dispatchEvent(['get_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  59. $values = [];
  60. $this->find($this->toggle)->click();
  61. $options = $this->getElements($this->selectedValue);
  62. foreach ($options as $option) {
  63. $values[] = $option->getText();
  64. }
  65. $this->find($this->toggle)->click();
  66. return $values;
  67. }
  68. /**
  69. * Deselect all options in the element.
  70. *
  71. * @return void
  72. */
  73. public function deselectAll()
  74. {
  75. $options = $this->getElements($this->selectedValue);
  76. /** @var SimpleElement $option */
  77. foreach ($options as $option) {
  78. $option->click();
  79. }
  80. }
  81. }