From 50eeae590616240bfe9069d694bce34e21fd6cc3 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 18 Jun 2013 10:50:44 +0800 Subject: [PATCH] MDL-40033 behat: Waiting until file manager is available --- lib/behat/behat_files.php | 54 ++++++++++++++++++- .../tests/behat/behat_repository_recent.php | 7 ++- repository/tests/behat/behat_filepicker.php | 45 +++++++++++----- .../tests/behat/behat_repository_upload.php | 33 ++++++++++-- 4 files changed, 117 insertions(+), 22 deletions(-) diff --git a/lib/behat/behat_files.php b/lib/behat/behat_files.php index ddfdab14706..4e8b7d171cd 100644 --- a/lib/behat/behat_files.php +++ b/lib/behat/behat_files.php @@ -31,7 +31,8 @@ require_once(__DIR__ . '/behat_base.php'); -use Behat\Mink\Exception\ExpectationException as ExpectationException; +use Behat\Mink\Exception\ExpectationException as ExpectationException, + Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException; /** * Files-related actions. @@ -187,4 +188,55 @@ class behat_files extends behat_base { // Selecting the repo. $repositorylink->click(); } + + /** + * Waits until the file manager modal windows are closed. + * + * @throws ExpectationException + * @return void + */ + protected function wait_until_return_to_form() { + + $exception = new ExpectationException('The file manager is taking too much time to finish the current action', $this->getSession()); + + $this->find( + 'xpath', + "//div[@id='filesskin']" . + "/descendant::div[contains(concat(' ', @class, ' '), ' yui3-widget-mask ')]" . + "[contains(concat(' ', @style, ' '), ' display: none; ')]", + $exception + ); + } + + /** + * Checks that the file manager contents are not being updated. + * + * @throws ExpectationException + * @param NodeElement $filepickernode The file manager DOM node + * @return void + */ + protected function wait_until_contents_are_updated($filepickernode) { + + $exception = new ExpectationException( + 'The file manager contents are requiring too much time to be updated', + $this->getSession() + ); + + // Looks for the loading image not being displayed. For single-file filepickers is + // only used when accessing the filepicker, there is no filemanager-loading after selecting the file. + $this->find( + 'xpath', + "//div[contains(concat(' ', @class, ' '), ' filemanager ')]" . + "[not(contains(concat(' ', @class, ' '), ' fm-updating '))]" . + "|" . + "//div[contains(concat(' ', @class, ' '), ' filemanager-loading ')]" . + "[contains(@style, 'display: none;')]", + $exception, + $filepickernode + ); + + // After removing the class FileManagerHelper.view_files() performs other actions. + $this->getSession()->wait(4 * 1000, false); + } + } diff --git a/repository/recent/tests/behat/behat_repository_recent.php b/repository/recent/tests/behat/behat_repository_recent.php index 2dbce305ea0..f5f3462f629 100644 --- a/repository/recent/tests/behat/behat_repository_recent.php +++ b/repository/recent/tests/behat/behat_repository_recent.php @@ -58,8 +58,11 @@ class behat_repository_recent extends behat_files { $this->find_button('Select this file')->click(); - // Wait a while for the file to be selected. - $this->getSession()->wait(3 * 1000, false); + // Ensure the file has been selected and we returned to the form page. + $this->wait_until_return_to_form(); + + // Wait until file manager contents are updated. + $this->wait_until_contents_are_updated($filepickernode); } } diff --git a/repository/tests/behat/behat_filepicker.php b/repository/tests/behat/behat_filepicker.php index 8b83ae3ecaf..e06e3864c41 100644 --- a/repository/tests/behat/behat_filepicker.php +++ b/repository/tests/behat/behat_filepicker.php @@ -65,8 +65,11 @@ class behat_filepicker extends behat_files { $this->getSession()->getPage()->pressButton('Create folder'); - // Wait few seconds for the folder to be created and filepicker contents reloaded. - $this->getSession()->wait(4 * 1000, false); + // Wait until the process finished and modal windows are hidden. + $this->wait_until_return_to_form(); + + // Wait until the current folder contents are updated + $this->wait_until_contents_are_updated($fieldnode); } /** @@ -86,6 +89,9 @@ class behat_filepicker extends behat_files { $this->getSession() ); + // Just in case there is any contents refresh in progress. + $this->wait_until_contents_are_updated($fieldnode); + // We look both in the pathbar and in the contents. try { @@ -93,7 +99,8 @@ class behat_filepicker extends behat_files { $folder = $this->find( 'xpath', "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" . - "//descendant::div[contains(concat(' ', normalize-space(.), ' '), '" . $foldername . "')]", + "/descendant::div[contains(concat(' ', @class, ' '), ' fp-filename ')]" . + "[normalize-space(.)='" . $foldername . "']", $exception, $fieldnode ); @@ -103,7 +110,7 @@ class behat_filepicker extends behat_files { $folder = $this->find( 'xpath', "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" . - "[contains(concat(' ', normalize-space(.), ' '), '" . $foldername . "')]", + "[normalize-space(.)='" . $foldername . "']", $exception, $fieldnode ); @@ -112,8 +119,8 @@ class behat_filepicker extends behat_files { // It should be a NodeElement, otherwise an exception would have been thrown. $folder->click(); - // Wait few seconds for the filepicker contents to be updated. - $this->getSession()->wait(4 * 1000, false); + // Wait until the current folder contents are updated + $this->wait_until_contents_are_updated($fieldnode); } /** @@ -133,9 +140,12 @@ class behat_filepicker extends behat_files { $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession()); $this->perform_on_element('unzip', $exception); - // Wait few seconds. - // Here the time will depend on the zip contents and the server load, so it better to be conservative. - $this->getSession()->wait(8 * 1000, false); + // Wait until the process finished and modal windows are hidden. + $this->wait_until_return_to_form(); + + // Wait until the current folder contents are updated + $containernode = $this->get_filepicker_node($filepickerelement); + $this->wait_until_contents_are_updated($containernode); } /** @@ -155,9 +165,12 @@ class behat_filepicker extends behat_files { $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession()); $this->perform_on_element('zip', $exception); - // Wait few seconds. - // Here the time will depend on the folder contents and the server load, so it better to be conservative. - $this->getSession()->wait(8 * 1000, false); + // Wait until the process finished and modal windows are hidden. + $this->wait_until_return_to_form(); + + // Wait until the current folder contents are updated + $containernode = $this->get_filepicker_node($filepickerelement); + $this->wait_until_contents_are_updated($containernode); } /** @@ -182,8 +195,12 @@ class behat_filepicker extends behat_files { $okbutton = $this->find('css', 'div.fp-dlg button.fp-dlg-butconfirm'); $okbutton->click(); - // Wait few seconds until filepicker contents are reloaded. - $this->getSession()->wait(4 * 1000, false); + // Wait until the process finished and modal windows are hidden. + $this->wait_until_return_to_form(); + + // Wait until file manager contents are updated. + $containernode = $this->get_filepicker_node($filepickerelement); + $this->wait_until_contents_are_updated($containernode); } } diff --git a/repository/upload/tests/behat/behat_repository_upload.php b/repository/upload/tests/behat/behat_repository_upload.php index e2efc158cf7..5378fc02740 100644 --- a/repository/upload/tests/behat/behat_repository_upload.php +++ b/repository/upload/tests/behat/behat_repository_upload.php @@ -54,20 +54,43 @@ class behat_repository_upload extends behat_files { $filepickernode = $this->get_filepicker_node($filepickerelement); + // Wait until file manager is completely loaded. + $this->wait_until_contents_are_updated($filepickernode); + // Opening the select repository window and selecting the upload repository. $this->open_add_file_window($filepickernode, get_string('pluginname', 'repository_upload')); + // Ensure all the form is ready. + $this->getSession()->wait(2 * 1000, false); + $noformexception = new ExpectationException('The upload file form is not ready', $this->getSession()); + $this->find( + 'xpath', + "//div[contains(concat(' ', @class, ' '), ' file-picker ')]" . + "[contains(concat(' ', @class, ' '), ' repository_upload ')]" . + "/descendant::div[@class='fp-content']" . + "/descendant::div[contains(concat(' ', @class, ' '), ' fp-upload-form ')]" . + "/descendant::form", + $noformexception + ); + // After this we have the elements we want to interact with. + + // Form elements to interact with. + $file = $this->find_file('repo_upload_file'); + $submit = $this->find_button(get_string('upload', 'repository')); + // Attaching specified file to the node. $filepath = str_replace('/', DIRECTORY_SEPARATOR, $filepath); $fileabsolutepath = $CFG->dirroot . DIRECTORY_SEPARATOR . $filepath; - $inputfilenode = $this->find_file('repo_upload_file'); - $inputfilenode->attachFile($fileabsolutepath); + $file->attachFile($fileabsolutepath); // Submit the file. - $this->getSession()->getPage()->pressButton('Upload this file'); + $submit->press(); - // Wait a while for the file to be uploaded. - $this->getSession()->wait(6 * 1000, false); + // Ensure the file has been uploaded and all ajax processes finished. + $this->wait_until_return_to_form(); + + // Wait until file manager contents are updated. + $this->wait_until_contents_are_updated($filepickernode); } }