Skip to content

Commit

Permalink
CC-32092 Fixed html parser for table helper (#10665)
Browse files Browse the repository at this point in the history
CC-32092 Fixed html parser for table helper
  • Loading branch information
artem-png authored Dec 5, 2023
1 parent 9d12a94 commit b366934
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 30 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"spryker/json-schema": "^1.0.0",
"spryker/propel-orm": "*",
"spryker/silex": "*",
"spryker/twig": "^3.0.0"
"spryker/twig": "^3.0.0",
"symfony/dom-crawler": "*"
},
"suggest": {
"spryker/silex": "Add this when you still want to use the abandoned Silex."
Expand Down
5 changes: 5 additions & 0 deletions dependency.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"include-dev": {
"symfony/dom-crawler": "Required if you want to use TableHelper."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Codeception\Module;
use Codeception\TestInterface;
use SprykerTest\Zed\Application\Helper\ApplicationHelperTrait;
use Symfony\Component\DomCrawler\Crawler;

class TableHelper extends Module
{
Expand Down Expand Up @@ -132,46 +133,35 @@ public function clickDataTableDeleteButton(int $rowPosition = 0): void
*/
public function clickDataTableButton(string $name, int $rowPosition = 0): void
{
if (!isset($this->currentData['data'])) {
$dataSet = $this->currentData['data'] ?? null;
if (!$dataSet) {
$this->fail('Data for table not set; Run successful ->listDataTable before');
}

if (count($this->currentData['data']) < $rowPosition) {
if (!isset($dataSet[$rowPosition])) {
$this->fail(sprintf(
'Current data set has only "%d" number of entries. The requested row "%d" doesn\'t exists.',
count($this->currentData['data']),
$rowPosition,
'Current data set has only "%d" entries. Requested row "%d" does not exist.',
count($dataSet),
$rowPosition
));
}

$rowData = $this->currentData['data'][$rowPosition];

$rowData = array_reverse($rowData);

foreach ($rowData as $rowContent) {
$xhtml = sprintf('<html>%s</html>', $rowContent);
$xhtml = simplexml_load_string($xhtml);
$xpath = sprintf('//a[contains(., "%1$s")] | //button[contains(., "%1$s")]', $name);

$elements = $xhtml->xpath($xpath);
$link = null;
if ($elements) {
$element = current($xhtml->xpath($xpath));
/** @var \SimpleXMLElement $attributes */
$attributes = (array)$element->attributes();
$rowData = $dataSet[$rowPosition];
$crawler = new Crawler('<html>' . implode('', array_reverse($rowData)) . '</html>');
$xpath = sprintf('//a[contains(., "%s")] | //button[contains(., "%s")]', $name, $name);

$link = $attributes['@attributes']['href'];
}

if ($link !== null) {
$this->getApplicationHelper()->amOnPage($link);
$this->getApplicationHelper()->seeResponseCodeIs(200);
$linkElement = $crawler->filterXPath($xpath)->first();
if ($linkElement->count() === 0) {
$this->fail(sprintf('Couldn\'t find "%s" link in row "%d"', $name, $rowPosition));
}

return;
}
$link = $linkElement->attr('href');
if ($link === null) {
$this->fail(sprintf('Found "%s" element does not have an "href" attribute in row "%d"', $name, $rowPosition));
}

$this->fail(sprintf('Couldn\'t find "%s" link in row "%d"', $name, $rowPosition));
$this->getApplicationHelper()->amOnPage($link);
$this->getApplicationHelper()->seeResponseCodeIs(200);
}

/**
Expand Down

0 comments on commit b366934

Please sign in to comment.