From f23fe5195a3fef7c5272ff96cb9cb2f2e5c0e7ad Mon Sep 17 00:00:00 2001 From: Sara Arjona Date: Tue, 8 Nov 2022 12:06:28 +0100 Subject: [PATCH] MDL-73853 core_h5p: Improve behat tests, to avoid random errors 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 --- h5p/js/embed.js | 25 +++++++++++++++---- .../tests/behat/recent_activity.feature | 6 ++--- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/h5p/js/embed.js b/h5p/js/embed.js index 87cb99663c9..892adcfa5c7 100644 --- a/h5p/js/embed.js +++ b/h5p/js/embed.js @@ -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; @@ -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; } diff --git a/mod/h5pactivity/tests/behat/recent_activity.feature b/mod/h5pactivity/tests/behat/recent_activity.feature index f28045fa0dc..d2250f85d2f 100644 --- a/mod/h5pactivity/tests/behat/recent_activity.feature +++ b/mod/h5pactivity/tests/behat/recent_activity.feature @@ -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 @@ -31,6 +31,7 @@ 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" @@ -38,6 +39,7 @@ Feature: Users can see the H5P recent activity from the recent activity block 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" @@ -45,7 +47,6 @@ Feature: Users can see the H5P recent activity from the recent activity block 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" @@ -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"