diff --git a/README.md b/README.md index 87772be..b230249 100644 --- a/README.md +++ b/README.md @@ -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(); diff --git a/src/Codeception/Module/DrupalAcceptance.php b/src/Codeception/Module/DrupalAcceptance.php index 305de85..f5f0c6f 100644 --- a/src/Codeception/Module/DrupalAcceptance.php +++ b/src/Codeception/Module/DrupalAcceptance.php @@ -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. * diff --git a/src/Codeception/Util/Drupal/FormField.php b/src/Codeception/Util/Drupal/FormField.php index 68945e5..5fd6411 100644 --- a/src/Codeception/Util/Drupal/FormField.php +++ b/src/Codeception/Util/Drupal/FormField.php @@ -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, ]); } @@ -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 = '') { diff --git a/src/Codeception/Util/Drupal/MTOFormField.php b/src/Codeception/Util/Drupal/MTOFormField.php index 4758e71..b6abde4 100644 --- a/src/Codeception/Util/Drupal/MTOFormField.php +++ b/src/Codeception/Util/Drupal/MTOFormField.php @@ -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); } /** @@ -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 = '') {