SelectstoreElement.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 option group selectors.
  10. */
  11. class SelectstoreElement extends SelectElement
  12. {
  13. /**
  14. * Store option group selector.
  15. *
  16. * @var string
  17. */
  18. protected $storeGroup = 'optgroup[option[contains(.,"%s")]]';
  19. /**
  20. * Website option group selector.
  21. *
  22. * @var string
  23. */
  24. protected $website = 'optgroup[following-sibling::optgroup[option[contains(.,"%s")]]]';
  25. /**
  26. * Get the value of form element.
  27. *
  28. * @return string
  29. */
  30. public function getValue()
  31. {
  32. $selectedLabel = trim(parent::getValue());
  33. $element = $this->find(sprintf($this->website, $selectedLabel), Locator::SELECTOR_XPATH);
  34. $value = trim($element->getAttribute('label'));
  35. $element = $this->find(sprintf($this->storeGroup, $selectedLabel), Locator::SELECTOR_XPATH);
  36. $value .= '/' . trim($element->getAttribute('label'), chr(0xC2) . chr(0xA0));
  37. $value .= '/' . $selectedLabel;
  38. return $value;
  39. }
  40. /**
  41. * Select value in dropdown which has option groups.
  42. *
  43. * @param string $value
  44. * @throws \Exception
  45. * @return void
  46. */
  47. public function setValue($value)
  48. {
  49. $pieces = explode('/', $value);
  50. if (1 == count($pieces)) {
  51. $optionLocator = './/option[contains(text(),"' . $pieces[0] . '")]';
  52. } else {
  53. $optionLocator = './/optgroup[contains(@label,"'
  54. . $pieces[0] . '")]/following-sibling::optgroup[contains(@label,"'
  55. . $pieces[1] . '")]/option[contains(text(), "'
  56. . $pieces[2] . '")]';
  57. }
  58. $option = $this->context->find($optionLocator, Locator::SELECTOR_XPATH);
  59. if (!$option->isVisible()) {
  60. throw new \Exception('[' . implode('/', $pieces) . '] option is not visible in store switcher.');
  61. }
  62. $option->click();
  63. }
  64. }