diff --git a/composer.json b/composer.json index bcf367c..acab3f1 100755 --- a/composer.json +++ b/composer.json @@ -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." diff --git a/dependency.json b/dependency.json new file mode 100644 index 0000000..500b33b --- /dev/null +++ b/dependency.json @@ -0,0 +1,5 @@ +{ + "include-dev": { + "symfony/dom-crawler": "Required if you want to use TableHelper." + } +} diff --git a/tests/SprykerTest/Zed/Testify/_support/Helper/Communication/Table/TableHelper.php b/tests/SprykerTest/Zed/Testify/_support/Helper/Communication/Table/TableHelper.php index a2006c2..bf502f8 100644 --- a/tests/SprykerTest/Zed/Testify/_support/Helper/Communication/Table/TableHelper.php +++ b/tests/SprykerTest/Zed/Testify/_support/Helper/Communication/Table/TableHelper.php @@ -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 { @@ -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('%s', $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('' . implode('', array_reverse($rowData)) . ''); + $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); } /**