Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,17 @@ public function waitFor($timeout, $callback)
}

/**
* {@inheritdoc}
*/
public function find($selector, $locator)
{
$items = $this->findAll($selector, $locator);

return count($items) ? current($items) : null;
}

/**
* {@inheritdoc}
* @param string $selector selector engine name
* @param string|array $locator selector locator
*
* @return NodeElement[]
*/
public function findAll($selector, $locator)
private function findAllWithLimit($selector, $locator, bool $firstOnly): array
{
if ('named' === $selector) {
$items = $this->findAll('named_exact', $locator);
$items = $this->findAllWithLimit('named_exact', $locator, $firstOnly);
if (empty($items)) {
$items = $this->findAll('named_partial', $locator);
$items = $this->findAllWithLimit('named_partial', $locator, $firstOnly);
}

return $items;
Expand All @@ -165,9 +158,31 @@ public function findAll($selector, $locator)
$xpath = $this->selectorsHandler->selectorToXpath($selector, $locator);
$xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath());

if ($firstOnly) {
$xpath = '(' . $xpath . ')[1]';
}

return $this->getDriver()->find($xpath);
}

/**
* {@inheritdoc}
*/
public function find($selector, $locator)
{
$items = $this->findAllWithLimit($selector, $locator, true);

return count($items) > 0 ? current($items) : null;
}

/**
* {@inheritdoc}
*/
public function findAll($selector, $locator)
{
return $this->findAllWithLimit($selector, $locator, false);
}

/**
* {@inheritdoc}
*/
Expand Down