OptgroupselectElement.php 2.2 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. use Magento\Mtf\Client\ElementInterface;
  9. /**
  10. * Typified element class for option group selectors.
  11. */
  12. class OptgroupselectElement extends SelectElement
  13. {
  14. /**
  15. * Option group selector.
  16. *
  17. * @var string
  18. */
  19. protected $optGroup = 'optgroup[option[contains(.,"%s")]]';
  20. /**
  21. * Option group locator.
  22. *
  23. * @var string
  24. */
  25. protected $optGroupValue = ".//optgroup[@label = '%s']/option[text() = '%s']";
  26. /**
  27. * Get the value of form element.
  28. *
  29. * @return string
  30. * @throws \Exception
  31. */
  32. public function getValue()
  33. {
  34. $this->eventManager->dispatchEvent(['get_value'], [(string)$this->getAbsoluteSelector()]);
  35. $selectedLabel = parent::getValue();
  36. if ($selectedLabel == '') {
  37. throw new \Exception('Selected value has not been found in optgroup select.');
  38. }
  39. $element = $this->find(sprintf($this->optGroup, $selectedLabel), Locator::SELECTOR_XPATH);
  40. $value = $this->getData($element);
  41. $value .= '/' . $selectedLabel;
  42. return $value;
  43. }
  44. /**
  45. * Get element data.
  46. *
  47. * @param ElementInterface $element
  48. * @return string
  49. */
  50. protected function getData(ElementInterface $element)
  51. {
  52. return trim($element->getAttribute('label'), chr(0xC2) . chr(0xA0));
  53. }
  54. /**
  55. * Select value in dropdown which has option groups.
  56. *
  57. * @param string $value
  58. * @return void
  59. */
  60. public function setValue($value)
  61. {
  62. $option = $this->prepareSetValue($value);
  63. $option->click();
  64. }
  65. /**
  66. * Prepare setValue.
  67. *
  68. * @param string $value
  69. * @return ElementInterface
  70. */
  71. protected function prepareSetValue($value)
  72. {
  73. $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
  74. list($group, $option) = explode('/', $value);
  75. $xpath = sprintf($this->optGroupValue, $group, $option);
  76. $option = $this->find($xpath, Locator::SELECTOR_XPATH);
  77. return $option;
  78. }
  79. }