|
8 | 8 |
|
9 | 9 | namespace Ibexa\AdminUi\Behat\Component; |
10 | 10 |
|
| 11 | +use Couchbase\TimeoutException; |
11 | 12 | use Ibexa\Behat\Browser\Component\Component; |
| 13 | +use Ibexa\Behat\Browser\Element\BaseElementInterface; |
| 14 | +use Ibexa\Behat\Browser\Element\Condition\ElementExistsCondition; |
| 15 | +use Ibexa\Behat\Browser\Element\ElementInterface; |
| 16 | +use Ibexa\Behat\Browser\Exception\ElementNotFoundException; |
| 17 | +use Ibexa\Behat\Browser\Locator\CSSLocator; |
12 | 18 | use Ibexa\Behat\Browser\Locator\VisibleCSSLocator; |
| 19 | +use PHPUnit\Framework\Assert; |
13 | 20 |
|
14 | 21 | class ContentTree extends Component |
15 | 22 | { |
16 | 23 | public function verifyIsLoaded(): void |
17 | 24 | { |
18 | 25 | $this->getHTMLPage()->find($this->getLocator('header'))->assert()->textEquals('Content tree'); |
19 | | -// $this->getHTMLPage()->setTimeout(10)->find($this->getLocator('item'))->assert()->isVisible(); |
| 26 | + } |
| 27 | + public function verifyItemExists(string $itemPath): void |
| 28 | + { |
| 29 | + Assert::assertTrue($this->itemExists($itemPath)); |
| 30 | + } |
| 31 | + |
| 32 | + private function itemExists(string $itemPath): bool |
| 33 | + { |
| 34 | + |
| 35 | + $pathParts = explode('/', $itemPath); |
| 36 | + |
| 37 | + try { |
| 38 | + $this->getHTMLPage() |
| 39 | + ->setTimeout(5) |
| 40 | + ->waitUntilCondition(new ElementExistsCondition($this->getHTMLPage(), |
| 41 | + $this->getLocator('treeItem'))); |
| 42 | + } catch (TimeoutException $e) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + $searchedNode = $this->getHTMLPage()->find($itemPath); |
| 46 | + |
| 47 | + try { |
| 48 | + $this->searchForItem(end($itemPath)); |
| 49 | + } catch (TimeoutException $e) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + foreach ($pathParts as $indent => $itemPath) { |
| 53 | + try { |
| 54 | + $searchedNode = $this->findNestedTreeElement($searchedNode, $itemPath, $indent); |
| 55 | + } catch (ElementNotFoundException $e) { |
| 56 | + return false; |
| 57 | + } catch (TimeoutException $e) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + if ($itemPath !== end($itemPath)) { |
| 62 | + $searchedNode = $searchedNode->find(new VisibleCSSLocator('', '.c-tb-list')); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $this->getHTMLPage() |
| 67 | + ->setTimeout(5) |
| 68 | + ->waitUntilCondition(new ElementExistsCondition($this->getHTMLPage(), $this->getLocator('treeItem'))); |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + private function findNestedTreeElement(BaseElementInterface $baseElement, string $searchedElementName, int $indent): ElementInterface |
| 73 | + { |
| 74 | + return $baseElement->findAll($this->getLocator('treeItem')) |
| 75 | + ->filter(static function (ElementInterface $element) use ($indent): bool { |
| 76 | + return $element->findAll( |
| 77 | + new CSSLocator('', sprintf('[style*="--indent: %d;"]', $indent)) |
| 78 | + )->any(); |
| 79 | + }) |
| 80 | + ->filter(static function (ElementInterface $element) use ($searchedElementName): bool { |
| 81 | + return str_replace(' ', '', $element->find( |
| 82 | + new VisibleCSSLocator('', '.c-tb-list-item-single__element') |
| 83 | + )->getText()) === $searchedElementName; |
| 84 | + }) |
| 85 | + ->first(); |
20 | 86 | } |
21 | 87 |
|
22 | 88 | protected function specifyLocators(): array |
23 | 89 | { |
24 | 90 | return [ |
25 | 91 | new VisibleCSSLocator('header', '.ibexa-content-tree-container .c-tb-header__name-content,.c-header .c-header__name'), |
26 | | -// new VisibleCSSLocator('optionsButton', '.c-tb-contextual-menu__toggler'), |
27 | | -// new VisibleCSSLocator('menuOption', '.c-tb-action-list__item'), |
28 | | -// new VisibleCSSLocator('item', '.c-tb-list-item-single__element .c-tb-list-item-single__element--main') |
29 | | - ]; |
| 92 | + new VisibleCSSLocator('treeItem', '.c-tb-list-item-single__label'), |
| 93 | + new VisibleCSSLocator('treeElement', '.ibexa-content-tree-container__root .c-tb-list-item-single__element'), |
| 94 | + ]; |
30 | 95 | } |
31 | 96 | } |
0 commit comments