Skip to content

Commit d3f7921

Browse files
authored
Merge pull request guncha25#17 from Rade333/feature/selectNthOptionFromList
Added method selectNthOptionFromList()
2 parents 9ff726f + 7cc1c22 commit d3f7921

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,16 @@ modules:
196196
// Fill title.
197197
$i->fillTextField(FormField::title(), 'Mans nosukums');
198198
199-
// Select english language for content.
199+
// Select option from select list by key or value.
200+
// For custom fields, target (last parameter) usually needs to be set to an
201+
// empty string.
200202
$i->selectOptionFromList(FormField::langcode(), 'en');
203+
$i->selectOptionFromList(FormField::langcode(), 'English');
204+
$i->selectOptionFromList(FormField::field_my_list(), 'Apple', '');
205+
206+
// Select the nth option from a select list.
207+
$i->selectOptionFromList(FormField::langcode());
208+
$i->selectNthOptionFromList(MTOFormField::field_my_list(), 2, '');
201209
202210
// Fill first paragraph of type text.
203211
$page_elements = ParagraphFormField::field_page_elements();

src/Codeception/Module/DrupalAcceptance.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ public function selectOptionFromList(IdentifiableFormFieldInterface $field, $opt
124124
$this->webdriver->selectOption($field->$target, $option);
125125
}
126126

127+
/**
128+
* Select nth option from select list.
129+
*
130+
* Useful if you don't know what the options in the list are, and just want
131+
* to select the first one, second one etc.
132+
*
133+
* @param \Codeception\Util\IdentifiableFormFieldInterface $field
134+
* Select list form field.
135+
* @param int $nth
136+
* Nth option to get. Default to first option.
137+
* @param string $target
138+
* Target field.
139+
*/
140+
public function selectNthOptionFromList(IdentifiableFormFieldInterface $field, $nth = 1, $target = 'value') {
141+
$option = $this->webdriver->grabTextFrom($field->{$target} . '/option[' . $nth . ']');
142+
$this->selectOptionFromList($field, $option, $target);
143+
}
144+
127145
/**
128146
* Click on element.
129147
*

src/Codeception/Util/Drupal/FormField.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ public function __toString() {
9090
/**
9191
* Returns xpath of current identifiers element.
9292
*
93-
* @param string $name
93+
* @param string $element
9494
* Name of element.
9595
*
9696
* @return string
9797
* Returns path with current identifier plus requested subfield.
9898
*/
99-
public function __get($name) {
99+
public function __get($element = '') {
100+
$suffix = $element ? '-' . $this->normalise($element) : '';
100101
return $this->getXpath([
101-
'identifier' => $this->getCurrentIdentifier() . '-' . $this->normalise($name),
102+
'identifier' => $this->getCurrentIdentifier() . $suffix,
102103
]);
103104
}
104105

@@ -142,7 +143,7 @@ public function getCurrentIdentifier() {
142143
* @param string $element
143144
* Name of element.
144145
*
145-
* @return mixed
146+
* @return string
146147
* Returns path with identifier plus requested subfield.
147148
*/
148149
public function get($element = '') {

src/Codeception/Util/Drupal/MTOFormField.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,14 @@ public function __toString() {
8080
/**
8181
* Returns xpath of current identifiers element.
8282
*
83-
* @param string $name
83+
* @param string $element
8484
* Name of element.
8585
*
8686
* @return string
8787
* Returns path with current identifier plus requested subfield.
8888
*/
89-
public function __get($name) {
90-
return $this->getXpath([
91-
'identifier' => $this->getIdentifier() . '-' . $this->normalise($name),
92-
]);
89+
public function __get($element = '') {
90+
return $this->get($element);
9391
}
9492

9593
/**
@@ -132,7 +130,7 @@ public function getCurrentIdentifier() {
132130
* @param string $element
133131
* Name of element.
134132
*
135-
* @return mixed
133+
* @return string
136134
* Returns path with identifier plus requested subfield.
137135
*/
138136
public function get($element = '') {

0 commit comments

Comments
 (0)