Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize single element find #827

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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]';
}

mvorisek marked this conversation as resolved.
Show resolved Hide resolved
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