From 556194cc11897ac61c5a55e26a43371168e34c27 Mon Sep 17 00:00:00 2001
From: David Monllao <davidm@moodle.com>
Date: Wed, 30 Oct 2013 14:20:46 +0800
Subject: [PATCH 1/2] MDL-42236 behat: New steps to look for elements
 visibility

---
 admin/tool/behat/tests/behat/basic_actions.feature |    6 +-
 lib/tests/behat/behat_general.php                  |   94 ++++++++++++++++++++
 2 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/admin/tool/behat/tests/behat/basic_actions.feature b/admin/tool/behat/tests/behat/basic_actions.feature
index 364b54e..09de133 100644
--- a/admin/tool/behat/tests/behat/basic_actions.feature
+++ b/admin/tool/behat/tests/behat/basic_actions.feature
@@ -4,7 +4,7 @@ Feature: Page contents assertions
   As a tests writer
   I need to check the page contents
 
-  @javascript
+  @javascript @wip
   Scenario: Basic contents assertions
     Given I log in as "admin"
     And I am on homepage
@@ -20,6 +20,10 @@ Feature: Page contents assertions
     And I wait "2" seconds
     And I hover ".region-content .generaltable td span" "css_element"
     Then I should see "I'm the description"
+    And "Grouping" "select" in the "region-main" "region" should be visible
+    And "Group" "select" should be visible
+    And "Messaging" "link" in the "Administration" "block" should not be visible
+    And "Change password" "link" should not be visible
     And I should see "Filter groups by"
     And I should not see "Filter groupssss by"
     And I should see "Group members" in the ".region-content table th.c1" "css_element"
diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php
index c95940b..080ea24 100644
--- a/lib/tests/behat/behat_general.php
+++ b/lib/tests/behat/behat_general.php
@@ -276,6 +276,100 @@ class behat_general extends behat_base {
     }
 
     /**
+     * Checks, that the specified element is visible. Only available in tests using Javascript.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should be visible$/
+     * @throws ElementNotFoundException
+     * @throws ExpectationException
+     * @throws DriverException
+     * @param string $element
+     * @param string $selectortype
+     * @return void
+     */
+    public function should_be_visible($element, $selectortype) {
+
+        if (!$this->running_javascript()) {
+            throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
+        }
+
+        $node = $this->get_selected_node($selectortype, $element);
+        if (!$node->isVisible()) {
+            throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is not visible', $this->getSession());
+        }
+    }
+
+    /**
+     * Checks, that the specified element is not visible. Only available in tests using Javascript.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>(?:[^"]|\\")*)" should not be visible$/
+     * @throws ElementNotFoundException
+     * @throws ExpectationException
+     * @param string $element
+     * @param string $selectortype
+     * @return void
+     */
+    public function should_not_be_visible($element, $selectortype) {
+
+        try {
+            $this->should_be_visible($element, $selectortype);
+            throw new ExpectationException('"' . $element . '" "' . $selectortype . '" is visible', $this->getSession());
+        } catch (ExpectationException $e) {
+            // All as expected.
+        }
+    }
+
+    /**
+     * Checks, that the specified element is visible inside the specified container. Only available in tests using Javascript.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should be visible$/
+     * @throws ElementNotFoundException
+     * @throws DriverException
+     * @throws ExpectationException
+     * @param string $element Element we look for
+     * @param string $selectortype The type of what we look for
+     * @param string $nodeelement Element we look in
+     * @param string $nodeselectortype The type of selector where we look in
+     */
+    public function in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
+
+        if (!$this->running_javascript()) {
+            throw new DriverException('Visible checks are disabled in scenarios without Javascript support');
+        }
+
+        $node = $this->get_node_in_container($selectortype, $element, $nodeselectortype, $nodeelement);
+        if (!$node->isVisible()) {
+            throw new ExpectationException(
+                '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is not visible',
+                $this->getSession()
+            );
+        }
+    }
+
+    /**
+     * Checks, that the specified element is not visible inside the specified container. Only available in tests using Javascript.
+     *
+     * @Then /^"(?P<element_string>(?:[^"]|\\")*)" "(?P<selector_string>[^"]*)" in the "(?P<element_container_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)" should not be visible$/
+     * @throws ElementNotFoundException
+     * @throws ExpectationException
+     * @param string $element Element we look for
+     * @param string $selectortype The type of what we look for
+     * @param string $nodeelement Element we look in
+     * @param string $nodeselectortype The type of selector where we look in
+     */
+    public function in_the_should_not_be_visible($element, $selectortype, $nodeelement, $nodeselectortype) {
+
+        try {
+            $this->in_the_should_be_visible($element, $selectortype, $nodeelement, $nodeselectortype);
+            throw new ExpectationException(
+                '"' . $element . '" "' . $selectortype . '" in the "' . $nodeelement . '" "' . $nodeselectortype . '" is visible',
+                $this->getSession()
+            );
+        } catch (ExpectationException $e) {
+            // All as expected.
+        }
+    }
+
+    /**
      * Checks, that page contains specified text. It also checks if the text is visible when running Javascript tests.
      *
      * @Then /^I should see "(?P<text_string>(?:[^"]|\\")*)"$/
-- 
1.7.9.5

