/** * @param $page * @param $selector * @param bool $throwMalformed * @return array */ protected function match($page, $selector, $throwMalformed = true) { if (is_array($selector)) { try { return $page->findElements($this->getStrictLocator($selector)); } catch (InvalidSelectorException $e) { throw new MalformedLocatorException(key($selector) . ' => ' . reset($selector), "Strict locator"); } } if ($selector instanceof WebDriverBy) { try { return $page->findElements($selector); } catch (InvalidSelectorException $e) { throw new MalformedLocatorException(sprintf("WebDriverBy::%s('%s')", $selector->getMechanism(), $selector->getValue()), 'WebDriver'); } } $isValidLocator = false; $nodes = []; try { if (Locator::isID($selector)) { $isValidLocator = true; $nodes = $page->findElements(WebDriverBy::id(substr($selector, 1))); } if (empty($nodes) and Locator::isCSS($selector)) { $isValidLocator = true; $nodes = $page->findElements(WebDriverBy::cssSelector($selector)); } if (empty($nodes) and Locator::isXPath($selector)) { $isValidLocator = true; $nodes = $page->findElements(WebDriverBy::xpath($selector)); } } catch (InvalidSelectorException $e) { throw new MalformedLocatorException($selector); } if (!$isValidLocator and $throwMalformed) { throw new MalformedLocatorException($selector); } return $nodes; }
/** * @param $selector * @return WebDriverBy * @throws \InvalidArgumentException */ protected function getLocator($selector) { if ($selector instanceof WebDriverBy) { return $selector; } if (is_array($selector)) { return $this->getStrictLocator($selector); } if (Locator::isID($selector)) { return WebDriverBy::id(substr($selector, 1)); } if (Locator::isCSS($selector)) { return WebDriverBy::cssSelector($selector); } if (Locator::isXPath($selector)) { return WebDriverBy::xpath($selector); } throw new \InvalidArgumentException("Only CSS or XPath allowed"); }
/** * @param $selector * * @return Crawler */ protected function match($selector) { if (is_array($selector)) { return $this->strictMatch($selector); } try { $selector = CssSelector::toXPath($selector); } catch (ParseException $e) { } if (!Locator::isXPath($selector)) { return null; } return @$this->crawler->filterXPath($selector); }
public function grabTextFrom($cssOrXPathOrRegex) { $el = null; if (Locator::isCSS($cssOrXPathOrRegex)) { $el = $this->session->getPage()->find('css', $cssOrXPathOrRegex); if ($el) { return $el->getText(); } } if (!$el and Locator::isXPath($cssOrXPathOrRegex)) { $el = @$this->session->getPage()->find('xpath', $cssOrXPathOrRegex); if ($el) { return $el->getText(); } } if (@preg_match($cssOrXPathOrRegex, $this->session->getPage()->getContent(), $matches)) { return $matches[1]; } throw new ElementNotFound($cssOrXPathOrRegex, 'CSS or XPath or Regex'); }
/** * @param $locator * @return Crawler */ protected function filterByXPath($locator) { if (!Locator::isXPath($locator)) { throw new MalformedLocatorException($locator, 'xpath'); } return $this->getCrawler()->filterXPath($locator); }
/** * @param $selector * * @return Crawler */ protected function match($selector) { if (is_array($selector)) { return $this->strictMatch($selector); } try { $selector = CssSelector::toXPath($selector); } catch (ParseException $e) { } if (!Locator::isXPath($selector)) { codecept_debug("XPath `{$selector}` is malformed!"); return new \Symfony\Component\DomCrawler\Crawler(); } return @$this->crawler->filterXPath($selector); }
/** * @param $page * @param $selector * @return array */ protected function match($page, $selector) { $nodes = array(); if (is_array($selector)) { return $page->findElements($this->getWebDriverLocator($selector)); } if ($selector instanceof \WebDriverBy) { return $page->findElements($selector); } if (Locator::isID($selector)) { $nodes = $page->findElements(\WebDriverBy::id(substr($selector, 1))); } if (!empty($nodes)) { return $nodes; } if (Locator::isCSS($selector)) { $nodes = $page->findElements(\WebDriverBy::cssSelector($selector)); } if (!empty($nodes)) { return $nodes; } if (Locator::isXPath($selector)) { $nodes = $page->findElements(\WebDriverBy::xpath($selector)); } return $nodes; }
public function testIsXPath() { $this->assertTrue(Locator::isXPath("//hr[@class='edge' and position()=1]")); $this->assertFalse(Locator::isXPath("and position()=1]")); $this->assertTrue(Locator::isXPath('//table[parent::div[@class="pad"] and not(@id)]//a')); }
protected function match($selector) { try { $selector = CssSelector::toXPath($selector); } catch (ParseException $e) { } if (!Locator::isXPath($selector)) return null; return @$this->crawler->filterXPath($selector); }
/** * @param $page * @param $selector * @return array */ protected function match($page, $selector) { $nodes = array(); if (Locator::isID($selector)) $nodes = $page->findElements(\WebDriverBy::id(substr($selector, 1))); if (!empty($nodes)) return $nodes; if (Locator::isCSS($selector)) $nodes = $page->findElements(\WebDriverBy::cssSelector($selector)); if (!empty($nodes)) return $nodes; if (Locator::isXPath($selector)) $nodes = $page->findElements(\WebDriverBy::xpath($selector)); return $nodes; }