Skip to content

Commit

Permalink
test more reserved characters
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Mar 10, 2025
1 parent d8d0548 commit 51bf724
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
7 changes: 6 additions & 1 deletion demos/_unit-test/dropdown-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/** @var App $app */
require_once __DIR__ . '/../init-app.php';

$makeTestStringFx = static fn ($v) => $v . ' <b> &lt;';
$makeTestStringFx = static fn ($v) => $v . ' <b>"\' &lt;';
$htmlValues = [
$makeTestStringFx('d') => $makeTestStringFx('dTitle'),
$makeTestStringFx('u') => $makeTestStringFx('uTitle'),
Expand Down Expand Up @@ -94,6 +94,11 @@
$form->onSubmit(static function (Form $form) use ($app, $initData, $makeTestStringFx) {
$message = $app->encodeJson($form->entity->get());

// TODO open Fomantic-UI issue, with preserveHTML = false all characters must be returned unchanged
foreach ($form->entity->get() as $k => $v) {
$form->entity->set($k, str_replace('&quot;', '"', $v));
}

$view = new Message('Values:');
$view->setApp($form->getApp());
$view->invokeInit();
Expand Down
42 changes: 24 additions & 18 deletions src/Behat/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ protected function assertNoDuplicateId(): void
}
}

private function quoteXpath(string $value): string
{
return str_contains($value, '\'')
? 'concat(\'' . str_replace('\'', '\', "\'", \'', $value) . '\')'
: '\'' . $value . '\'';
}

/**
* @return array{ 'css'|'xpath', string }
*/
Expand Down Expand Up @@ -319,7 +326,7 @@ public function iDragElementOnto(string $selector, string $selectorTarget): void
*/
public function iPressButton(string $buttonLabel): void
{
$button = $this->findElement(null, '//div[text()="' . $buttonLabel . '"]');
$button = $this->findElement(null, '//div[text()=' . $this->quoteXpath($buttonLabel) . ']');
$button->click();
}

Expand All @@ -328,15 +335,15 @@ public function iPressButton(string $buttonLabel): void
*/
public function iSeeButton(string $buttonLabel): void
{
$this->findElement(null, '//div[text()="' . $buttonLabel . '"]');
$this->findElement(null, '//div[text()=' . $this->quoteXpath($buttonLabel) . ']');
}

/**
* @Then I don't see button :arg1
*/
public function idontSeeButton(string $text): void
{
$element = $this->findElement(null, '//div[text()="' . $text . '"]');
$element = $this->findElement(null, '//div[text()=' . $this->quoteXpath($text) . ']');
if (!str_contains($element->getAttribute('style'), 'display: none')) {
throw new \Exception('Element with text "' . $text . '" must be invisible');
}
Expand All @@ -351,7 +358,7 @@ public function idontSeeButton(string $text): void
*/
public function iClickLink(string $label): void
{
$this->findElement(null, '//a[text()="' . $label . '"]')->click();
$this->findElement(null, '//a[text()=' . $this->quoteXpath($label) . ']')->click();
}

/**
Expand Down Expand Up @@ -411,7 +418,7 @@ public function iFillField(string $selector, string $value): void
public function iPressModalButton(string $buttonLabel): void
{
$modal = $this->findElement(null, '.modal.visible.active.front');
$button = $this->findElement($modal, '//div[text()="' . $buttonLabel . '"]');
$button = $this->findElement($modal, '//div[text()=' . $this->quoteXpath($buttonLabel) . ']');
$button->click();
}

Expand All @@ -423,12 +430,8 @@ public function iPressModalButton(string $buttonLabel): void
*/
public function modalIsOpenWithText(string $text, string $selector = '*'): void
{
$textEncoded = str_contains($text, '"')
? 'concat("' . str_replace('"', '", \'"\', "', $text) . '")'
: '"' . $text . '"';

$modal = $this->findElement(null, '.modal.visible.active.front');
$this->findElement($modal, '//' . $selector . '[text()[normalize-space()=' . $textEncoded . ']]');
$this->findElement($modal, '//' . $selector . '[text()[normalize-space()=' . $this->quoteXpath($text) . ']]');
}

/**
Expand Down Expand Up @@ -479,7 +482,7 @@ public function panelIsOpen(): void
public function panelIsOpenWithText(string $text, string $selector = '*'): void
{
$panel = $this->findElement(null, '.atk-right-panel.atk-visible');
$this->findElement($panel, '//' . $selector . '[text()[normalize-space()="' . $text . '"]]');
$this->findElement($panel, '//' . $selector . '[text()[normalize-space()=' . $this->quoteXpath($text) . ']]');
}

/**
Expand All @@ -498,7 +501,7 @@ public function iFillPanelField(string $fieldName, string $value): void
public function iPressPanelButton(string $buttonLabel): void
{
$panel = $this->findElement(null, '.atk-right-panel.atk-visible');
$button = $this->findElement($panel, '//div[text()="' . $buttonLabel . '"]');
$button = $this->findElement($panel, '//div[text()=' . $this->quoteXpath($buttonLabel) . ']');
$button->click();
}

Expand All @@ -512,7 +515,7 @@ public function iPressPanelButton(string $buttonLabel): void
public function iClickTabWithTitle(string $tabTitle): void
{
$tabMenu = $this->findElement(null, '.ui.tabular.menu');
$link = $this->findElement($tabMenu, '//div[text()="' . $tabTitle . '"]');
$link = $this->findElement($tabMenu, '//div[text()=' . $this->quoteXpath($tabTitle) . ']');
$link->click();
}

Expand Down Expand Up @@ -558,13 +561,16 @@ public function iSearchGridFor(string $text): void
/**
* TODO better method name, it selects name/title, not value.
*
* @When I select value :arg1 in lookup :arg2
* @When ~^I select value "((?:[^"]|\\")*)" in lookup "((?:[^"]|\\")*)"$~
*/
public function iSelectValueInLookup(string $value, string $inputName): void
{
$value = $this->unquoteStepArgument($value);
$inputName = $this->unquoteStepArgument($inputName);

// get dropdown item from Fomantic-UI which is direct parent of input HTML element
$isSelectorXpath = $this->parseSelector($inputName)[0] === 'xpath';
$lookupElem = $this->findElement(null, ($isSelectorXpath ? $inputName : '//input[@name="' . $inputName . '"]') . '/parent::div');
$lookupElem = $this->findElement(null, ($isSelectorXpath ? $inputName : '//input[@name=' . $this->quoteXpath($inputName) . ']') . '/parent::div');

if ($value === '') {
$this->findElement($lookupElem, 'i.remove.icon')->click();
Expand All @@ -578,7 +584,7 @@ public function iSelectValueInLookup(string $value, string $inputName): void
$this->jqueryWait('$(arguments[0]).hasClass(\'visible\')', [$lookupElem]);

// select value
$valueElem = $this->findElement($lookupElem, '//div.menu//div.item[text()="' . $value . '"]');
$valueElem = $this->findElement($lookupElem, '//div.menu//div.item[text()=' . $this->quoteXpath($value) . ']');
$this->getSession()->executeScript('$(arguments[0]).dropdown(\'set selected\', arguments[1]);', [$lookupElem, $valueElem->getAttribute('data-value')]);
$this->jqueryWait();

Expand All @@ -592,7 +598,7 @@ public function iSelectValueInLookup(string $value, string $inputName): void
*/
public function iSelectFile(string $inputName, string $fileContent, string $fileName): void
{
$element = $this->findElement(null, '//input[@name="' . $inputName . '" and @type="hidden"]/following-sibling::input[@type="file"]');
$element = $this->findElement(null, '//input[@name=' . $this->quoteXpath($inputName) . ' and @type="hidden"]/following-sibling::input[@type="file"]');
$this->getSession()->executeScript(<<<'EOF'
const dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([new Uint8Array(arguments[1])], arguments[2]));
Expand Down Expand Up @@ -724,7 +730,7 @@ public function compareInputValueToText(string $selector, string $text): void
*/
public function dump(string $arg1): void
{
$element = $this->getSession()->getPage()->find('xpath', '//div[text()="' . $arg1 . '"]');
$element = $this->getSession()->getPage()->find('xpath', '//div[text()=' . $this->quoteXpath($arg1) . ']');
var_dump($element->getOuterHtml());
}

Expand Down
16 changes: 8 additions & 8 deletions tests-behat/dropdown.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Feature: Dropdown
When I press button "Save"
Then Modal is open with text "match init: 1"
When I click close modal
When I select value "uTitle <b> &lt;" in lookup "dropdown_single"
When I select value "uTitle <b> &lt;" in lookup "dropdown_single2"
When I select value "uTitle <b> &lt;" in lookup "dropdown_multi"
When I select value "uTitle <b> &lt;" in lookup "dropdown_multi2"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_single"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_single2"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_multi"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_multi2"
When I press button "Save"
Then Modal is open with text "match u add: 1"
When I click close modal
Expand All @@ -44,10 +44,10 @@ Feature: Dropdown
When I press button "Save"
Then Modal is open with text "match empty: 1"
When I click close modal
When I select value "uTitle <b> &lt;" in lookup "dropdown_single"
When I select value "uTitle <b> &lt;" in lookup "dropdown_single2"
When I select value "uTitle <b> &lt;" in lookup "dropdown_multi"
When I select value "uTitle <b> &lt;" in lookup "dropdown_multi2"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_single"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_single2"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_multi"
When I select value "uTitle <b>\"' &lt;" in lookup "dropdown_multi2"
When I press button "Save"
Then Modal is open with text "match u only: 1"

Expand Down

0 comments on commit 51bf724

Please sign in to comment.