/** * 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 testXpathLiteral() { $handler = new SelectorsHandler(); $this->assertEquals("'some simple string'", $handler->xpathLiteral('some simple string')); $this->assertEquals("'some \"d-brackets\" string'", $handler->xpathLiteral('some "d-brackets" string')); $this->assertEquals("\"some 's-brackets' string\"", $handler->xpathLiteral('some \'s-brackets\' string')); $this->assertEquals('concat(\'some \',"\'",\'s-brackets\',"\'",\' and "d-brackets" string\')', $handler->xpathLiteral('some \'s-brackets\' and "d-brackets" string')); }
/** * @dataProvider getSelectorTests */ public function testSelectors($fixtureFile, $selector, $locator, $expectedExactCount, $expectedPartialCount = null) { $expectedCount = $this->allowPartialMatch() && null !== $expectedPartialCount ? $expectedPartialCount : $expectedExactCount; $dom = new \DOMDocument('1.0', 'UTF-8'); $dom->loadHTMLFile(__DIR__ . '/fixtures/' . $fixtureFile); // Escape the locator as Mink 1.x expects the caller of the NamedSelector to handle it $selectorsHandler = new SelectorsHandler(); $locator = $selectorsHandler->xpathLiteral($locator); $namedSelector = $this->getSelector(); $xpath = $namedSelector->translateToXPath(array($selector, $locator)); $domXpath = new \DOMXPath($dom); $nodeList = $domXpath->query($xpath); $this->assertEquals($expectedCount, $nodeList->length); }
/** * @group legacy */ public function testXpathLiteral() { $handler = new SelectorsHandler(); $this->assertEquals("'some simple string'", $handler->xpathLiteral('some simple string')); }
public function testXpathLiteral() { $handler = new SelectorsHandler(); $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); $this->assertEquals("'some simple string'", $handler->xpathLiteral('some simple string')); }