From 91314b23889f0e2078cdbe580dc723a163328719 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 18 Apr 2022 13:14:03 +0200 Subject: [PATCH 1/2] Optimize single element find --- src/Element/Element.php | 47 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/Element/Element.php b/src/Element/Element.php index d6bacb082..2ff8c7f48 100644 --- a/src/Element/Element.php +++ b/src/Element/Element.php @@ -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, ?int $limit): array { if ('named' === $selector) { - $items = $this->findAll('named_exact', $locator); + $items = $this->findAllWithLimit('named_exact', $locator, $limit); if (empty($items)) { - $items = $this->findAll('named_partial', $locator); + $items = $this->findAllWithLimit('named_partial', $locator, $limit); } return $items; @@ -165,9 +158,35 @@ public function findAll($selector, $locator) $xpath = $this->selectorsHandler->selectorToXpath($selector, $locator); $xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath()); + if ($limit !== null) { + if ($limit === 1) { + $xpath = '(' . $xpath . ')[1]'; + } else { + $xpath = '(' . $xpath . ')[position() <= ' . $limit . ']'; + } + } + return $this->getDriver()->find($xpath); } + /** + * {@inheritdoc} + */ + public function find($selector, $locator) + { + $items = $this->findAllWithLimit($selector, $locator, 1); + + return count($items) > 0 ? current($items) : null; + } + + /** + * {@inheritdoc} + */ + public function findAll($selector, $locator) + { + return $this->findAllWithLimit($selector, $locator, null); + } + /** * {@inheritdoc} */ From 2a6eff2dbc9f00042b425bf7d4c1f5730355234a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 18 Apr 2022 13:18:10 +0200 Subject: [PATCH 2/2] simplify private --- src/Element/Element.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/Element/Element.php b/src/Element/Element.php index 2ff8c7f48..761528fcf 100644 --- a/src/Element/Element.php +++ b/src/Element/Element.php @@ -144,12 +144,12 @@ public function waitFor($timeout, $callback) * * @return NodeElement[] */ - private function findAllWithLimit($selector, $locator, ?int $limit): array + private function findAllWithLimit($selector, $locator, bool $firstOnly): array { if ('named' === $selector) { - $items = $this->findAllWithLimit('named_exact', $locator, $limit); + $items = $this->findAllWithLimit('named_exact', $locator, $firstOnly); if (empty($items)) { - $items = $this->findAllWithLimit('named_partial', $locator, $limit); + $items = $this->findAllWithLimit('named_partial', $locator, $firstOnly); } return $items; @@ -158,12 +158,8 @@ private function findAllWithLimit($selector, $locator, ?int $limit): array $xpath = $this->selectorsHandler->selectorToXpath($selector, $locator); $xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath()); - if ($limit !== null) { - if ($limit === 1) { - $xpath = '(' . $xpath . ')[1]'; - } else { - $xpath = '(' . $xpath . ')[position() <= ' . $limit . ']'; - } + if ($firstOnly) { + $xpath = '(' . $xpath . ')[1]'; } return $this->getDriver()->find($xpath); @@ -174,7 +170,7 @@ private function findAllWithLimit($selector, $locator, ?int $limit): array */ public function find($selector, $locator) { - $items = $this->findAllWithLimit($selector, $locator, 1); + $items = $this->findAllWithLimit($selector, $locator, true); return count($items) > 0 ? current($items) : null; } @@ -184,7 +180,7 @@ public function find($selector, $locator) */ public function findAll($selector, $locator) { - return $this->findAllWithLimit($selector, $locator, null); + return $this->findAllWithLimit($selector, $locator, false); } /**