Skip to content

Commit

Permalink
MDL-78289 courseformat: Amend behat tests to reduce test overheads
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols authored and roland04 committed Aug 1, 2023
1 parent 8ff6051 commit 01a1210
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 25 deletions.
67 changes: 42 additions & 25 deletions course/format/tests/behat/coursepage_completion.feature
@@ -1,8 +1,8 @@
@core @core_course @core_courseformat @core_completion @javascript
@core @core_course @core_courseformat @core_completion
Feature: Course page activities completion
In order to check activities completions
As a student
I need to see the activity completion criterias dropdown.
In order to check activities completions
As a student
I need to see the activity completion criterias dropdown.

Background:
Given the following "users" exist:
Expand All @@ -12,44 +12,61 @@ Feature: Course page activities completion
And the following "courses" exist:
| shortname | fullname | enablecompletion |
| C1 | Course 1 | 1 |
And the following "activities" exist:
| activity | name | intro | course | idnumber | section | completion | completionview |
| assign | Activity sample 1 | Test assignment description | C1 | sample1 | 1 | 1 | 0 |
| assign | Activity sample 2 | Test assignment description | C1 | sample2 | 1 | 2 | 1 |
And the following "course enrolments" exist:
| user | course | role |
| student1 | C1 | student |
| teacher1 | C1 | editingteacher |

Scenario: Teacher does not see manual completion button
Given the following "activity" exists:
| activity | assign |
| name | Activity sample |
| course | C1 |
| completion | 1 |
| completionview | 0 |
When I am on the "C1" "Course" page logged in as "teacher1"
Then "Mark as done" "button" should not exist in the "Activity sample 1" "activity"
And I click on "Completion" "button" in the "Activity sample 1" "activity"
And I should see "Mark as done" in the "Activity sample 1" "core_courseformat > Activity completion"
Then "Mark as done" "button" should not exist in the "Activity sample" "activity"
And the "Mark as done" item should exist in the "Completion" dropdown of the "Activity sample" "activity"

@javascript
Scenario: Student should see the manual completion button
Given the following "activity" exists:
| activity | assign |
| name | Activity sample |
| course | C1 |
| completion | 1 |
| completionview | 0 |
When I am on the "C1" "Course" page logged in as "student1"
Then the manual completion button for "Activity sample 1" should exist
And the manual completion button of "Activity sample 1" is displayed as "Mark as done"
And I toggle the manual completion state of "Activity sample 1"
And the manual completion button of "Activity sample 1" is displayed as "Done"
Then the manual completion button for "Activity sample" should exist
And the manual completion button of "Activity sample" is displayed as "Mark as done"
And I toggle the manual completion state of "Activity sample"
And the manual completion button of "Activity sample" is displayed as "Done"

Scenario: Teacher should see the automatic completion criterias of activities
Given the following "activity" exists:
| activity | assign |
| name | Activity sample |
| course | C1 |
| completion | 2 |
| completionview | 1 |
When I am on the "C1" "Course" page logged in as "teacher1"
And I click on "Completion" "button" in the "Activity sample 2" "activity"
Then I should see "View" in the "Activity sample 2" "core_courseformat > Activity completion"
And the "View" item should exist in the "Completion" dropdown of the "Activity sample" "activity"
# After viewing the activity, the completion criteria dropdown should still display "Completion".
And I am on the "sample2" Activity page
And I am on the "Activity sample" "assign Activity" page
And I am on the "Course 1" course page
And "Completion" "button" should exist in the "Activity sample 2" "activity"
And "Completion" "button" should exist in the "Activity sample" "activity"

Scenario: Student should see the automatic completion criterias statuses of activities
Given the following "activity" exists:
| activity | assign |
| name | Activity sample |
| course | C1 |
| completion | 2 |
| completionview | 1 |
When I am on the "C1" "Course" page logged in as "student1"
And I click on "To do" "button" in the "Activity sample 2" "activity"
Then I should see "View" in the "Activity sample 2" "core_courseformat > Activity completion"
And the "View" item should exist in the "To do" dropdown of the "Activity sample" "activity"
# After viewing the activity, the completion criteria dropdown should display "Done" instead of "To do".
And I am on the "sample2" Activity page
And I am on the "Activity sample" "assign Activity" page
And I am on the "Course 1" course page
And "To do" "button" should not exist in the "Activity sample 2" "activity"
And I click on "Done" "button" in the "Activity sample 2" "activity"
And I should see "View" in the "Activity sample 2" "core_courseformat > Activity completion"
And "To do" "button" should not exist in the "Activity sample" "activity"
And the "View" item should exist in the "Done" dropdown of the "Activity sample" "activity"
22 changes: 22 additions & 0 deletions lib/behat/classes/partial_named_selector.php
Expand Up @@ -65,6 +65,7 @@ public function __construct() {
'block' => 'block',
'css_element' => 'css_element',
'dialogue' => 'dialogue',
'dropdown_item' => 'dropdown_item',
'fieldset' => 'fieldset',
'icon' => 'icon',
'list_item' => 'list_item',
Expand Down Expand Up @@ -93,6 +94,8 @@ public function __construct() {
'checkbox' => 'checkbox',
'css_element' => 'css_element',
'dialogue' => 'dialogue',
'dropdown' => 'dropdown',
'dropdown_item' => 'dropdown_item',
'field' => 'field',
'fieldset' => 'fieldset',
'file' => 'file',
Expand Down Expand Up @@ -182,6 +185,25 @@ public function __construct() {
and
normalize-space(descendant::*[contains(concat(' ', normalize-space(@class), ' '), ' modal-header ')]) = %locator%
]
XPATH
, 'dropdown' => <<<XPATH
.//*[
contains(concat(' ', normalize-space(@class), ' '), ' dropdown-menu ')
and
@aria-labelledby =
(//*[
contains(concat(' ', normalize-space(@class), ' '), ' dropdown-toggle ')
and
(contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
]/@id)
]
XPATH
, 'dropdown_item' => <<<XPATH
.//*[
@role = 'listitem'
and
(contains(normalize-space(.), %locator%) or descendant::*[%titleMatch%])
]
XPATH
, 'group_message' => <<<XPATH
.//*[@data-conversation-id]//img[contains(@alt, %locator%)]/..
Expand Down
78 changes: 78 additions & 0 deletions lib/tests/behat/behat_navigation.php
Expand Up @@ -27,6 +27,7 @@

require_once(__DIR__ . '/../../behat/behat_base.php');

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ExpectationException as ExpectationException;
use Behat\Mink\Exception\DriverException as DriverException;
use Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
Expand Down Expand Up @@ -1432,4 +1433,81 @@ public function i_turn_editing_mode_off() {
throw new ExpectationException('The edit mode could not be turned off', $this->getSession());
}
}

/**
* The named item should exist in the named dropdown.
*
* @Then /^the "(?P<item_string>[^"]*)" item should (?P<not_bool>not )?exist in the "(?P<dropdown_string>[^"]*)" dropdown$/
* @Then /^the "(?P<item_string>[^"]*)" item should (?P<not_bool>not )?exist in the "(?P<dropdown_string>[^"]*)" dropdown of the "(?P<container_string>[^"]*)" "(?P<containertype_string>[^"]*)"$/
* @param string $item The text on the dropdown menu item
* @param bool $not Whether to negate this search
* @param string $dropdown The name of the dropdown
* @param string $container The name of the container
* @param string $containertype The type of the container
*/
public function should_exist_in_dropdown(
string $item,
bool $not,
string $dropdown,
string $container = null,
string $containertype = null,
): void {
$containernode = null;
if ($container && $containertype) {
$containernode = $this->find(
selector: $containertype,
locator: $container,
node: null,
);
}
$this->should_exist_in_dropdown_in(
item: $item,
dropdown: $dropdown,
container: $containernode,
not: $not,
);
}

/**
* Helper to check whether an item exists in a dropdown.
*
* @param string $item The text of the item to look for
* @param string $dropdown The name of the dropdown
* @param null|NodeElement $container The container to look within
*/
public function should_exist_in_dropdown_in(
string $item,
string $dropdown,
null|NodeElement $container,
bool $not,
): void {
$dropdownnode = $this->find(
selector: 'named_partial',
locator: ['dropdown', $dropdown],
node: $container ?? false,
);

if ($not) {
try {
$this->find(
selector: 'named_partial',
locator: ['dropdown_item', $item],
node: $dropdownnode,
);

throw new ExpectationException(
"The '{$item}' dropdown item was found in the '{$dropdown}' selector",
$this->getSession(),
);
} catch (ElementNotFoundException $e) {
return;
}
} else {
$this->find(
selector: 'named_partial',
locator: ['dropdown_item', $item],
node: $dropdownnode,
);
}
}
}

0 comments on commit 01a1210

Please sign in to comment.