[TASK] Introduce a pagetree helper to navigate in the pagetree 53/47653/7
authorSascha Egerer <sascha@sascha-egerer.de>
Wed, 13 Apr 2016 19:53:15 +0000 (21:53 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 14 Apr 2016 08:49:00 +0000 (10:49 +0200)
The pagetree will be used in many acceptance tests. To simplyfy
the navigation a special pagetree helper is added. The helper
can simply be injected in test by codeceptions dependecy injection.

Change-Id: I38e2f9b44586c6d6de25740692cafbde52442dc8
Resolves: #75582
Releases: master
Reviewed-on: https://review.typo3.org/47653
Reviewed-by: Dirk Jüttner <dirk.juettner@gmail.com>
Tested-by: Dirk Jüttner <dirk.juettner@gmail.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Tests/Acceptance/Support/Page/PageTree.php [new file with mode: 0644]

diff --git a/typo3/sysext/core/Tests/Acceptance/Support/Page/PageTree.php b/typo3/sysext/core/Tests/Acceptance/Support/Page/PageTree.php
new file mode 100644 (file)
index 0000000..820ba65
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+namespace TYPO3\CMS\Core\Tests\Acceptance\Support\Page;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use AcceptanceTester;
+use Facebook\WebDriver\Remote\RemoteWebElement;
+use Facebook\WebDriver\WebDriverBy;
+
+/**
+ * Helper class to interact with the page tree
+ */
+class PageTree
+{
+    // Selectors
+    public static $pageTreeFrameSelector = '#typo3-pagetree';
+    public static $pageTreeSelector = '#typo3-pagetree-treeContainer';
+    public static $treeItemSelector = '.x-tree-node-ct > .x-tree-node';
+    public static $treeItemAnchorSelector = '.x-tree-node-anchor';
+
+    /**
+     * @var AcceptanceTester
+     */
+    protected $tester;
+
+    /**
+     * @param AcceptanceTester $I
+     */
+    public function __construct(\AcceptanceTester $I)
+    {
+        $this->tester = $I;
+    }
+
+    /**
+     * Open the given hierarchical path in the pagetree and click the last page.
+     *
+     * Example to open "styleuide -> elements basic" page:
+     * [
+     *    'styleguide TCA demo',
+     *    'elements basic',
+     * ]
+     *
+     * @param string[] $path
+     */
+    public function openPath(array $path)
+    {
+        $context = $this->getPageTreeElement();
+        foreach ($path as $pageName) {
+            $context = $this->ensureTreeNodeIsOpen($pageName, $context);
+        }
+        $context->findElement(\WebDriverBy::cssSelector(self::$treeItemAnchorSelector))->click();
+    }
+
+    /**
+     * Check if the pagetree is visible end return the web element object
+     *
+     * @return RemoteWebElement
+     */
+    public function getPageTreeElement()
+    {
+        $I = $this->tester;
+        $I->switchToIFrame();
+        return $I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) {
+            return $webdriver->findElement(\WebDriverBy::cssSelector(self::$pageTreeSelector));
+        });
+    }
+
+    /**
+     * Search for an element with the given link text in the provided context.
+     *
+     * @param string $nodeText
+     * @param RemoteWebElement $context
+     * @return RemoteWebElement
+     */
+    protected function ensureTreeNodeIsOpen(string $nodeText, RemoteWebElement $context)
+    {
+        $I = $this->tester;
+        $I->see($nodeText, self::$treeItemSelector);
+
+        /** @var RemoteWebElement $context */
+        $context = $I->executeInSelenium(function() use ($nodeText, $context
+        ) {
+            return $context->findElement(\WebDriverBy::linkText($nodeText))->findElement(
+                WebDriverBy::xpath('ancestor::li[@class="x-tree-node"][1]')
+            );
+        });
+
+        try {
+            $context->findElement(\WebDriverBy::cssSelector('.x-tree-elbow-end-plus'))->click();
+        } catch(\Facebook\WebDriver\Exception\NoSuchElementException $e) {
+            // element not found so it may be already opened...
+        }
+
+        return $context;
+    }
+}