Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,16 @@ modules:
// Fill title.
$i->fillTextField(FormField::title(), 'Mans nosukums');

// Select english language for content.
// Select option from select list by key or value.
// For custom fields, target (last parameter) usually needs to be set to an
// empty string.
$i->selectOptionFromList(FormField::langcode(), 'en');
$i->selectOptionFromList(FormField::langcode(), 'English');
$i->selectOptionFromList(FormField::field_my_list(), 'Apple', '');

// Select the nth option from a select list.
$i->selectOptionFromList(FormField::langcode());
$i->selectNthOptionFromList(MTOFormField::field_my_list(), 2, '');

// Fill first paragraph of type text.
$page_elements = ParagraphFormField::field_page_elements();
Expand Down
18 changes: 18 additions & 0 deletions src/Codeception/Module/DrupalAcceptance.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ public function selectOptionFromList(IdentifiableFormFieldInterface $field, $opt
$this->webdriver->selectOption($field->$target, $option);
}

/**
* Select nth option from select list.
*
* Useful if you don't know what the options in the list are, and just want
* to select the first one, second one etc.
*
* @param \Codeception\Util\IdentifiableFormFieldInterface $field
* Select list form field.
* @param int $nth
* Nth option to get. Default to first option.
* @param string $target
* Target field.
*/
public function selectNthOptionFromList(IdentifiableFormFieldInterface $field, $nth = 1, $target = 'value') {
$option = $this->webdriver->grabTextFrom($field->{$target} . '/option[' . $nth . ']');
$this->selectOptionFromList($field, $option, $target);
}

/**
* Click on element.
*
Expand Down
9 changes: 5 additions & 4 deletions src/Codeception/Util/Drupal/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ public function __toString() {
/**
* Returns xpath of current identifiers element.
*
* @param string $name
* @param string $element
* Name of element.
*
* @return string
* Returns path with current identifier plus requested subfield.
*/
public function __get($name) {
public function __get($element = '') {
$suffix = $element ? '-' . $this->normalise($element) : '';
return $this->getXpath([
'identifier' => $this->getCurrentIdentifier() . '-' . $this->normalise($name),
'identifier' => $this->getCurrentIdentifier() . $suffix,
Comment on lines +99 to +102
Copy link
Contributor Author

@Rade333 Rade333 Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guncha25 Calling $i->selectOptionFromList(FormField::field_my_list(), 'foo', ''); would result in identifier //*[@data-drupal-selector="edit-field-my-list-0-"] without this fix. The last parameter (target) needs to support empty values, since custom List fields usually don't have a target.

]);
}

Expand Down Expand Up @@ -142,7 +143,7 @@ public function getCurrentIdentifier() {
* @param string $element
* Name of element.
*
* @return mixed
* @return string
* Returns path with identifier plus requested subfield.
*/
public function get($element = '') {
Expand Down
10 changes: 4 additions & 6 deletions src/Codeception/Util/Drupal/MTOFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,14 @@ public function __toString() {
/**
* Returns xpath of current identifiers element.
*
* @param string $name
* @param string $element
* Name of element.
*
* @return string
* Returns path with current identifier plus requested subfield.
*/
public function __get($name) {
return $this->getXpath([
'identifier' => $this->getIdentifier() . '-' . $this->normalise($name),
]);
public function __get($element = '') {
return $this->get($element);
}

/**
Expand Down Expand Up @@ -132,7 +130,7 @@ public function getCurrentIdentifier() {
* @param string $element
* Name of element.
*
* @return mixed
* @return string
* Returns path with identifier plus requested subfield.
*/
public function get($element = '') {
Expand Down