SwitcherElement.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. * Toggle element in the backend.
  10. * Switches value between YES and NO.
  11. */
  12. class SwitcherElement extends SimpleElement
  13. {
  14. /**
  15. * XPath locator of the parent container.
  16. *
  17. * @var string
  18. */
  19. protected $parentContainer = 'parent::div[@data-role="switcher"]';
  20. /**
  21. * XPath selector for label text on switcher element.
  22. *
  23. * @var string
  24. */
  25. private $labelText = './following-sibling::label';
  26. /**
  27. * Set value to Yes or No.
  28. *
  29. * @param string $value Yes|No
  30. * @return void
  31. */
  32. public function setValue($value)
  33. {
  34. if (($value != 'Yes') && ($value != 'No')) {
  35. throw new \UnexpectedValueException(
  36. sprintf('Switcher element accepts only "Yes" and "No" values.')
  37. );
  38. }
  39. if ($value != $this->getValue()) {
  40. $this->find($this->labelText, Locator::SELECTOR_XPATH)->click();
  41. }
  42. }
  43. /**
  44. * Get the current value.
  45. *
  46. * @return string 'Yes'|'No'
  47. * @throws \Exception
  48. */
  49. public function getValue()
  50. {
  51. if ($this->find($this->parentContainer, 'xpath')->find('input:checked')->isVisible()) {
  52. return 'Yes';
  53. } elseif ($this->find($this->parentContainer, 'xpath')->find('input')->isVisible()) {
  54. return 'No';
  55. } else {
  56. throw new \Exception(
  57. sprintf('Element %s not found on page', $this->getLocator())
  58. );
  59. }
  60. }
  61. }