/** * Translates provided locator into XPath. * * @param mixed $locator Current selector locator. * * @return string * @throws ElementException When used selector is broken or not implemented. */ public function translateToXPath($locator) { if (!$locator || !is_array($locator)) { throw new ElementException('Incorrect Selenium selector format', ElementException::TYPE_INCORRECT_SELECTOR); } list($selector, $locator) = each($locator); $locator = trim($locator); if ($selector == How::CLASS_NAME) { $locator = $this->_handler->xpathLiteral(' ' . $locator . ' '); return "descendant-or-self::*[@class and contains(concat(' ', normalize-space(@class), ' '), " . $locator . ')]'; } elseif ($selector == How::CSS) { return $this->_handler->selectorToXpath('css', $locator); } elseif ($selector == How::ID) { return 'descendant-or-self::*[@id = ' . $this->_handler->xpathLiteral($locator) . ']'; } elseif ($selector == How::NAME) { return 'descendant-or-self::*[@name = ' . $this->_handler->xpathLiteral($locator) . ']'; } elseif ($selector == How::TAG_NAME) { return 'descendant-or-self::' . $locator; } elseif ($selector == How::XPATH) { return $locator; } /*case How::LINK_TEXT: case How::PARTIAL_LINK_TEXT:*/ throw new ElementException(sprintf('Selector type "%s" not yet implemented', $selector), ElementException::TYPE_UNKNOWN_SELECTOR); }
public function testSelectorToXpath() { $selector = $this->getMockBuilder('Behat\\Mink\\Selector\\SelectorInterface')->getMock(); $handler = new SelectorsHandler(); $handler->registerSelector('custom_selector', $selector); $selector->expects($this->once())->method('translateToXPath')->with($locator = 'some[locator]')->will($this->returnValue($ret = '[]some[]locator')); $this->assertEquals($ret, $handler->selectorToXpath('custom_selector', $locator)); $this->setExpectedException('InvalidArgumentException'); $handler->selectorToXpath('undefined', 'asd'); }
function it_assumes_a_css_selector_if_not_specified(Session $session, Factory $factory, SelectorsHandler $selectorsHandler) { $this->beAnInstanceOf('spec\\SensioLabs\\Behat\\PageObjectExtension\\PageObject\\MySimpleElement'); $this->beConstructedWith($session, $factory); $selectorsHandler->selectorToXpath('css', 'div#my-box')->willReturn('//div[@id="my-box"]'); $this->getXpath()->shouldReturn('//div[@id="my-box"]'); }
/** * {@inheritdoc} */ public function findAll($selector, $locator) { if ('named' === $selector) { $items = $this->findAll('named_exact', $locator); if (empty($items)) { $items = $this->findAll('named_partial', $locator); } return $items; } $xpath = $this->selectorsHandler->selectorToXpath($selector, $locator); $xpath = $this->xpathManipulator->prepend($xpath, $this->getXpath()); return $this->getDriver()->find($xpath); }
/** * @param string|array $selector * @param SelectorsHandler $selectorsHandler * * @return string */ private function getSelectorAsXpath($selector, SelectorsHandler $selectorsHandler) { $selectorType = is_array($selector) ? key($selector) : 'css'; $locator = is_array($selector) ? $selector[$selectorType] : $selector; return $selectorsHandler->selectorToXpath($selectorType, $locator); }