From 1e6071b0c05b9dcb6d8a1df3d7d7af949b9949e1 Mon Sep 17 00:00:00 2001 From: Rasmus Werling Date: Fri, 28 Jan 2022 14:16:53 +0200 Subject: [PATCH 1/2] Added DrupalAcceptance::selectNthOptionFromList(), updated readme. --- README.md | 10 +++++++++- src/Codeception/Module/DrupalAcceptance.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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. * From 7cc1c22cbd25531ceec75f057a6d93b944b4169c Mon Sep 17 00:00:00 2001 From: Rasmus Werling Date: Fri, 28 Jan 2022 14:19:11 +0200 Subject: [PATCH 2/2] Fixed __get() methods in FormField and MTOFormField, as the dash '-' would be added even if there was no suffix, for example when calling $field->{$target} with an empty target. --- src/Codeception/Util/Drupal/FormField.php | 9 +++++---- src/Codeception/Util/Drupal/MTOFormField.php | 10 ++++------ 2 files changed, 9 insertions(+), 10 deletions(-) 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 = '') {