4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DomCrawler\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DomCrawler\Crawler;
17 class CrawlerTest extends TestCase
19 public function testConstructor()
21 $crawler = new Crawler();
22 $this->assertCount(0, $crawler, '__construct() returns an empty crawler');
24 $doc = new \DOMDocument();
25 $node = $doc->createElement('test');
27 $crawler = new Crawler($node);
28 $this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
31 public function testGetUri()
33 $uri = 'http://symfony.com';
34 $crawler = new Crawler(null, $uri);
35 $this->assertEquals($uri, $crawler->getUri());
38 public function testGetBaseHref()
40 $baseHref = 'http://symfony.com';
41 $crawler = new Crawler(null, null, $baseHref);
42 $this->assertEquals($baseHref, $crawler->getBaseHref());
45 public function testAdd()
47 $crawler = new Crawler();
48 $crawler->add($this->createDomDocument());
49 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMDocument');
51 $crawler = new Crawler();
52 $crawler->add($this->createNodeList());
53 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList');
56 foreach ($this->createNodeList() as $node) {
59 $crawler = new Crawler();
61 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an array of nodes');
63 $crawler = new Crawler();
64 $crawler->add($this->createNodeList()->item(0));
65 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNode');
67 $crawler = new Crawler();
68 $crawler->add('<html><body>Foo</body></html>');
69 $this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string');
73 * @expectedException \InvalidArgumentException
75 public function testAddInvalidType()
77 $crawler = new Crawler();
82 * @expectedException \InvalidArgumentException
83 * @expectedExceptionMessage Attaching DOM nodes from multiple documents in the same crawler is forbidden.
85 public function testAddMultipleDocumentNode()
87 $crawler = $this->createTestCrawler();
88 $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
91 public function testAddHtmlContent()
93 $crawler = new Crawler();
94 $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
96 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
99 public function testAddHtmlContentWithBaseTag()
101 $crawler = new Crawler();
103 $crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
105 $this->assertEquals('http://symfony.com', $crawler->filterXPath('//base')->attr('href'), '->addHtmlContent() adds nodes from an HTML string');
106 $this->assertEquals('http://symfony.com/contact', $crawler->filterXPath('//a')->link()->getUri(), '->addHtmlContent() adds nodes from an HTML string');
110 * @requires extension mbstring
112 public function testAddHtmlContentCharset()
114 $crawler = new Crawler();
115 $crawler->addHtmlContent('<html><div class="foo">Tiếng Việt</html>', 'UTF-8');
117 $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
120 public function testAddHtmlContentInvalidBaseTag()
122 $crawler = new Crawler(null, 'http://symfony.com');
124 $crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8');
126 $this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute');
129 public function testAddHtmlContentUnsupportedCharset()
131 $crawler = new Crawler();
132 $crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/windows-1250.html'), 'Windows-1250');
134 $this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
138 * @requires extension mbstring
140 public function testAddHtmlContentCharsetGbk()
142 $crawler = new Crawler();
143 //gbk encode of <html><p>中文</p></html>
144 $crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
146 $this->assertEquals('中文', $crawler->filterXPath('//p')->text());
149 public function testAddHtmlContentWithErrors()
151 $internalErrors = libxml_use_internal_errors(true);
153 $crawler = new Crawler();
154 $crawler->addHtmlContent(<<<'EOF'
160 <nav><a href="#"><a href="#"></nav>
166 $errors = libxml_get_errors();
167 $this->assertCount(1, $errors);
168 $this->assertEquals("Tag nav invalid\n", $errors[0]->message);
170 libxml_clear_errors();
171 libxml_use_internal_errors($internalErrors);
174 public function testAddXmlContent()
176 $crawler = new Crawler();
177 $crawler->addXmlContent('<html><div class="foo"></div></html>', 'UTF-8');
179 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addXmlContent() adds nodes from an XML string');
182 public function testAddXmlContentCharset()
184 $crawler = new Crawler();
185 $crawler->addXmlContent('<html><div class="foo">Tiếng Việt</div></html>', 'UTF-8');
187 $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
190 public function testAddXmlContentWithErrors()
192 $internalErrors = libxml_use_internal_errors(true);
194 $crawler = new Crawler();
195 $crawler->addXmlContent(<<<'EOF'
201 <nav><a href="#"><a href="#"></nav>
207 $this->assertGreaterThan(1, libxml_get_errors());
209 libxml_clear_errors();
210 libxml_use_internal_errors($internalErrors);
213 public function testAddContent()
215 $crawler = new Crawler();
216 $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8');
217 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string');
219 $crawler = new Crawler();
220 $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8; dir=RTL');
221 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string with extended content type');
223 $crawler = new Crawler();
224 $crawler->addContent('<html><div class="foo"></html>');
225 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() uses text/html as the default type');
227 $crawler = new Crawler();
228 $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml; charset=UTF-8');
229 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
231 $crawler = new Crawler();
232 $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml');
233 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
235 $crawler = new Crawler();
236 $crawler->addContent('foo bar', 'text/plain');
237 $this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml');
239 $crawler = new Crawler();
240 $crawler->addContent('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>');
241 $this->assertEquals('中文', $crawler->filterXPath('//span')->text(), '->addContent() guess wrong charset');
245 * @requires extension iconv
247 public function testAddContentNonUtf8()
249 $crawler = new Crawler();
250 $crawler->addContent(iconv('UTF-8', 'SJIS', '<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html>'));
251 $this->assertEquals('日本語', $crawler->filterXPath('//body')->text(), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag');
254 public function testAddDocument()
256 $crawler = new Crawler();
257 $crawler->addDocument($this->createDomDocument());
259 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument');
262 public function testAddNodeList()
264 $crawler = new Crawler();
265 $crawler->addNodeList($this->createNodeList());
267 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList');
270 public function testAddNodes()
273 foreach ($this->createNodeList() as $node) {
277 $crawler = new Crawler();
278 $crawler->addNodes($list);
280 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodes() adds nodes from an array of nodes');
283 public function testAddNode()
285 $crawler = new Crawler();
286 $crawler->addNode($this->createNodeList()->item(0));
288 $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMNode');
291 public function testClear()
293 $doc = new \DOMDocument();
294 $node = $doc->createElement('test');
296 $crawler = new Crawler($node);
298 $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
301 public function testEq()
303 $crawler = $this->createTestCrawler()->filterXPath('//li');
304 $this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler');
305 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler');
307 $this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list');
308 $this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
311 public function testEach()
313 $data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
314 return $i.'-'.$node->text();
317 $this->assertEquals(array('0-One', '1-Two', '2-Three'), $data, '->each() executes an anonymous function on each node of the list');
320 public function testIteration()
322 $crawler = $this->createTestCrawler()->filterXPath('//li');
324 $this->assertInstanceOf('Traversable', $crawler);
325 $this->assertContainsOnlyInstancesOf('DOMElement', iterator_to_array($crawler), 'Iterating a Crawler gives DOMElement instances');
328 public function testSlice()
330 $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
331 $this->assertNotSame($crawler->slice(), $crawler, '->slice() returns a new instance of a crawler');
332 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler->slice(), '->slice() returns a new instance of a crawler');
334 $this->assertCount(3, $crawler->slice(), '->slice() does not slice the nodes in the list if any param is entered');
335 $this->assertCount(1, $crawler->slice(1, 1), '->slice() slices the nodes in the list');
338 public function testReduce()
340 $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
341 $nodes = $crawler->reduce(function ($node, $i) {
344 $this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler');
345 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler');
347 $this->assertCount(2, $nodes, '->reduce() filters the nodes in the list');
350 public function testAttr()
352 $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list');
355 $this->createTestCrawler()->filterXPath('//ol')->attr('class');
356 $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty');
357 } catch (\InvalidArgumentException $e) {
358 $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
362 public function testMissingAttrValueIsNull()
364 $crawler = new Crawler();
365 $crawler->addContent('<html><div non-empty-attr="sample value" empty-attr=""></div></html>', 'text/html; charset=UTF-8');
366 $div = $crawler->filterXPath('//div');
368 $this->assertEquals('sample value', $div->attr('non-empty-attr'), '->attr() reads non-empty attributes correctly');
369 $this->assertEquals('', $div->attr('empty-attr'), '->attr() reads empty attributes correctly');
370 $this->assertNull($div->attr('missing-attr'), '->attr() reads missing attributes correctly');
373 public function testNodeName()
375 $this->assertEquals('li', $this->createTestCrawler()->filterXPath('//li')->nodeName(), '->nodeName() returns the node name of the first element of the node list');
378 $this->createTestCrawler()->filterXPath('//ol')->nodeName();
379 $this->fail('->nodeName() throws an \InvalidArgumentException if the node list is empty');
380 } catch (\InvalidArgumentException $e) {
381 $this->assertTrue(true, '->nodeName() throws an \InvalidArgumentException if the node list is empty');
385 public function testText()
387 $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');
390 $this->createTestCrawler()->filterXPath('//ol')->text();
391 $this->fail('->text() throws an \InvalidArgumentException if the node list is empty');
392 } catch (\InvalidArgumentException $e) {
393 $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
397 public function testHtml()
399 $this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
400 $this->assertEquals('<input type="text" value="TextValue" name="TextName"><input type="submit" value="FooValue" name="FooName" id="FooId"><input type="button" value="BarValue" name="BarName" id="BarId"><button value="ButtonValue" name="ButtonName" id="ButtonId"></button>', trim(preg_replace('~>\s+<~', '><', $this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html())));
403 $this->createTestCrawler()->filterXPath('//ol')->html();
404 $this->fail('->html() throws an \InvalidArgumentException if the node list is empty');
405 } catch (\InvalidArgumentException $e) {
406 $this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
410 public function testExtract()
412 $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
414 $this->assertEquals(array('One', 'Two', 'Three'), $crawler->extract('_text'), '->extract() returns an array of extracted data from the node list');
415 $this->assertEquals(array(array('One', 'first'), array('Two', ''), array('Three', '')), $crawler->extract(array('_text', 'class')), '->extract() returns an array of extracted data from the node list');
417 $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
420 public function testFilterXpathComplexQueries()
422 $crawler = $this->createTestCrawler()->filterXPath('//body');
424 $this->assertCount(0, $crawler->filterXPath('/input'));
425 $this->assertCount(0, $crawler->filterXPath('/body'));
426 $this->assertCount(1, $crawler->filterXPath('./body'));
427 $this->assertCount(1, $crawler->filterXPath('.//body'));
428 $this->assertCount(5, $crawler->filterXPath('.//input'));
429 $this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
430 $this->assertCount(1, $crawler->filterXPath('body'));
431 $this->assertCount(6, $crawler->filterXPath('//button | //input'));
432 $this->assertCount(1, $crawler->filterXPath('//body'));
433 $this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
434 $this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
435 $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child');
436 $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child');
437 $this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
438 $this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
439 $this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
440 $this->assertCount(1, $crawler->filterXPath("//a[./@href][((./@id = 'Klausi|Claudiu' or normalize-space(string(.)) = 'Klausi|Claudiu' or ./@title = 'Klausi|Claudiu' or ./@rel = 'Klausi|Claudiu') or .//img[./@alt = 'Klausi|Claudiu'])]"));
443 public function testFilterXPath()
445 $crawler = $this->createTestCrawler();
446 $this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler');
447 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
449 $crawler = $this->createTestCrawler()->filterXPath('//ul');
450 $this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');
452 $crawler = $this->createTestCrawler();
453 $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
456 public function testFilterRemovesDuplicates()
458 $crawler = $this->createTestCrawler()->filter('html, body')->filter('li');
459 $this->assertCount(6, $crawler, 'The crawler removes duplicates when filtering.');
462 public function testFilterXPathWithDefaultNamespace()
464 $crawler = $this->createTestXmlCrawler()->filterXPath('//default:entry/default:id');
465 $this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace');
466 $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
469 public function testFilterXPathWithCustomDefaultNamespace()
471 $crawler = $this->createTestXmlCrawler();
472 $crawler->setDefaultNamespacePrefix('x');
473 $crawler = $crawler->filterXPath('//x:entry/x:id');
475 $this->assertCount(1, $crawler, '->filterXPath() lets to override the default namespace prefix');
476 $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
479 public function testFilterXPathWithNamespace()
481 $crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');
482 $this->assertCount(2, $crawler, '->filterXPath() automatically registers a namespace');
485 public function testFilterXPathWithMultipleNamespaces()
487 $crawler = $this->createTestXmlCrawler()->filterXPath('//media:group/yt:aspectRatio');
488 $this->assertCount(1, $crawler, '->filterXPath() automatically registers multiple namespaces');
489 $this->assertSame('widescreen', $crawler->text());
492 public function testFilterXPathWithManuallyRegisteredNamespace()
494 $crawler = $this->createTestXmlCrawler();
495 $crawler->registerNamespace('m', 'http://search.yahoo.com/mrss/');
497 $crawler = $crawler->filterXPath('//m:group/yt:aspectRatio');
498 $this->assertCount(1, $crawler, '->filterXPath() uses manually registered namespace');
499 $this->assertSame('widescreen', $crawler->text());
502 public function testFilterXPathWithAnUrl()
504 $crawler = $this->createTestXmlCrawler();
506 $crawler = $crawler->filterXPath('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"]');
507 $this->assertCount(1, $crawler);
508 $this->assertSame('Music', $crawler->text());
511 public function testFilterXPathWithFakeRoot()
513 $crawler = $this->createTestCrawler();
514 $this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node');
515 $this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node');
516 $this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
519 public function testFilterXPathWithAncestorAxis()
521 $crawler = $this->createTestCrawler()->filterXPath('//form');
523 $this->assertCount(0, $crawler->filterXPath('ancestor::*'), 'The fake root node has no ancestor nodes');
526 public function testFilterXPathWithAncestorOrSelfAxis()
528 $crawler = $this->createTestCrawler()->filterXPath('//form');
530 $this->assertCount(0, $crawler->filterXPath('ancestor-or-self::*'), 'The fake root node has no ancestor nodes');
533 public function testFilterXPathWithAttributeAxis()
535 $crawler = $this->createTestCrawler()->filterXPath('//form');
537 $this->assertCount(0, $crawler->filterXPath('attribute::*'), 'The fake root node has no attribute nodes');
540 public function testFilterXPathWithAttributeAxisAfterElementAxis()
542 $this->assertCount(3, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis');
545 public function testFilterXPathWithChildAxis()
547 $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]');
549 $this->assertCount(1, $crawler->filterXPath('child::div'), 'A child selection finds only the current div');
552 public function testFilterXPathWithFollowingAxis()
554 $crawler = $this->createTestCrawler()->filterXPath('//a');
556 $this->assertCount(0, $crawler->filterXPath('following::div'), 'The fake root node has no following nodes');
559 public function testFilterXPathWithFollowingSiblingAxis()
561 $crawler = $this->createTestCrawler()->filterXPath('//a');
563 $this->assertCount(0, $crawler->filterXPath('following-sibling::div'), 'The fake root node has no following nodes');
566 public function testFilterXPathWithNamespaceAxis()
568 $crawler = $this->createTestCrawler()->filterXPath('//button');
570 $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'The fake root node has no namespace nodes');
573 public function testFilterXPathWithNamespaceAxisAfterElementAxis()
575 $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]/namespace::*');
577 $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'Namespace axes cannot be requested');
580 public function testFilterXPathWithParentAxis()
582 $crawler = $this->createTestCrawler()->filterXPath('//button');
584 $this->assertCount(0, $crawler->filterXPath('parent::*'), 'The fake root node has no parent nodes');
587 public function testFilterXPathWithPrecedingAxis()
589 $crawler = $this->createTestCrawler()->filterXPath('//form');
591 $this->assertCount(0, $crawler->filterXPath('preceding::*'), 'The fake root node has no preceding nodes');
594 public function testFilterXPathWithPrecedingSiblingAxis()
596 $crawler = $this->createTestCrawler()->filterXPath('//form');
598 $this->assertCount(0, $crawler->filterXPath('preceding-sibling::*'), 'The fake root node has no preceding nodes');
601 public function testFilterXPathWithSelfAxes()
603 $crawler = $this->createTestCrawler()->filterXPath('//a');
605 $this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name');
606 $this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name');
607 $this->assertCount(10, $crawler->filterXPath('self::*/a'));
610 public function testFilter()
612 $crawler = $this->createTestCrawler();
613 $this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler');
614 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler');
616 $crawler = $this->createTestCrawler()->filter('ul');
618 $this->assertCount(6, $crawler->filter('li'), '->filter() filters the node list with the CSS selector');
621 public function testFilterWithDefaultNamespace()
623 $crawler = $this->createTestXmlCrawler()->filter('default|entry default|id');
624 $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
625 $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
628 public function testFilterWithNamespace()
630 $crawler = $this->createTestXmlCrawler()->filter('yt|accessControl');
631 $this->assertCount(2, $crawler, '->filter() automatically registers namespaces');
634 public function testFilterWithMultipleNamespaces()
636 $crawler = $this->createTestXmlCrawler()->filter('media|group yt|aspectRatio');
637 $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
638 $this->assertSame('widescreen', $crawler->text());
641 public function testFilterWithDefaultNamespaceOnly()
643 $crawler = new Crawler('<?xml version="1.0" encoding="UTF-8"?>
644 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
646 <loc>http://localhost/foo</loc>
647 <changefreq>weekly</changefreq>
648 <priority>0.5</priority>
649 <lastmod>2012-11-16</lastmod>
652 <loc>http://localhost/bar</loc>
653 <changefreq>weekly</changefreq>
654 <priority>0.5</priority>
655 <lastmod>2012-11-16</lastmod>
660 $this->assertEquals(2, $crawler->filter('url')->count());
663 public function testSelectLink()
665 $crawler = $this->createTestCrawler();
666 $this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler');
667 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler');
669 $this->assertCount(1, $crawler->selectLink('Fabien\'s Foo'), '->selectLink() selects links by the node values');
670 $this->assertCount(1, $crawler->selectLink('Fabien\'s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
672 $this->assertCount(2, $crawler->selectLink('Fabien"s Foo'), '->selectLink() selects links by the node values');
673 $this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
675 $this->assertCount(1, $crawler->selectLink('\' Fabien"s Foo'), '->selectLink() selects links by the node values');
676 $this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
678 $this->assertCount(4, $crawler->selectLink('Foo'), '->selectLink() selects links by the node values');
679 $this->assertCount(4, $crawler->selectLink('Bar'), '->selectLink() selects links by the node values');
682 public function testSelectImage()
684 $crawler = $this->createTestCrawler();
685 $this->assertNotSame($crawler, $crawler->selectImage('Bar'), '->selectImage() returns a new instance of a crawler');
686 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectImage() returns a new instance of a crawler');
688 $this->assertCount(1, $crawler->selectImage('Fabien\'s Bar'), '->selectImage() selects images by alt attribute');
689 $this->assertCount(2, $crawler->selectImage('Fabien"s Bar'), '->selectImage() selects images by alt attribute');
690 $this->assertCount(1, $crawler->selectImage('\' Fabien"s Bar'), '->selectImage() selects images by alt attribute');
693 public function testSelectButton()
695 $crawler = $this->createTestCrawler();
696 $this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
697 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler');
699 $this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
700 $this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
701 $this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
703 $this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
704 $this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
705 $this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
707 $this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
708 $this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
711 public function testSelectButtonWithSingleQuotesInNameAttribute()
718 <a href="/index.php?r=site/login">Login</a>
720 <form id="login-form" action="/index.php?r=site/login" method="post">
721 <button type="submit" name="Click 'Here'">Submit</button>
727 $crawler = new Crawler($html);
729 $this->assertCount(1, $crawler->selectButton('Click \'Here\''));
732 public function testSelectButtonWithDoubleQuotesInNameAttribute()
739 <a href="/index.php?r=site/login">Login</a>
741 <form id="login-form" action="/index.php?r=site/login" method="post">
742 <button type="submit" name='Click "Here"'>Submit</button>
748 $crawler = new Crawler($html);
750 $this->assertCount(1, $crawler->selectButton('Click "Here"'));
753 public function testLink()
755 $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
756 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
758 $this->assertEquals('POST', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
760 $crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink');
761 $this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance');
764 $this->createTestCrawler()->filterXPath('//ol')->link();
765 $this->fail('->link() throws an \InvalidArgumentException if the node list is empty');
766 } catch (\InvalidArgumentException $e) {
767 $this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty');
772 * @expectedException \InvalidArgumentException
773 * @expectedExceptionMessage The selected node should be instance of DOMElement
775 public function testInvalidLink()
777 $crawler = $this->createTestCrawler('http://example.com/bar/');
778 $crawler->filterXPath('//li/text()')->link();
782 * @expectedException \InvalidArgumentException
783 * @expectedExceptionMessage The selected node should be instance of DOMElement
785 public function testInvalidLinks()
787 $crawler = $this->createTestCrawler('http://example.com/bar/');
788 $crawler->filterXPath('//li/text()')->link();
791 public function testImage()
793 $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
794 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $crawler->image(), '->image() returns an Image instance');
797 $this->createTestCrawler()->filterXPath('//ol')->image();
798 $this->fail('->image() throws an \InvalidArgumentException if the node list is empty');
799 } catch (\InvalidArgumentException $e) {
800 $this->assertTrue(true, '->image() throws an \InvalidArgumentException if the node list is empty');
804 public function testSelectLinkAndLinkFiltered()
811 <a href="/index.php?r=site/login">Login</a>
813 <form id="login-form" action="/index.php?r=site/login" method="post">
814 <button type="submit">Submit</button>
820 $crawler = new Crawler($html);
821 $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']");
823 $this->assertCount(0, $filtered->selectLink('Login'));
824 $this->assertCount(1, $filtered->selectButton('Submit'));
826 $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']");
828 $this->assertCount(1, $filtered->selectLink('Login'));
829 $this->assertCount(0, $filtered->selectButton('Submit'));
831 $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login'));
832 $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit'));
835 public function testChaining()
837 $crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>');
839 $this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name'));
842 public function testLinks()
844 $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
845 $this->assertInternalType('array', $crawler->links(), '->links() returns an array');
847 $this->assertCount(4, $crawler->links(), '->links() returns an array');
848 $links = $crawler->links();
849 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances');
851 $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
854 public function testImages()
856 $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
857 $this->assertInternalType('array', $crawler->images(), '->images() returns an array');
859 $this->assertCount(4, $crawler->images(), '->images() returns an array');
860 $images = $crawler->images();
861 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $images[0], '->images() returns an array of Image instances');
863 $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
866 public function testForm()
868 $testCrawler = $this->createTestCrawler('http://example.com/bar/');
869 $crawler = $testCrawler->selectButton('FooValue');
870 $crawler2 = $testCrawler->selectButton('FooBarValue');
871 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
872 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance');
874 $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute');
876 $this->assertEquals(array('FooName' => 'FooBar', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument');
877 $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->getValues() returns correct form values');
878 $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->getValues() returns correct form values');
881 $this->createTestCrawler()->filterXPath('//ol')->form();
882 $this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
883 } catch (\InvalidArgumentException $e) {
884 $this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty');
889 * @expectedException \InvalidArgumentException
890 * @expectedExceptionMessage The selected node should be instance of DOMElement
892 public function testInvalidForm()
894 $crawler = $this->createTestCrawler('http://example.com/bar/');
895 $crawler->filterXPath('//li/text()')->form();
898 public function testLast()
900 $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
901 $this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler');
902 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler');
904 $this->assertEquals('Three', $crawler->last()->text());
907 public function testFirst()
909 $crawler = $this->createTestCrawler()->filterXPath('//li');
910 $this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler');
911 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler');
913 $this->assertEquals('One', $crawler->first()->text());
916 public function testSiblings()
918 $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
919 $this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler');
920 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler');
922 $nodes = $crawler->siblings();
923 $this->assertEquals(2, $nodes->count());
924 $this->assertEquals('One', $nodes->eq(0)->text());
925 $this->assertEquals('Three', $nodes->eq(1)->text());
927 $nodes = $this->createTestCrawler()->filterXPath('//li')->eq(0)->siblings();
928 $this->assertEquals(2, $nodes->count());
929 $this->assertEquals('Two', $nodes->eq(0)->text());
930 $this->assertEquals('Three', $nodes->eq(1)->text());
933 $this->createTestCrawler()->filterXPath('//ol')->siblings();
934 $this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty');
935 } catch (\InvalidArgumentException $e) {
936 $this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty');
940 public function testNextAll()
942 $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
943 $this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler');
944 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler');
946 $nodes = $crawler->nextAll();
947 $this->assertEquals(1, $nodes->count());
948 $this->assertEquals('Three', $nodes->eq(0)->text());
951 $this->createTestCrawler()->filterXPath('//ol')->nextAll();
952 $this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty');
953 } catch (\InvalidArgumentException $e) {
954 $this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty');
958 public function testPreviousAll()
960 $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(2);
961 $this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler');
962 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler');
964 $nodes = $crawler->previousAll();
965 $this->assertEquals(2, $nodes->count());
966 $this->assertEquals('Two', $nodes->eq(0)->text());
969 $this->createTestCrawler()->filterXPath('//ol')->previousAll();
970 $this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty');
971 } catch (\InvalidArgumentException $e) {
972 $this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty');
976 public function testChildren()
978 $crawler = $this->createTestCrawler()->filterXPath('//ul');
979 $this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler');
980 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler');
982 $nodes = $crawler->children();
983 $this->assertEquals(3, $nodes->count());
984 $this->assertEquals('One', $nodes->eq(0)->text());
985 $this->assertEquals('Two', $nodes->eq(1)->text());
986 $this->assertEquals('Three', $nodes->eq(2)->text());
989 $this->createTestCrawler()->filterXPath('//ol')->children();
990 $this->fail('->children() throws an \InvalidArgumentException if the node list is empty');
991 } catch (\InvalidArgumentException $e) {
992 $this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
996 $crawler = new Crawler('<p></p>');
997 $crawler->filter('p')->children();
998 $this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
999 } catch (\PHPUnit\Framework\Error\Notice $e) {
1000 $this->fail('->children() does not trigger a notice if the node has no children');
1001 } catch (\PHPUnit_Framework_Error_Notice $e) {
1002 $this->fail('->children() does not trigger a notice if the node has no children');
1006 public function testParents()
1008 $crawler = $this->createTestCrawler()->filterXPath('//li[1]');
1009 $this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
1010 $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
1012 $nodes = $crawler->parents();
1013 $this->assertEquals(3, $nodes->count());
1015 $nodes = $this->createTestCrawler()->filterXPath('//html')->parents();
1016 $this->assertEquals(0, $nodes->count());
1019 $this->createTestCrawler()->filterXPath('//ol')->parents();
1020 $this->fail('->parents() throws an \InvalidArgumentException if the node list is empty');
1021 } catch (\InvalidArgumentException $e) {
1022 $this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty');
1027 * @dataProvider getBaseTagData
1029 public function testBaseTag($baseValue, $linkValue, $expectedUri, $currentUri = null, $description = null)
1031 $crawler = new Crawler('<html><base href="'.$baseValue.'"><a href="'.$linkValue.'"></a></html>', $currentUri);
1032 $this->assertEquals($expectedUri, $crawler->filterXPath('//a')->link()->getUri(), $description);
1035 public function getBaseTagData()
1038 array('http://base.com', 'link', 'http://base.com/link'),
1039 array('//base.com', 'link', 'https://base.com/link', 'https://domain.com', '<base> tag can use a schema-less URL'),
1040 array('path/', 'link', 'https://domain.com/path/link', 'https://domain.com', '<base> tag can set a path'),
1041 array('http://base.com', '#', 'http://base.com#', 'http://domain.com/path/link', '<base> tag does work with links to an anchor'),
1042 array('http://base.com', '', 'http://base.com', 'http://domain.com/path/link', '<base> tag does work with empty links'),
1047 * @dataProvider getBaseTagWithFormData
1049 public function testBaseTagWithForm($baseValue, $actionValue, $expectedUri, $currentUri = null, $description = null)
1051 $crawler = new Crawler('<html><base href="'.$baseValue.'"><form method="post" action="'.$actionValue.'"><button type="submit" name="submit"/></form></html>', $currentUri);
1052 $this->assertEquals($expectedUri, $crawler->filterXPath('//button')->form()->getUri(), $description);
1055 public function getBaseTagWithFormData()
1058 array('https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'),
1059 array('/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'),
1060 array('/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'),
1061 array('http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'),
1062 array('http://base.com', '', 'http://domain.com/path/form', 'http://domain.com/path/form', '<base> tag does work with a URL and an empty form action'),
1063 array('http://base.com/path', '/registration', 'http://base.com/registration', 'http://domain.com/path/form', '<base> tag does work with a URL and form action'),
1067 public function testCountOfNestedElements()
1069 $crawler = new Crawler('<html><body><ul><li>List item 1<ul><li>Sublist item 1</li><li>Sublist item 2</ul></li></ul></body></html>');
1071 $this->assertCount(1, $crawler->filter('li:contains("List item 1")'));
1074 public function testEvaluateReturnsTypedResultOfXPathExpressionOnADocumentSubset()
1076 $crawler = $this->createTestCrawler();
1078 $result = $crawler->filterXPath('//form/input')->evaluate('substring-before(@name, "Name")');
1080 $this->assertSame(array('Text', 'Foo', 'Bar'), $result);
1083 public function testEvaluateReturnsTypedResultOfNamespacedXPathExpressionOnADocumentSubset()
1085 $crawler = $this->createTestXmlCrawler();
1087 $result = $crawler->filterXPath('//yt:accessControl/@action')->evaluate('string(.)');
1089 $this->assertSame(array('comment', 'videoRespond'), $result);
1092 public function testEvaluateReturnsTypedResultOfNamespacedXPathExpression()
1094 $crawler = $this->createTestXmlCrawler();
1095 $crawler->registerNamespace('youtube', 'http://gdata.youtube.com/schemas/2007');
1097 $result = $crawler->evaluate('string(//youtube:accessControl/@action)');
1099 $this->assertSame(array('comment'), $result);
1102 public function testEvaluateReturnsACrawlerIfXPathExpressionEvaluatesToANode()
1104 $crawler = $this->createTestCrawler()->evaluate('//form/input[1]');
1106 $this->assertInstanceOf(Crawler::class, $crawler);
1107 $this->assertCount(1, $crawler);
1108 $this->assertSame('input', $crawler->first()->nodeName());
1112 * @expectedException \LogicException
1114 public function testEvaluateThrowsAnExceptionIfDocumentIsEmpty()
1116 (new Crawler())->evaluate('//form/input[1]');
1119 public function createTestCrawler($uri = null)
1121 $dom = new \DOMDocument();
1125 <a href="foo">Foo</a>
1126 <a href="/foo"> Fabien\'s Foo </a>
1127 <a href="/foo">Fabien"s Foo</a>
1128 <a href="/foo">\' Fabien"s Foo</a>
1130 <a href="/bar"><img alt="Bar"/></a>
1131 <a href="/bar"><img alt=" Fabien\'s Bar "/></a>
1132 <a href="/bar"><img alt="Fabien"s Bar"/></a>
1133 <a href="/bar"><img alt="\' Fabien"s Bar"/></a>
1135 <a href="?get=param">GetLink</a>
1137 <a href="/example">Klausi|Claudiu</a>
1139 <form action="foo" id="FooFormId">
1140 <input type="text" value="TextValue" name="TextName" />
1141 <input type="submit" value="FooValue" name="FooName" id="FooId" />
1142 <input type="button" value="BarValue" name="BarName" id="BarId" />
1143 <button value="ButtonValue" name="ButtonName" id="ButtonId" />
1146 <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
1147 <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
1150 <li class="first">One</li>
1160 <div id="child"></div>
1161 <div id="child2" xmlns:foo="http://example.com"></div>
1163 <div id="sibling"><img /></div>
1168 return new Crawler($dom, $uri);
1171 protected function createTestXmlCrawler($uri = null)
1173 $xml = '<?xml version="1.0" encoding="UTF-8"?>
1174 <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
1175 <id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
1176 <yt:accessControl action="comment" permission="allowed"/>
1177 <yt:accessControl action="videoRespond" permission="moderated"/>
1179 <media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
1180 <yt:aspectRatio>widescreen</yt:aspectRatio>
1182 <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
1185 return new Crawler($xml, $uri);
1188 protected function createDomDocument()
1190 $dom = new \DOMDocument();
1191 $dom->loadXML('<html><div class="foo"></div></html>');
1196 protected function createNodeList()
1198 $dom = new \DOMDocument();
1199 $dom->loadXML('<html><div class="foo"></div></html>');
1200 $domxpath = new \DOMXPath($dom);
1202 return $domxpath->query('//div');