From d87bbb3d3dc83357c411ca466bdcb429915f3100 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Mon, 1 Apr 2013 12:16:57 +0800 Subject: [PATCH] MDL-38814 behat: Move filepicker steps definitions to repository/ --- repository/tests/behat/behat_filepicker.php | 189 ++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 repository/tests/behat/behat_filepicker.php diff --git a/repository/tests/behat/behat_filepicker.php b/repository/tests/behat/behat_filepicker.php new file mode 100644 index 00000000000..10c9b3f0241 --- /dev/null +++ b/repository/tests/behat/behat_filepicker.php @@ -0,0 +1,189 @@ +. + +/** + * Files and filepicker manipulation steps definitions. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../../lib/behat/behat_files.php'); + +use Behat\Mink\Exception\ExpectationException as ExpectationException; + +/** + * Steps definitions to deal with the filepicker. + * + * Extends behat_files rather than behat_base as is file-related. + * + * @package core + * @category test + * @copyright 2013 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_filepicker extends behat_files { + + /** + * Creates a folder with specified name in the current folder and in the specified filepicker field. + * + * @Given /^I create "(?P(?:[^"]|\\")*)" folder in "(?P(?:[^"]|\\")*)" filepicker$/ + * @throws ExpectationException Thrown by behat_base::find + * @param string $foldername + * @param string $filepickerelement + */ + public function i_create_folder_in_filepicker($foldername, $filepickerelement) { + + $fieldnode = $this->get_filepicker_node($filepickerelement); + + // Looking for the create folder button inside the specified filepicker. + $exception = new ExpectationException('No folders can be created in "'.$filepickerelement.'" filepicker', $this->getSession()); + $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode); + $newfolder->click(); + + // Setting the folder name in the modal window. + $exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession()); + $dialoginput = $this->find('css', '.fp-mkdir-dlg-text input'); + $dialoginput->setValue($foldername); + + $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); + } + + /** + * Opens the contents of a filepicker folder. It looks for the folder in the current folder and in the path bar. + * + * @Given /^I open "(?P(?:[^"]|\\")*)" folder from "(?P(?:[^"]|\\")*)" filepicker$/ + * @throws ExpectationException Thrown by behat_base::find + * @param string $foldername + * @param string $filepickerelement + */ + public function i_open_folder_from_filepicker($foldername, $filepickerelement) { + + $fieldnode = $this->get_filepicker_node($filepickerelement); + + $exception = new ExpectationException( + 'The "'.$foldername.'" folder can not be found in the "'.$filepickerelement.'" filepicker', + $this->getSession() + ); + + // 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 . "')]", + $exception, + $fieldnode + ); + } catch (ExpectationException $e) { + + // And in the pathbar. + $folder = $this->find( + 'xpath', + "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')] +[contains(concat(' ', normalize-space(.), ' '), '" . $foldername . "')]", + $exception, + $fieldnode + ); + } + + // 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); + } + + /** + * Unzips the specified file from the specified filepicker field. The zip file has to be visible in the current folder. + * + * @Given /^I unzip "(?P(?:[^"]|\\")*)" file from "(?P(?:[^"]|\\")*)" filepicker$/ + * @throws ExpectationException Thrown by behat_base::find + * @param string $filename + * @param string $filepickerelement + */ + public function i_unzip_file_from_filepicker($filename, $filepickerelement) { + + // Open the contextual menu of the filepicker element. + $this->open_element_contextual_menu($filename, $filepickerelement); + + // Execute the action. + $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); + } + + /** + * Zips the specified folder from the specified filepicker field. The folder has to be in the current folder. + * + * @Given /^I zip "(?P(?:[^"]|\\")*)" folder from "(?P(?:[^"]|\\")*)" filepicker$/ + * @throws ExpectationException Thrown by behat_base::find + * @param string $foldername + * @param string $filepickerelement + */ + public function i_zip_folder_from_filepicker($foldername, $filepickerelement) { + + // Open the contextual menu of the filepicker element. + $this->open_element_contextual_menu($foldername, $filepickerelement); + + // Execute the action. + $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); + } + + /** + * Deletes the specified file or folder from the specified filepicker field. + * + * @Given /^I delete "(?P(?:[^"]|\\")*)" from "(?P(?:[^"]|\\")*)" filepicker$/ + * @throws ExpectationException Thrown by behat_base::find + * @param string $foldername + * @param string $filepickerelement + */ + public function i_delete_file_from_filepicker($name, $filepickerelement) { + + // Open the contextual menu of the filepicker element. + $this->open_element_contextual_menu($name, $filepickerelement); + + // Execute the action. + $exception = new ExpectationException($name.' element can not be deleted', $this->getSession()); + $this->perform_on_element('delete', $exception); + + // Yes, we are sure. + // Using xpath + click instead of pressButton as 'Ok' it is a common string. + $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); + } + +}