-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
MultisuggestElement.php
106 lines (93 loc) · 2.39 KB
/
MultisuggestElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Mtf\Client\Element;
use Magento\Mtf\Client\Locator;
/**
* Typified element class for multi suggest element.
*/
class MultisuggestElement extends SuggestElement
{
/**
* Selector list choice
*
* @var string
*/
protected $listChoice = './/ul[contains(@class,"mage-suggest-choices")]';
/**
* Selector choice item
*
* @var string
*/
protected $choice = './/li/div[text()="%s"]/..';
/**
* Selector choice value
*
* @var string
*/
protected $choiceValue = './/li[contains(@class,"mage-suggest-choice")]/div';
/**
* Selector remove choice item
*
* @var string
*/
protected $choiceClose = '.mage-suggest-choice-close';
/**
* Set value
*
* @param array|string $values
* @return void
*/
public function setValue($values)
{
$this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
$this->clear();
foreach ((array)$values as $value) {
if (!$this->isChoice($value)) {
parent::setValue($value);
}
}
}
/**
* Get value
*
* @return array
*/
public function getValue()
{
$this->eventManager->dispatchEvent(['get_value'], [(string) $this->getAbsoluteSelector()]);
$listChoice = $this->find($this->listChoice, Locator::SELECTOR_XPATH);
$choices = $listChoice->getElements($this->choiceValue, Locator::SELECTOR_XPATH);
$values = [];
foreach ($choices as $choice) {
/** @var \Magento\Mtf\Client\ElementInterface $choice */
$values[] = $choice->getText();
}
return $values;
}
/**
* Check exist selected item
*
* @param string $value
* @return bool
*/
protected function isChoice($value)
{
return $this->find(sprintf($this->choice, $value), Locator::SELECTOR_XPATH)->isVisible();
}
/**
* Clear element
*
* @return void
*/
protected function clear()
{
$choiceClose = $this->find($this->choiceClose);
while ($choiceClose->isVisible()) {
$choiceClose->click();
$choiceClose = $this->find($this->choiceClose);
}
}
}