-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
SelectTrait.php
77 lines (64 loc) · 2.31 KB
/
SelectTrait.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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
/**
* Trait SelectTrait.
*
* Steps to work with HTML select element.
*
* @package DrevOps\BehatSteps
*/
trait SelectTrait {
/**
* Assert that a select has an option.
*
* @Then select :select should have an option :option
*/
public function selectShouldHaveOption(string $select, string $option): void {
$selectElement = $this->getSession()->getPage()->findField($select);
if (is_null($selectElement)) {
throw new \InvalidArgumentException(sprintf('Element "%s" is not found.', $select));
}
$optionElement = $selectElement->find('named', ['option', $option]);
if (is_null($optionElement)) {
throw new \InvalidArgumentException(sprintf('Option "%s" is not found in select "%s".', $option, $select));
}
}
/**
* Assert that a select does not have an option.
*
* @Then select :select should not have an option :option
*/
public function selectShouldNotHaveOption(string $select, string $option): void {
$selectElement = $this->getSession()->getPage()->findField($select);
if (is_null($selectElement)) {
throw new \InvalidArgumentException(sprintf('Element "%s" is not found.', $select));
}
$optionElement = $selectElement->find('named', ['option', $option]);
if (!is_null($optionElement)) {
throw new \InvalidArgumentException(sprintf('Option "%s" is found in select "%s", but should not.', $option, $select));
}
}
/**
* Assert that a select option is selected.
*
* @Then /^the option "([^"]*)" from select "([^"]*)" is selected$/
*/
public function selectOptionSelected(string $value, string $select): void {
$selectField = $this->getSession()->getPage()->findField($select);
$currentUrl = $this->getSession()->getCurrentUrl();
if (!$selectField) {
throw new \Exception(sprintf('The select "%s" was not found on the page %s', $select, $currentUrl));
}
$optionField = $selectField->find('named', [
'option',
$value,
]);
if (!$optionField) {
throw new \Exception(sprintf('No option is selected in the %s select on the page %s', $select, $currentUrl));
}
if (!$optionField->isSelected()) {
throw new \Exception(sprintf('The option "%s" was not selected on the page %s', $value, $currentUrl));
}
}
}