Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MultiselectgrouplistElement.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Mtf\Client\Locator;
10 use Magento\Mtf\Client\ElementInterface;
11 
16 class MultiselectgrouplistElement extends MultiselectElement
17 {
21  const INDENT_LENGTH = 4;
22 
28  protected $optgroupByLabel = './/optgroup[@label="%s"]';
29 
35  protected $optgroupByNumber = './/optgroup[%d]';
36 
42  protected $nextOptgroup = './/following-sibling::optgroup[%d]';
43 
49  protected $childOptgroup = ".//following-sibling::optgroup[%d][@label='%s']";
50 
56  protected $parentOptgroup = 'optgroup[option[text()="%s"]]';
57 
63  protected $precedingOptgroup = '/preceding-sibling::optgroup[1][substring(@label,1,%d)="%s"]';
64 
70  protected $option = './/option[text()="%s"]';
71 
77  protected $childOptionByNumber = './/optgroup[%d]/option[%d]';
78 
84  protected $uiOptionText = './/option[@data-title="%s"]';
85 
91  protected $optionByNumber = './option[%d]';
92 
98  protected $indent = "\xC2\xA0\xC2\xA0\xC2\xA0\xC2\xA0";
99 
105  protected $trim = "\xC2\xA0 ";
106 
113  public function setValue($values)
114  {
115  $this->deselectAll();
116  $values = is_array($values) ? $values : [$values];
117  foreach ($values as $value) {
118  $this->selectOption($value);
119  }
120  }
121 
129  protected function selectOption($option)
130  {
131  $optionElement = $this->find(sprintf($this->uiOptionText, $option), Locator::SELECTOR_XPATH);
132  if ($optionElement->isVisible()) {
133  if (!$optionElement->isSelected()) {
134  $optionElement->click();
135  }
136  return;
137  }
138 
139  $isOptgroup = false;
140  $optgroupIndent = '';
141  $values = explode('/', $option);
142  $context = $this;
143 
144  foreach ($values as $value) {
145  $optionIndent = $isOptgroup ? $this->indent : '';
146  $optionElement = $context->find(sprintf($this->option, $optionIndent . $value), Locator::SELECTOR_XPATH);
147  if ($optionElement->isVisible()) {
148  if (!$optionElement->isSelected()) {
149  $optionElement->click();
150  }
151  return;
152  }
153 
154  $value = $optgroupIndent . $value;
155  $optgroupIndent .= $this->indent;
156  if ($isOptgroup) {
157  $context = $this->getChildOptgroup($value, $context);
158  } else {
159  $context = $this->getOptgroup($value, $context);
160  $isOptgroup = true;
161  }
162  }
163  throw new \Exception("Can't find option \"{$option}\".");
164  }
165 
174  protected function getOptgroup($value, ElementInterface $context)
175  {
176  $optgroup = $context->find(sprintf($this->optgroupByLabel, $value), Locator::SELECTOR_XPATH);
177  if (!$optgroup->isVisible()) {
178  throw new \Exception("Can't find group \"{$value}\".");
179  }
180 
181  return $optgroup;
182  }
183 
192  protected function getChildOptgroup($value, ElementInterface $context)
193  {
194  $childOptgroup = null;
195  $count = 1;
196  while (!$childOptgroup) {
197  $optgroup = $context->find(sprintf($this->nextOptgroup, $count), Locator::SELECTOR_XPATH);
198  if (!$optgroup->isVisible()) {
199  throw new \Exception("Can't find child group \"{$value}\"");
200  }
201 
202  $childOptgroup = $context->find(
203  sprintf($this->childOptgroup, $count, $value),
204  Locator::SELECTOR_XPATH
205  );
206  if (!$childOptgroup->isVisible()) {
207  $childOptgroup = null;
208  }
209  ++$count;
210  }
211 
212  return $childOptgroup;
213  }
214 
221  public function getValue()
222  {
223  $values = [];
224  $indentOption = str_repeat(' ', self::INDENT_LENGTH);
225 
226  foreach ($this->getSelectedOptions() as $option) {
227  $value = [];
228 
230  $optionText = $option->getText();
231  $optionValue = trim($optionText, $this->trim);
232  $value[] = $optionValue;
233  if (0 !== strpos($optionText, $indentOption)) {
234  $values[] = implode('/', $value);
235  continue;
236  }
237 
238  $pathOptgroup = sprintf($this->parentOptgroup, $this->indent . $optionValue);
239  $optgroup = $this->find($pathOptgroup, Locator::SELECTOR_XPATH);
240  $optgroupText = $optgroup->getAttribute('label');
241  $optgroupValue = trim($optgroupText, $this->trim);
242  $amountIndent = strlen($optgroupText) - strlen($optgroupValue);
243  $amountIndent = $amountIndent ? ($amountIndent / strlen($this->indent)) : 0;
244  $value[] = $optgroupValue;
245  if (0 == $amountIndent) {
246  $values[] = implode('/', $value);
247  continue;
248  }
249 
250  --$amountIndent;
251  $indent = $amountIndent ? str_repeat($this->indent, $amountIndent) : '';
252  $pathOptgroup .= sprintf($this->precedingOptgroup, $amountIndent * self::INDENT_LENGTH, $indent);
253  while (0 <= $amountIndent && $this->find($pathOptgroup, Locator::SELECTOR_XPATH)->isVisible()) {
254  $optgroup = $this->find($pathOptgroup, Locator::SELECTOR_XPATH);
255  $optgroupText = $optgroup->getAttribute('label');
256  $optgroupValue = trim($optgroupText, $this->trim);
257  $value[] = $optgroupValue;
258 
259  --$amountIndent;
260  $indent = (0 < $amountIndent) ? str_repeat($this->indent, $amountIndent) : '';
261  $pathOptgroup .= sprintf($this->precedingOptgroup, $amountIndent * self::INDENT_LENGTH, $indent);
262  }
263 
264  $values[] = implode('/', array_reverse($value));
265  }
266 
267  return $values;
268  }
269 
275  protected function getOptions()
276  {
277  $options = [];
278 
279  $countOption = 1;
280  $option = $this->find(sprintf($this->optionByNumber, $countOption), Locator::SELECTOR_XPATH);
281  while ($option->isVisible()) {
282  $options[] = $option;
283  ++$countOption;
284  $option = $this->find(sprintf($this->optionByNumber, $countOption), Locator::SELECTOR_XPATH);
285  }
286 
287  $countOptgroup = 1;
288  $optgroup = $this->find(sprintf($this->optgroupByNumber, $countOptgroup), Locator::SELECTOR_XPATH);
289  while ($optgroup->isVisible()) {
290  $countOption = 1;
291  $option = $this->find(
292  sprintf($this->childOptionByNumber, $countOptgroup, $countOption),
293  Locator::SELECTOR_XPATH
294  );
295  while ($option->isVisible()) {
296  $options[] = $option;
297  ++$countOption;
298  $option = $this->find(
299  sprintf($this->childOptionByNumber, $countOptgroup, $countOption),
300  Locator::SELECTOR_XPATH
301  );
302  }
303 
304  ++$countOptgroup;
305  $optgroup = $this->find(sprintf($this->optgroupByNumber, $countOptgroup), Locator::SELECTOR_XPATH);
306  }
307 
308  return $options;
309  }
310 
316  protected function getSelectedOptions()
317  {
318  $options = [];
319  foreach ($this->getOptions() as $option) {
320  if ($option->isSelected()) {
321  $options[] = $option;
322  }
323  }
324 
325  return $options;
326  }
327 
331  public function deselectAll()
332  {
333  $options = $this->getSelectedOptions();
334 
336  foreach ($options as $option) {
337  $option->click();
338  }
339  }
340 }
$count
Definition: recent.phtml:13
taxRateField find('.mselect-list') .on( 'click.mselect-edit'
Definition: edit.phtml:162
$values
Definition: options.phtml:88
$value
Definition: gender.phtml:16