Skip to content

Commit

Permalink
MDL-73853 core_h5p: Improve behat tests, to avoid random errors
Browse files Browse the repository at this point in the history
An improvement has been added to the H5P tests to avoid random errors
due to the fact that H5P content needs a while to be displayed.

Co-author: Ferran Recio <[email protected]>
  • Loading branch information
sarjona committed Nov 9, 2022
1 parent 288963e commit f23fe51
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
25 changes: 20 additions & 5 deletions h5p/js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,24 @@ H5PEmbedCommunicator = (function() {
return (window.postMessage && window.addEventListener ? new Communicator() : undefined);
})();

document.onreadystatechange = function() {
var getH5PObject = async (iFrame) => {
var H5P = iFrame.contentWindow.H5P;
if (H5P?.instances?.[0]) {
return H5P;
}

// In some cases, the H5P takes a while to be initialized (which causes some random behat failures).
const sleep = (delay) => new Promise((resolve) => setTimeout(resolve, delay));
let remainingAttemps = 10;
while (!H5P?.instances?.[0] && remainingAttemps > 0) {
await sleep(100);
H5P = iFrame.contentWindow.H5P;
remainingAttemps--;
}
return H5P;
};

document.onreadystatechange = async() => {
// Wait for instances to be initialize.
if (document.readyState !== 'complete') {
return;
Expand All @@ -108,10 +125,8 @@ document.onreadystatechange = function() {
if (!iFrame || !iFrame.contentWindow) {
return;
}
var H5P = iFrame.contentWindow.H5P;

// Check for H5P instances.
if (!H5P || !H5P.instances || !H5P.instances[0]) {
var H5P = await getH5PObject(iFrame);
if (!H5P?.instances?.[0]) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions mod/h5pactivity/tests/behat/recent_activity.feature
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@mod @mod_h5pactivity @core_h5p @_file_upload @_switch_iframe
@mod @mod_h5pactivity @core_h5p @_file_upload @_switch_iframe @javascript
Feature: Users can see the H5P recent activity from the recent activity block
In order to quickly see the updates from H5P activity in my course
As a user
Expand Down Expand Up @@ -31,21 +31,22 @@ Feature: Users can see the H5P recent activity from the recent activity block
And I add the "Recent activity" block
And I log out
And I am on the "Awesome H5P package" "h5pactivity activity" page logged in as student1
# The H5P content needs some time to be displayed (so better to wait for 1 second to avoid random errors).
And I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And I click on "Wrong one" "text" in the ".h5p-question-content" "css_element"
And I click on "Check" "button" in the ".h5p-question-buttons" "css_element"
And I switch to the main frame
And I log out
And I am on the "Awesome H5P package" "h5pactivity activity" page logged in as student2
# The H5P content needs some time to be displayed (so better to wait for 1 second to avoid random errors).
And I switch to "h5p-player" class iframe
And I switch to "h5p-iframe" class iframe
And I click on "Correct one" "text" in the ".h5p-question-content" "css_element"
And I click on "Check" "button" in the ".h5p-question-buttons" "css_element"
And I switch to the main frame
And I log out

@javascript
Scenario: Student see only his own activity
Given I am on the "Course 1" course page logged in as student1
And I should see "H5P submitted:" in the "Recent activity" "block"
Expand All @@ -58,7 +59,6 @@ Feature: Users can see the H5P recent activity from the recent activity block
And I should not see "Grade:"
And I should not see "Student 2 - "

@javascript
Scenario: Teacher see each student activity
Given I am on the "Course 1" course page logged in as teacher1
And I should see "H5P submitted:" in the "Recent activity" "block"
Expand Down

0 comments on commit f23fe51

Please sign in to comment.