From ba57c851dc089902f3bcee58f80b87a5bf5ae65b Mon Sep 17 00:00:00 2001
From: Tobias Reischmann <tobias.reischmann@wi.uni-muenster.de>
Date: Wed, 4 Sep 2019 14:52:40 +0200
Subject: [PATCH] Fix behat tests for action menues under javascript

---
 tests/behat/behat_tool_lifecycle.php | 62 +++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 2 deletions(-)

diff --git a/tests/behat/behat_tool_lifecycle.php b/tests/behat/behat_tool_lifecycle.php
index d54a2c9..456b0a3 100644
--- a/tests/behat/behat_tool_lifecycle.php
+++ b/tests/behat/behat_tool_lifecycle.php
@@ -49,6 +49,7 @@ class behat_tool_lifecycle extends behat_base {
      * @throws Exception
      */
     public function click_on_the_tool_in_the_row_of_the_table($tool, $rowname, $tablename) {
+        $this->open_or_close_action_menu($tool, $rowname, $tablename);
         $xpathelement = $this->i_should_see_the_tool_in_the_row_of_the_table($tool, $rowname, $tablename);
 
         $this->execute('behat_general::i_click_on', [$xpathelement, 'xpath_element']);
@@ -66,10 +67,14 @@ class behat_tool_lifecycle extends behat_base {
      * @throws Exception
      */
     public function i_should_see_the_tool_in_the_row_of_the_table($tool, $rowname, $tablename) {
+
         $xpathelement = $this->get_xpath_of_tool_in_table($tool, $rowname, $tablename);
 
+        $this->open_or_close_action_menu($tool, $rowname, $tablename);
+
         try {
             $this->find('xpath', $xpathelement);
+            $this->open_or_close_action_menu($tool, $rowname, $tablename);
         } catch (ElementNotFoundException $e) {
             throw new ExpectationException('"The tool "' . $tool . '"  was not found for the row "'. $rowname.
                 '" of the table ' . $tablename, $this->getSession());
@@ -175,10 +180,13 @@ class behat_tool_lifecycle extends behat_base {
      * @throws Exception
      */
     public function i_should_not_see_the_tool_in_the_row_of_the_table($tool, $rowname, $tablename) {
+        $this->open_or_close_action_menu($tool, $rowname, $tablename);
+
         $xpathelement = $this->get_xpath_of_tool_in_table($tool, $rowname, $tablename);
 
         try {
             $this->find('xpath', $xpathelement);
+            $this->open_or_close_action_menu($tool, $rowname, $tablename);
         } catch (ElementNotFoundException $e) {
             return;
         }
@@ -186,6 +194,25 @@ class behat_tool_lifecycle extends behat_base {
             '" of the table ' . $tablename, $this->getSession());
     }
 
+    /**
+     * If Javascript is active, this function tries to click on an action dropdown, to reveal the underlying actions.
+     * @param $tool string identifier of the tool
+     * @param $rowname string identifier of the row
+     * @param $tablename string identifier of the table
+     * @throws Exception
+     */
+    protected function open_or_close_action_menu($tool, $rowname, $tablename) {
+        if ($this->running_javascript()) {
+            $actionelement = $this->get_xpath_of_action_menu_in_table($tool, $rowname, $tablename);
+            try {
+            $element = $this->find('xpath', $actionelement);
+            $element->click();
+            } catch (ElementNotFoundException $e) {
+                return;
+            }
+        }
+    }
+
 
     /**
      * Build the xpath to the table element with class tablename, throws exceptions if not present.
@@ -245,7 +272,7 @@ class behat_tool_lifecycle extends behat_base {
             throw new ExpectationException('"The table ' . $tablename . ' was not found.', $this->getSession());
         }
 
-        $xpathelement = $xpathelement . "//*[contains(text(),'$rowname')]/ancestor::tr";
+        $xpathelement = $xpathelement . "//*[text()[contains(.,'$rowname')]]/ancestor::tr";
 
         try {
             $this->find('xpath', $xpathelement);
@@ -255,12 +282,43 @@ class behat_tool_lifecycle extends behat_base {
         }
 
         $xpathelement = $xpathelement . "//a[@title = '$tool']" .
-            " | " . $xpathelement. "//span[contains(text(), '$tool')]/parent::a".
+            " | " . $xpathelement. "//span[text()[contains(., '$tool')]]/parent::a".
             " | " . $xpathelement. "//button[text() = '$tool']";
 
         return $xpathelement;
     }
 
+    /**
+     * Build the xpath to the action menu and throws exceptions if either the table or the row are not present.
+     * @param $tool string identifier of the tool
+     * @param $rowname string identifier of the row
+     * @param $tablename string identifier of the table
+     * @return string xpath of the tool
+     * @throws ExpectationException
+     */
+    private function get_xpath_of_action_menu_in_table($tool, $rowname, $tablename) {
+        $xpathelement = "//table/tbody/tr[contains(@id, '$tablename')]";
+
+        try {
+            $this->find('xpath', $xpathelement);
+        } catch (ElementNotFoundException $e) {
+            throw new ExpectationException('"The table ' . $tablename . ' was not found.', $this->getSession());
+        }
+
+        $xpathelement = $xpathelement . "//*[text()[contains(.,'$rowname')]]/ancestor::tr";
+
+        try {
+            $this->find('xpath', $xpathelement);
+        } catch (ElementNotFoundException $e) {
+            throw new ExpectationException('"The row "'. $rowname.
+                '" of the table ' . $tablename . ' was not found.', $this->getSession());
+        }
+
+        $xpathelement = $xpathelement . "//a[text()[contains(.,'Action')]]";
+
+        return $xpathelement;
+    }
+
     /**
      * I should not see a tool for a entry of a table.
      *
-- 
GitLab