Permalink
Browse files

MDL-40033 behat: Waiting until file manager is available

  • Loading branch information...
1 parent e978b66 commit 922a5cf370ba6042e24c0d32141ff5814a4b908b @dmonllao dmonllao committed Jun 18, 2013
View
@@ -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 @@ protected function open_add_file_window($filepickernode, $repositoryname) {
// 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);
+ }
+
}
@@ -58,8 +58,11 @@ public function i_add_file_from_recent_files_to_filepicker($filename, $filepicke
$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);
}
}
@@ -65,8 +65,11 @@ public function i_create_folder_in_filepicker($foldername, $filepickerelement) {
$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,14 +89,18 @@ public function i_open_folder_from_filepicker($foldername, $filepickerelement) {
$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 {
// In the current folder workspace.
$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 @@ public function i_open_folder_from_filepicker($foldername, $filepickerelement) {
$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 @@ public function i_open_folder_from_filepicker($foldername, $filepickerelement) {
// 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 @@ public function i_unzip_file_from_filepicker($filename, $filepickerelement) {
$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 @@ public function i_zip_folder_from_filepicker($foldername, $filepickerelement) {
$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 @@ public function i_delete_file_from_filepicker($name, $filepickerelement) {
$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);
}
}
@@ -54,20 +54,43 @@ public function i_upload_file_to_filepicker($filepath, $filepickerelement) {
$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();
+
+ // Ensure the file has been uploaded and all ajax processes finished.
+ $this->wait_until_return_to_form();
- // Wait a while for the file to be uploaded.
- $this->getSession()->wait(6 * 1000, false);
+ // Wait until file manager contents are updated.
+ $this->wait_until_contents_are_updated($filepickernode);
}
}

0 comments on commit 922a5cf

Please sign in to comment.