Skip to content

Commit

Permalink
[Behat] Removed hardcoded button positions
Browse files Browse the repository at this point in the history
  • Loading branch information
mnocon committed Nov 7, 2023
1 parent 161f563 commit 93c74d1
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/lib/Behat/Component/Fields/RichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

namespace Ibexa\AdminUi\Behat\Component\Fields;

use Behat\Mink\Session;
use Ibexa\Behat\Browser\Element\Condition\ElementExistsCondition;
use Ibexa\Behat\Browser\Element\Criterion\ElementTextCriterion;
use Ibexa\Behat\Browser\Element\ElementInterface;
use Ibexa\Behat\Browser\Element\Mapper\ElementTextMapper;
use Ibexa\Behat\Browser\Locator\VisibleCSSLocator;
use Ibexa\FieldTypeRichText\Configuration\Provider\CustomStyle;
use PHPUnit\Framework\Assert;

class RichText extends FieldTypeComponent
Expand All @@ -29,15 +27,6 @@ class RichText extends FieldTypeComponent
'Heading 6' => 'h6',
];

/** @var \Ibexa\FieldTypeRichText\Configuration\Provider\CustomStyle */
private $customStyleProvider;

public function __construct(Session $session, CustomStyle $customStyleProvider)
{
parent::__construct($session);
$this->customStyleProvider = $customStyleProvider;
}

private function getFieldInput(): ElementInterface
{
return $this->getHTMLPage()->find($this->parentLocator)->find($this->getLocator('fieldInput'));
Expand Down Expand Up @@ -141,16 +130,14 @@ public function addUnorderedList(array $listElements): void

public function clickEmbedInlineButton(): void
{
$buttonPosition = 11 + $this->getCustomStylesOffset();
$this->openElementsToolbar();
$this->clickElementsToolbarButton($buttonPosition);
$this->clickElementsToolbarButton('Embed inline');
}

public function clickEmbedButton(): void
{
$buttonPosition = 8 + $this->getCustomStylesOffset();
$this->openElementsToolbar();
$this->clickElementsToolbarButton($buttonPosition);
$this->clickElementsToolbarButton('Embed');
}

public function equalsEmbedInlineItem($itemName): bool
Expand Down Expand Up @@ -194,20 +181,13 @@ private function executeCommand(string $commandName): void
$this->getSession()->executeScript($script);
}

private function getCustomStylesOffset(): int
{
return count(array_filter($this->customStyleProvider->getConfiguration(), static function (array $config): bool {
return $config['inline'] === true;
}));
}

private function clickElementsToolbarButton(int $buttonPosition): void
private function clickElementsToolbarButton(string $buttonText): void
{
$script = sprintf(
"document.querySelectorAll('%s %s')[%d].click()",
"Array.from(document.querySelectorAll('%s %s').filter(e => e.textContent =='%s')[0].click()",
$this->getLocator('additionalToolbar')->getSelector(),
$this->getLocator('toolbarElement')->getSelector(),
$buttonPosition,
$buttonText
);

$this->getSession()->executeScript($script);
Expand Down

0 comments on commit 93c74d1

Please sign in to comment.