Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LiselectstoreElement.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Client\Locator;
10 
15 class LiselectstoreElement extends SimpleElement
16 {
22  protected $optionMaskElement = 'li[*[contains(text(), "%s")]]';
23 
29  protected $optionMaskFollowing = '/following-sibling::';
30 
36  protected $websiteSelector = '(.//li[a[@data-role="website-id"]])[%d]';
37 
43  protected $storeSelector = '(.//li[@class = "store-switcher-store disabled"])[%d]';
44 
50  protected $storeViewSelector = './/li[a[@data-role="store-view-id"]]';
51 
57  protected $toggleSelector = '.admin__action-dropdown[data-toggle="dropdown"]';
58 
65  public function setValue($value)
66  {
67  $this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
68  $this->context->find($this->toggleSelector)->click();
69 
70  $value = explode('/', $value);
71  $optionSelector = [];
72  foreach ($value as $key => $option) {
73  $optionSelector[] = sprintf($this->optionMaskElement, $value[$key]);
74  }
75  $optionSelector = './/' . implode($this->optionMaskFollowing, $optionSelector) . '/a';
76 
77  $option = $this->context->find($optionSelector, Locator::SELECTOR_XPATH);
78  if (!$option->isVisible()) {
79  throw new \Exception('[' . implode('/', $value) . '] option is not visible in store switcher.');
80  }
81  $option->click();
82  }
83 
89  protected function getLiElements()
90  {
91  $this->find($this->toggleSelector)->click();
92  $elements = $this->driver->getElements($this, 'li', Locator::SELECTOR_TAG_NAME);
93  $dropdownData = [];
94  foreach ($elements as $element) {
95  $class = $element->getAttribute('class');
96  $dropdownData[] = [
97  'element' => $element,
98  'storeView' => $this->isSubstring($class, "store-switcher-store-view"),
99  'store' => $this->isSubstring($class, "store-switcher-store "),
100  'website' => $this->isSubstring($class, "store-switcher-website"),
101  'current' => $this->isSubstring($class, "current"),
102  'default_config' => $this->isSubstring($class, "store-switcher-all"),
103  ];
104  }
105  return $dropdownData;
106  }
107 
113  public function getValues()
114  {
115  $dropdownData = $this->getLiElements();
116  $data = [];
117  foreach ($dropdownData as $key => $dropdownElement) {
118  if ($dropdownElement['storeView']) {
119  $data[] = $this->findNearestElement('website', $key, $dropdownData) . "/"
120  . $this->findNearestElement('store', $key, $dropdownData) . "/"
121  . $dropdownElement['element']->getText();
122  }
123  }
124  return $data;
125  }
126 
134  protected function isSubstring($haystack, $pattern)
135  {
136  return preg_match("/$pattern/", $haystack) != 0 ? true : false;
137  }
138 
147  protected function findNearestElement($criteria, $key, array $elements)
148  {
149  $elementText = false;
150  while ($elementText == false) {
151  $elementText = $elements[$key][$criteria] == true ? $elements[$key]['element']->getText() : false;
152  $key--;
153  }
154  return $elementText;
155  }
156 
163  public function getValue()
164  {
165  $this->eventManager->dispatchEvent(['get_value'], [$this->getAbsoluteSelector()]);
166  $elements = $this->getLiElements();
167  foreach ($elements as $key => $element) {
168  if ($element['current'] == true) {
169  if ($element['default_config'] == true) {
170  return $element['element']->getText();
171  }
172  $path = $this->findNearestElement('website', $key, $elements) . "/"
173  . $this->findNearestElement('store', $key, $elements) . "/"
174  . $element['element']->getText();
175  return $path;
176  }
177  }
178 
179  return '';
180  }
181 }
findNearestElement($criteria, $key, array $elements)
$pattern
Definition: website.php:22
taxRateField find('.mselect-list') .on( 'click.mselect-edit'
Definition: edit.phtml:162
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$element
Definition: element.phtml:12