[BUGFIX] Allow any navigation component to load in BE
[Packages/TYPO3.CMS.git] / components / testing_framework / Classes / Core / Acceptance / Support / Page / PageTree.php
1 <?php
2 namespace TYPO3\TestingFramework\Core\Acceptance\Support\Page;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use AcceptanceTester;
18 use Facebook\WebDriver\Remote\RemoteWebElement;
19 use Facebook\WebDriver\WebDriverBy;
20
21 /**
22 * Helper class to interact with the page tree
23 */
24 class PageTree
25 {
26 // Selectors
27 public static $pageTreeFrameSelector = '#typo3-pagetree';
28 public static $pageTreeSelector = '#typo3-pagetree-treeContainer';
29 public static $treeItemSelector = '.x-tree-node-ct > .x-tree-node';
30 public static $treeItemAnchorSelector = '.x-tree-node-anchor';
31
32 /**
33 * @var AcceptanceTester
34 */
35 protected $tester;
36
37 /**
38 * @param AcceptanceTester $I
39 */
40 public function __construct(\AcceptanceTester $I)
41 {
42 $this->tester = $I;
43 }
44
45 /**
46 * Open the given hierarchical path in the pagetree and click the last page.
47 *
48 * Example to open "styleuide -> elements basic" page:
49 * [
50 * 'styleguide TCA demo',
51 * 'elements basic',
52 * ]
53 *
54 * @param string[] $path
55 */
56 public function openPath(array $path)
57 {
58 $context = $this->getPageTreeElement();
59 foreach ($path as $pageName) {
60 $context = $this->ensureTreeNodeIsOpen($pageName, $context);
61 }
62 $context->findElement(\WebDriverBy::cssSelector(self::$treeItemAnchorSelector))->click();
63 }
64
65 /**
66 * Check if the pagetree is visible end return the web element object
67 *
68 * @return RemoteWebElement
69 */
70 public function getPageTreeElement()
71 {
72 $I = $this->tester;
73 $I->switchToIFrame();
74 $I->waitforelementVisible(self::$pageTreeSelector);
75 return $I->executeInSelenium(function (\Facebook\WebDriver\Remote\RemoteWebDriver $webdriver) {
76 return $webdriver->findElement(\WebDriverBy::cssSelector(self::$pageTreeSelector));
77 });
78 }
79
80 /**
81 * Search for an element with the given link text in the provided context.
82 *
83 * @param string $nodeText
84 * @param RemoteWebElement $context
85 * @return RemoteWebElement
86 */
87 protected function ensureTreeNodeIsOpen(string $nodeText, RemoteWebElement $context)
88 {
89 $I = $this->tester;
90 $I->see($nodeText, self::$treeItemSelector);
91
92 /** @var RemoteWebElement $context */
93 $context = $I->executeInSelenium(function () use ($nodeText, $context
94 ) {
95 return $context->findElement(\WebDriverBy::linkText($nodeText))->findElement(
96 WebDriverBy::xpath('ancestor::li[@class="x-tree-node"][1]')
97 );
98 });
99
100 try {
101 $context->findElement(\WebDriverBy::cssSelector('.x-tree-elbow-end-plus'))->click();
102 } catch (\Facebook\WebDriver\Exception\NoSuchElementException $e) {
103 // element not found so it may be already opened...
104 }
105
106 return $context;
107 }
108 }