MDL-38547 behat: New generic steps definitions
authorDavid Monllao <davidm@moodle.com>
Tue, 19 Mar 2013 08:29:29 +0000 (16:29 +0800)
committerDavid Monllao <davidm@moodle.com>
Tue, 19 Mar 2013 08:29:29 +0000 (16:29 +0800)
For advanced users as specified in the step
description, they can be used to match any locator
using any selector type

lib/tests/behat/behat_general.php

index 67d773e..b0481dd 100644 (file)
@@ -27,7 +27,8 @@
 
 require_once(__DIR__ . '/../../behat/behat_base.php');
 
-use Behat\Mink\Exception\ExpectationException as ExpectationException;
+use Behat\Mink\Exception\ExpectationException as ExpectationException,
+    Behat\Mink\Exception\ElementNotFoundException as ElementNotFoundException;
 
 /**
  * Cross component steps definitions.
@@ -216,4 +217,40 @@ class behat_general extends behat_base {
         }
     }
 
+    /**
+     * Checks the provided element and selector type exists in the current page. This step is for advanced users, use it if you don't find anything else suitable for what you need.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should exists$/
+     * @throws ElementNotFoundException Thrown by behat_base::find
+     * @param string $element The locator of the specified selector
+     * @param string $selectortype The selector type
+     */
+    public function should_exists($element, $selectortype) {
+
+        // Getting Mink selector and locator.
+        list($selector, $locator) = $this->transform_selector($selectortype, $element);
+
+        // Will throw an ElementNotFoundException if it does not exist.
+        $this->find($selector, $locator);
+    }
+
+    /**
+     * Checks that the provided element and selector type not exists in the current page. This step is for advanced users, use it if you don't find anything else suitable for what you need.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" should not exists$/
+     * @throws ExpectationException
+     * @param string $element The locator of the specified selector
+     * @param string $selectortype The selector type
+     */
+    public function should_not_exists($element, $selectortype) {
+
+        try {
+            $this->should_exists($element, $selectortype);
+            throw new ExpectationException('The "' . $element . '" "' . $selectortype . '" exists in the current page', $this->getSession());
+        }catch (ElementNotFoundException $e) {
+            // It passes.
+            return;
+        }
+    }
+
 }