MDL-38184 behat: Filepicker management steps
authorDavid Monllao <davidm@moodle.com>
Thu, 28 Feb 2013 07:59:03 +0000 (15:59 +0800)
committerDavid Monllao <davidm@moodle.com>
Fri, 8 Mar 2013 03:57:15 +0000 (11:57 +0800)
Includes create/open/delete/zip/unzip
of files and folders

lib/behat/behat_base.php
lib/behat/behat_files.php
lib/tests/behat/behat_filepicker.php [new file with mode: 0644]

index 532086b..5917d88 100644 (file)
@@ -143,6 +143,9 @@ class behat_base extends Behat\MinkExtension\Context\RawMinkContext {
                     // We are in the container node.
                     if (strpos($union, '.') === 0) {
                         $union = substr($union, 1);
+                    } else if (strpos($union, '/') !== 0) {
+                        // Adding the path separator in case it is not there.
+                        $union = '/' . $union;
                     }
                     $unions[$key] = $args['node']->getXpath() . $union;
                 }
index 7ab6a5b..a17c1f0 100644 (file)
@@ -65,7 +65,8 @@ class behat_files extends behat_base {
         // Gets the ffilemanager node specified by the locator which contains the filepicker container.
         $filepickercontainer = $this->find(
             'xpath',
-            "//input[./@id = //label[contains(normalize-space(string(.)), '" . $filepickerelement . "')]/@for]/ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ') or contains(concat(' ', normalize-space(@class), ' '), ' ffilepicker ')]",
+            "//input[./@id = //label[contains(normalize-space(string(.)), '" . $filepickerelement . "')]/@for]
+//ancestor::div[contains(concat(' ', normalize-space(@class), ' '), ' ffilemanager ') or contains(concat(' ', normalize-space(@class), ' '), ' ffilepicker ')]",
             $exception
         );
 
@@ -109,32 +110,34 @@ class behat_files extends behat_base {
         // Get a filepicker element (folder or file).
         try {
 
-            // First we look at the folder as we need the contextual menu otherwise it would be opened.
+            // First we look at the folder as we need to click on the contextual menu otherwise it would be opened.
             $node = $this->find(
                 'xpath',
                 "//div[@class='fp-content']
 //descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]
+[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')][contains(normalize-space(string(.)), '" . $name . "')]
 //descendant::a[contains(concat(' ', normalize-space(@class), ' '), ' fp-contextmenu ')]
-[contains(concat(' ', normalize-space(.), ' '), '" . $name . "')]",
+",
                 $exception,
                 $filepickernode
             );
 
-        } catch (Exception $e) {
+        } catch (ExpectationException $e) {
 
+            // Here the contextual menu is hidden, we click on the thumbnail.
             $node = $this->find(
                 'xpath',
                 "//div[@class='fp-content']
-//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]
-//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]
-[contains(concat(' ', normalize-space(.), ' '), '" . $name . "')]",
+//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')][contains(normalize-space(string(.)), '" . $name . "')]
+//descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-thumbnail ')]
+",
                 $exception,
                 $filepickernode
             );
         }
 
-        // Right click opens the contextual menu in both folder and file.
-        $node->rightClick();
+        // Click opens the contextual menu when clicking on files.
+        $node->click();
     }
 
     /**
@@ -167,8 +170,8 @@ class behat_files extends behat_base {
         $repositorylink = $this->find(
             'xpath',
             "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-area ')]
-/descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]
-[contains(concat(' ', normalize-space(.), ' '), ' " . $repositoryname . "')]",
+//descendant::span[contains(concat(' ', normalize-space(@class), ' '), ' fp-repo-name ')]
+[contains(normalize-space(string(.)), '" . $repositoryname . "')]",
             $repoexception
         );
 
diff --git a/lib/tests/behat/behat_filepicker.php b/lib/tests/behat/behat_filepicker.php
new file mode 100644 (file)
index 0000000..68a79b1
--- /dev/null
@@ -0,0 +1,189 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * 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__ . '/../../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<foldername_string>(?:[^"]|\\")*)" folder in "(?P<filepicker_field_string>(?:[^"]|\\")*)" 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<foldername_string>(?:[^"]|\\")*)" folder from "(?P<filepicker_field_string>(?:[^"]|\\")*)" 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<filename_string>(?:[^"]|\\")*)" file from "(?P<filepicker_field_string>(?:[^"]|\\")*)" 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<filename_string>(?:[^"]|\\")*)" folder from "(?P<filepicker_field_string>(?:[^"]|\\")*)" 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<file_or_folder_name_string>(?:[^"]|\\")*)" from "(?P<filepicker_field_string>(?:[^"]|\\")*)" 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);
+    }
+
+}