Skip to content

Commit

Permalink
Merge branch 'MDL-70148-master' of git://github.com/andrewnicols/mood…
Browse files Browse the repository at this point in the history
…le into master
  • Loading branch information
stronk7 committed Nov 17, 2020
2 parents 84aae71 + 3959958 commit 6ca32e2
Show file tree
Hide file tree
Showing 34 changed files with 321 additions and 122 deletions.
52 changes: 52 additions & 0 deletions admin/tool/behat/tests/behat/keyboard.feature
@@ -0,0 +1,52 @@
@tool_behat
Feature: Verify that keyboard steps work as expected
In order to use behat step definitions
As a test writer
I need to verify that the keyboard steps work as expected

@javascript
Scenario: Typing keys into a field causes them to be input
Given the following "users" exist:
| username | email | firstname | lastname | password |
| saffronr | saffron.rutledge@example.com | Saffron | Rutledge | flowerpower |
Given I click on "Log in" "link"
And I click on "Username" "field"
When I type "saffronr"
And I press the tab key
And I type "flowerpower"
And I press enter
Then I should see "You are logged in as Saffron Rutledge"

@javascript
Scenario: Using tab changes focus to the next or previous field
Given I click on "Log in" "link"
And I click on "Username" "field"
And the focused element is "Username" "field"
When I press the tab key
Then the focused element is "Password" "field"

And I press the shift tab key
And the focused element is "Username" "field"

@javascript
Scenario: Using the arrow keys allows me to navigate through menus
Given the following "users" exist:
| username | email | firstname | lastname |
| saffronr | saffron.rutledge@example.com | Saffron | Rutledge |
And I log in as "saffronr"
And I click on "Saffron Rutledge" "link" in the ".usermenu" "css_element"
When I press the up key
Then the focused element is "Log out" "link"

@javascript
Scenario: The escape key can be used to close a dialogue
Given the following "course" exists:
| fullname | C1|
| shortname | C1 |
And I log in as "admin"
And I am on "C1" course homepage
And I navigate to course participants
And I press "Enrol users"
And "Enrol users" "dialogue" should be visible
When I press the escape key
Then "Enrol users" "dialogue" should not be visible
Expand Up @@ -46,7 +46,7 @@ Feature: Manage data requests
And I open the action menu in "John Doe" "table_row"
And I should see "View the request"
But I should not see "Mark as complete"
And I press key "27" in ".moodle-actionmenu" "css_element"
And I press the escape key
And I open the action menu in "Jane Doe" "table_row"
And I choose "Mark as complete" in the open action menu
And I should see "Do you really want to mark this user enquiry as complete?"
Expand Down
6 changes: 3 additions & 3 deletions admin/tool/dataprivacy/tests/behat/manage_purposes.feature
Expand Up @@ -15,10 +15,10 @@ Feature: Manage data storage purposes
And I click on ".form-autocomplete-downarrow" "css_element" in the "Lawful bases" "form_row"
And I click on "Contract (GDPR Art. 6.1(b))" "list_item"
And I click on "Legal obligation (GDPR Art 6.1(c))" "list_item"
And I press key "27" in the field "Lawful bases"
And I press the escape key
And I click on ".form-autocomplete-downarrow" "css_element" in the "Sensitive personal data processing reasons" "form_row"
And I click on "Explicit consent (GDPR Art. 9.2(a))" "list_item"
And I press key "27" in the field "Sensitive personal data processing reasons"
And I press the escape key
And I set the field "retentionperiodnumber" to "2"
When I press "Save"
Then I should see "Purpose 1" in the "List of data purposes" "table"
Expand All @@ -36,7 +36,7 @@ Feature: Manage data storage purposes
And I click on "Legal obligation (GDPR Art 6.1(c))" "text" in the ".form-autocomplete-selection" "css_element"
And I click on ".form-autocomplete-downarrow" "css_element" in the "Lawful bases" "form_row"
And I click on "Vital interests (GDPR Art. 6.1(d))" "list_item"
And I press key "27" in the field "Lawful bases"
And I press the escape key
And I set the field "retentionperiodnumber" to "3"
And I click on "protected" "checkbox"
When I press "Save changes"
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/lp/tests/behat/course_competencies.feature
Expand Up @@ -53,11 +53,11 @@ Feature: See the competencies for an activity on the course competencies page.
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 press key "13" in the field "Filter competencies by resource or activity"
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 press key "13" in the field "Filter competencies by resource or activity"
And I press the enter key
And I should not see "Test-Comp1"
And I should not see "Test-Comp2"
And I should see "No competencies have been linked to this activity or resource."
Expand Down
4 changes: 2 additions & 2 deletions admin/tool/lp/tests/behat/plan_crud.feature
Expand Up @@ -30,7 +30,7 @@ Feature: Manage plearning plan
And I click on ".template-userplans" "css_element" in the "Science template" "table_row"
And I open the autocomplete suggestions list
And I click on "Admin User" item in the autocomplete list
And I press key "27" in the field "Select users to create learning plans for"
And I press the escape key
When I click on "Create learning plans" "button"
Then I should see "A learning plan was created"
And I should see "Admin User" in the "Science template" "table_row"
Expand All @@ -55,7 +55,7 @@ Feature: Manage plearning plan
And I click on ".template-cohorts" "css_element" in the "Science template cohort" "table_row"
And I click on ".form-autocomplete-downarrow" "css_element"
And I click on "cohort plan" item in the autocomplete list
And I press key "27" in the field "Select cohorts to sync"
And I press the escape key
When I click on "Add cohorts" "button"
Then I should see "2 learning plans were created."
And I follow "Learning plan templates"
Expand Down
2 changes: 1 addition & 1 deletion blocks/site_main_menu/tests/behat/edit_activities.feature
Expand Up @@ -14,7 +14,7 @@ Feature: Edit activities in main menu block
| Forum name | My forum name |
And I click on "Edit title" "link" in the "My forum name" activity in site main menu block
And I set the field "New name for activity My forum name" to "New forum name"
And I press key "13" in the field "New name for activity My forum name"
And I press the enter key
Then I should not see "My forum name"
And I should see "New forum name"
And I follow "New forum name"
Expand Down
Expand Up @@ -27,7 +27,7 @@ Feature: Edit activities in social activities block
And I press "Save and return to course"
And I click on "Edit title" "link" in the "My forum name" activity in social activities block
And I set the field "New name for activity My forum name" to "New forum name"
And I press key "13" in the field "New name for activity My forum name"
And I press the enter key
Then I should not see "My forum name" in the "Social activities" "block"
And I should see "New forum name"
And I follow "New forum name"
Expand Down
2 changes: 1 addition & 1 deletion cohort/tests/behat/add_cohort.feature
Expand Up @@ -64,7 +64,7 @@ Feature: Add cohorts of users
When I follow "Cohorts"
And I click on "Edit cohort name" "link" in the "Test cohort name" "table_row"
And I set the field "New name for cohort Test cohort name" to "Students cohort"
And I press key "13" in the field "New name for cohort Test cohort name"
And I press the enter key
Then I should not see "Test cohort name"
And I should see "Students cohort"
And I follow "Cohorts"
Expand Down
Expand Up @@ -55,7 +55,7 @@ Feature: Sections can be edited and deleted in topics format
Scenario: Inline edit section name in topics format
When I click on "Edit topic name" "link" in the "li#section-1" "css_element"
And I set the field "New name for topic Topic 1" to "Midterm evaluation"
And I press key "13" in the field "New name for topic Topic 1"
And I press the enter key
Then I should not see "Topic 1" in the "region-main" "region"
And "New name for topic" "field" should not exist
And I should see "Midterm evaluation" in the "li#section-1" "css_element"
Expand Down
Expand Up @@ -56,7 +56,7 @@ Feature: Sections can be edited and deleted in weeks format
Scenario: Inline edit section name in weeks format
When I click on "Edit week name" "link" in the "li#section-1" "css_element"
And I set the field "New name for week 1 May - 7 May" to "Midterm evaluation"
And I press key "13" in the field "New name for week 1 May - 7 May"
And I press the enter key
Then I should not see "1 May - 7 May" in the "region-main" "region"
And "New name for week" "field" should not exist
And I should see "Midterm evaluation" in the "li#section-1" "css_element"
Expand Down
5 changes: 2 additions & 3 deletions course/tests/behat/activities_edit_name.feature
Expand Up @@ -23,7 +23,7 @@ Feature: Edit activity name in-place
# Rename activity
And I click on "Edit title" "link" in the "//div[contains(@class,'activityinstance') and contains(.,'Test forum name')]" "xpath_element"
And I set the field "New name for activity Test forum name" to "Good news"
And I press key "13" in the field "New name for activity Test forum name"
And I press the enter key
Then I should not see "Test forum name" in the ".course-content" "css_element"
And "New name for activity Test forum name" "field" should not exist
And I should see "Good news"
Expand All @@ -33,11 +33,10 @@ Feature: Edit activity name in-place
# Cancel renaming
And I click on "Edit title" "link" in the "//div[contains(@class,'activityinstance') and contains(.,'Good news')]" "xpath_element"
And I set the field "New name for activity Good news" to "Terrible news"
And I press key "27" in the field "New name for activity Good news"
And I press the escape key
And "New name for activity Good news" "field" should not exist
And I should see "Good news"
And I should not see "Terrible news"
And I am on "Course 1" course homepage
And I should see "Good news"
And I should not see "Terrible news"
And I log out
2 changes: 1 addition & 1 deletion customfield/tests/behat/edit_categories.feature
Expand Up @@ -22,7 +22,7 @@ Feature: Managers can manage categories for course custom fields
And I navigate to "Courses > Course custom fields" in site administration
And I click on "Edit category name" "link" in the "//div[contains(@class,'categoryinstance') and contains(.,'Category for test')]" "xpath_element"
And I set the field "New value for Category for test" to "Good fields"
And I press key "13" in the field "New value for Category for test"
And I press the enter key
Then I should not see "Category for test" in the "#customfield_catlist" "css_element"
And "New value for Category for test" "field" should not exist
And I should see "Good fields" in the "#customfield_catlist" "css_element"
Expand Down
2 changes: 1 addition & 1 deletion enrol/tests/behat/add_to_group.feature
Expand Up @@ -32,6 +32,6 @@ Feature: Users can be added to multiple groups at once
And I click on "Group 1" item in the autocomplete list
And I click on ".form-autocomplete-downarrow" "css_element" in the "student1" "table_row"
And I click on "Group 2" item in the autocomplete list
And I press key "27" in the field "Edit groups for \"Student 1\""
And I press the escape key
And I click on "Save changes" "link" in the "student1" "table_row"
Then I should see "Group 1, Group 2"
22 changes: 11 additions & 11 deletions grade/report/grader/tests/behat/ajax_grader.feature
Expand Up @@ -59,18 +59,18 @@ Feature: Using the AJAX grading feature of Grader report to update grades and fe
Then I should see a grade field for "Student 2" and grade item "Item VU"
And I should not see a feedback field for "Student 2" and grade item "Item VU"
And I set the field "ajaxgrade" to "33"
And I press key "13" in the field "ajaxgrade"
And I press the enter key
And I should not see a grade field for "Student 2" and grade item "Item VU"
And I should not see a feedback field for "Student 2" and grade item "Item VU"
And I click on student "Student 3" for grade item "Item SU"
And I set the field "ajaxgrade" to "Very good"
And I press the shift tab key
And I click on student "Student 3" for grade item "Item VU"
And I set the field "ajaxgrade" to "50"
And I press key "13" in the field "ajaxgrade"
And I press the enter key
And I click on student "Student 3" for grade item "Item 1"
And I set the field "ajaxgrade" to "80"
And I press key "13" in the field "ajaxgrade"
And I click on student "Student 3" for grade item "Item SU"
And I set the field "ajaxgrade" to "Very good"
And I press key "13" in the field "ajaxgrade"
And I press the enter key
And the following should exist in the "user-grades" table:
| -1- | -6- | -7- | -13- | -16- |
| Student 2 | - | 33.00 | - | 33.00 |
Expand All @@ -88,7 +88,7 @@ Feature: Using the AJAX grading feature of Grader report to update grades and fe
And I should see a grade field for "Student 1" and grade item "Course total"
And I should not see a feedback field for "Student 1" and grade item "Course total"
And I set the field "ajaxgrade" to "90"
And I press key "13" in the field "ajaxgrade"
And I press the enter key
And the following should exist in the "user-grades" table:
| -1- | -16- |
| Student 1 | 90.00 |
Expand All @@ -112,19 +112,19 @@ Feature: Using the AJAX grading feature of Grader report to update grades and fe
And I should see a feedback field for "Student 2" and grade item "Item VU"
And I set the field "ajaxgrade" to "33"
And I set the field "ajaxfeedback" to "Student 2 VU feedback"
And I press key "13" in the field "ajaxfeedback"
And I press the enter key
And I click on student "Student 3" for grade item "Item VL"
And I should not see a grade field for "Student 3" and grade item "Item VL"
And I should not see a feedback field for "Student 3" and grade item "Item VL"
And I click on student "Student 3" for grade item "Item TU"
And I should not see a grade field for "Student 3" and grade item "Item TU"
And I should see a feedback field for "Student 3" and grade item "Item TU"
And I set the field "ajaxfeedback" to "Student 3 TU feedback"
And I press key "13" in the field "ajaxfeedback"
And I press the enter key
And I click on student "Student 2" for grade item "Item SU"
And I set the field "ajaxgrade" to "Very good"
And I set the field "ajaxfeedback" to "Student 2 SU feedback"
And I press key "13" in the field "ajaxfeedback"
And I press the enter key
# Reload grader report:
And I navigate to "View > User report" in the course gradebook
And I navigate to "View > Grader report" in the course gradebook
Expand All @@ -148,7 +148,7 @@ Feature: Using the AJAX grading feature of Grader report to update grades and fe
Then I should see a grade field for "Student 2" and grade item "Item VU"
And I should see a feedback field for "Student 2" and grade item "Item VU"
And I set the field "ajaxgrade" to "33"
And I press key "13" in the field "ajaxgrade"
And I press the enter key
And I click on student "Student 2" for grade item "Course total"
And I should not see a grade field for "Student 3" and grade item "Course total"
And I should not see a feedback field for "Student 3" and grade item "Course total"
Expand Down
42 changes: 42 additions & 0 deletions lib/behat/behat_base.php
Expand Up @@ -38,6 +38,10 @@
require_once(__DIR__ . '/classes/component_named_selector.php');
require_once(__DIR__ . '/classes/component_named_replacement.php');

// Alias the WebDriver\Key class to behat_keys to make future transition to a different WebDriver implementation
// easier.
class_alias('WebDriver\\Key', 'behat_keys');

/**
* Steps definitions base class.
*
Expand Down Expand Up @@ -264,6 +268,44 @@ public function normalise_selector(string $selector, $locator, Element $containe
];
}

/**
* Send key presses straight to the currently active element.
*
* The `$keys` array contains a list of key values to send to the session as defined in the WebDriver and JsonWire
* specifications:
* - JsonWire: https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol#sessionsessionidkeys
* - W3C WebDriver: https://www.w3.org/TR/webdriver/#keyboard-actions
*
* This may be a combination of typable characters, modifier keys, and other supported keypoints.
*
* The NULL_KEY should be used to release modifier keys. If the NULL_KEY is not used then modifier keys will remain
* in the pressed state.
*
* Example usage:
*
* behat_base::type_keys($this->getSession(), [behat_keys::SHIFT, behat_keys::TAB, behat_keys::NULL_KEY]);
* behat_base::type_keys($this->getSession(), [behat_keys::ENTER, behat_keys::NULL_KEY]);
* behat_base::type_keys($this->getSession(), [behat_keys::ESCAPE, behat_keys::NULL_KEY]);
*
* It can also be used to send text input, for example:
*
* behat_base::type_keys(
* $this->getSession(),
* ['D', 'o', ' ', 'y', 'o', 'u', ' ', 'p', 'l', 'a' 'y', ' ', 'G', 'o', '?', behat_base::NULL_KEY]
* );
*
*
* Please note: This function does not use the element/sendKeys variants but sends keys straight to the browser.
*
* @param Session $session
* @param string[] $keys
*/
public static function type_keys(Session $session, array $keys): void {
$session->getDriver()->getWebDriverSession()->keys([
'value' => $keys,
]);
}

/**
* Finds DOM nodes in the page using named selectors.
*
Expand Down
10 changes: 4 additions & 6 deletions lib/behat/form_field/behat_form_autocomplete.php
Expand Up @@ -78,14 +78,12 @@ public function set_value($value) {
$suggestion->click();
} else {
// Press the return key to create a new tag.
// Note: We cannot use $this->key_press() because the keyPress action, in combination with the keyDown
// submits the form.
$this->field->keyDown(13);
$this->field->keyUp(13);
behat_base::type_keys($this->session, [behat_keys::ENTER]);
}

$this->wait_for_pending_js();
$this->key_press(27);

// Press the escape to close the autocomplete suggestions list.
behat_base::type_keys($this->session, [behat_keys::ESCAPE]);
$this->wait_for_pending_js();
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tests/behat/action_menu.feature
Expand Up @@ -12,7 +12,7 @@ Feature: Navigate action menu
# The menu should now be visible.
Then ".usermenu [role='menu']" "css_element" should be visible
# Press down arrow.
And I press key "40" in "#actionmenuaction-1" "css_element"
And I press the down key
# The menu should still be visible.
And ".usermenu [role='menu']" "css_element" should be visible

Expand Down

0 comments on commit 6ca32e2

Please sign in to comment.