-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
OptgroupselectElement.php
66 lines (57 loc) · 1.73 KB
/
OptgroupselectElement.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
<?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 option group selectors.
*/
class OptgroupselectElement extends SelectElement
{
/**
* Option group selector.
*
* @var string
*/
protected $optGroup = 'optgroup[option[contains(.,"%s")]]';
/**
* Option group locator.
*
* @var string
*/
protected $optionGroupValue = ".//optgroup[@label = '%s']/option[text() = '%s']";
/**
* Get the value of form element.
*
* @return string
* @throws \Exception
*/
public function getValue()
{
$this->eventManager->dispatchEvent(['get_value'], [(string)$this->getAbsoluteSelector()]);
$selectedLabel = parent::getValue();
if ($selectedLabel == '') {
throw new \Exception('Selected value has not been found in optgroup select.');
}
$element = $this->find(sprintf($this->optGroup, $selectedLabel), Locator::SELECTOR_XPATH);
$value = trim($element->getAttribute('label'), chr(0xC2) . chr(0xA0));
$value .= '/' . $selectedLabel;
return $value;
}
/**
* Select value in dropdown which has option groups.
*
* @param string $value
* @return void
*/
public function setValue($value)
{
$this->eventManager->dispatchEvent(['set_value'], [__METHOD__, $this->getAbsoluteSelector()]);
list($group, $option) = explode('/', $value);
$xpath = sprintf($this->optionGroupValue, $group, $option);
$option = $this->find($xpath, Locator::SELECTOR_XPATH);
$option->click();
}
}