/** * 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); }
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"]'); }
/** * @group legacy */ public function testBcLayer() { $selector = $this->getMockBuilder('Behat\\Mink\\Selector\\SelectorInterface')->getMock(); $handler = new SelectorsHandler(); $handler->registerSelector('named_partial', $selector); $this->assertSame($selector, $handler->getSelector('named')); }
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')); }
public function testBcLayer() { $selector = $this->getMockBuilder('Behat\\Mink\\Selector\\SelectorInterface')->getMock(); $handler = new SelectorsHandler(); $handler->registerSelector('named_partial', $selector); $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); $this->assertSame($selector, $handler->getSelector('named')); }
/** * @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); }
protected function mockNamedFinder($xpath, array $results, $locator, $times = 2) { if (!is_array($results[0])) { $results = array($results, array()); } // In case of empty results, a second call will be done using the partial selector $processedResults = array(); foreach ($results as $result) { $processedResults[] = $result; if (empty($result)) { $processedResults[] = $result; $times++; } } $returnValue = call_user_func_array(array($this, 'onConsecutiveCalls'), $processedResults); $this->driver->expects($this->exactly($times))->method('find')->with('//html' . $xpath)->will($returnValue); $this->selectors->expects($this->exactly($times))->method('selectorToXpath')->with($this->logicalOr('named_exact', 'named_partial'), $locator)->will($this->returnValue($xpath)); }
/** * {@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); }
public function testXpathLiteral() { $handler = new SelectorsHandler(); $this->assertEquals("'some simple string'", $handler->xpathLiteral('some simple string')); }