diff --git a/admin/tool/lp/tests/behat/course_competencies.feature b/admin/tool/lp/tests/behat/course_competencies.feature index ddf92705022..0a2fd5ee8fb 100644 --- a/admin/tool/lp/tests/behat/course_competencies.feature +++ b/admin/tool/lp/tests/behat/course_competencies.feature @@ -46,11 +46,11 @@ Feature: See the competencies for an activity on the course competencies page. When I follow "Competencies" Then I should see "Test-Comp1" And I should see "Test-Comp2" - And I set the field "Filter competencies by resource or activity" to "PageName1" + And I set the competency filter "Filter competencies by resource or activity" to "PageName1" And I press the enter key And I should see "Test-Comp1" And I should not see "Test-Comp2" - And I set the field "Filter competencies by resource or activity" to "PageName2" + And I set the competency filter "Filter competencies by resource or activity" to "PageName2" And I press the enter key And I should not see "Test-Comp1" And I should not see "Test-Comp2" diff --git a/lib/behat/behat_field_manager.php b/lib/behat/behat_field_manager.php index 6136871759a..204b44fdd28 100644 --- a/lib/behat/behat_field_manager.php +++ b/lib/behat/behat_field_manager.php @@ -72,7 +72,6 @@ class behat_field_manager { * @return behat_form_field */ public static function get_form_field(NodeElement $fieldnode, Session $session) { - // Get the field type if is part of a moodleform. if (self::is_moodleform_field($fieldnode)) { $type = self::get_field_node_type($fieldnode, $session); diff --git a/lib/behat/form_field/behat_form_field.php b/lib/behat/form_field/behat_form_field.php index b01ed794bbf..6d5c8ad5192 100644 --- a/lib/behat/form_field/behat_form_field.php +++ b/lib/behat/form_field/behat_form_field.php @@ -326,4 +326,13 @@ class behat_form_field implements behat_session_interface { return $this->fieldlocator; } + + /** + * Returns the field node. + * + * @return NodeElement + */ + public function get_node(): NodeElement { + return $this->field; + } } diff --git a/report/competency/tests/behat/behat_report_competency.php b/report/competency/tests/behat/behat_report_competency.php index 6d5df5350a8..ddf06d5e26a 100644 --- a/report/competency/tests/behat/behat_report_competency.php +++ b/report/competency/tests/behat/behat_report_competency.php @@ -76,4 +76,52 @@ class behat_report_competency extends behat_base { ]; } + /** + * Set the value of a competency filter. + * + * @When /^I set the competency filter "([^"]*)" to "([^"]*)"$/ + * @param string $fieldlocator The field locator. + * @param string $value The value to set. + * @throws Exception + */ + public function set_competency_filter( + string $fieldlocator, + string $value, + ): void { + $field = behat_field_manager::get_form_field_from_label($fieldlocator, $this)->get_node(); + $session = $this->getSession(); + + $value = trim($value); + + // Click into the field. + $field->click(); + + // Remove any existing text. + do { + behat_base::type_keys($session, [behat_keys::BACKSPACE, behat_keys::DELETE]); + } while (strlen($field->getValue()) > 0); + $this->wait_for_pending_js(); + + // Type in the new value. + behat_base::type_keys($session, str_split($value)); + $this->wait_for_pending_js(); + + // If the autocomplete found suggestions, then it will have: + // 1) marked itself as expanded; and + // 2) have an aria-selected suggestion in the list. + $expanded = $field->getAttribute('aria-expanded'); + $suggestion = $field->getParent()->getParent()->find('css', '.form-autocomplete-suggestions > [aria-selected="true"]'); + + if ($expanded && null !== $suggestion) { + // A suggestion was found. + // Click on the first item in the list. + $suggestion->click(); + } else { + throw new \InvalidArgumentException( + "Unable to find '{$value}' in the list of options." + ); + } + + $this->wait_for_pending_js(); + } } diff --git a/report/competency/tests/behat/breakdown_by_activity.feature b/report/competency/tests/behat/breakdown_by_activity.feature index 2608cd87ed4..87ebdc139c6 100644 --- a/report/competency/tests/behat/breakdown_by_activity.feature +++ b/report/competency/tests/behat/breakdown_by_activity.feature @@ -50,7 +50,7 @@ Feature: See the competencies for an activity Scenario: Go to the competency breakdown report When I navigate to "Reports" in current page administration And I click on "Competency breakdown" "link" - And I set the field "Filter competencies by resource or activity" to "PageName1" + And I set the competency filter "Filter competencies by resource or activity" to "PageName1" Then I should see "Test-Comp1" And I should not see "Test-Comp2" And I should see "Ann, Jill, Grainne, Beauchamp"