Skip to content

Commit

Permalink
Merge pull request #17 from Rade333/feature/selectNthOptionFromList
Browse files Browse the repository at this point in the history
Added method selectNthOptionFromList()
  • Loading branch information
guncha25 authored Oct 11, 2023
2 parents 9ff726f + 7cc1c22 commit d3f7921
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
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 @@ -124,6 +124,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,
]);
}

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

0 comments on commit d3f7921

Please sign in to comment.