CrawlerTest.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DomCrawler\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DomCrawler\Crawler;
  13. class CrawlerTest extends TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $crawler = new Crawler();
  18. $this->assertCount(0, $crawler, '__construct() returns an empty crawler');
  19. $doc = new \DOMDocument();
  20. $node = $doc->createElement('test');
  21. $crawler = new Crawler($node);
  22. $this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
  23. }
  24. public function testGetUri()
  25. {
  26. $uri = 'http://symfony.com';
  27. $crawler = new Crawler(null, $uri);
  28. $this->assertEquals($uri, $crawler->getUri());
  29. }
  30. public function testGetBaseHref()
  31. {
  32. $baseHref = 'http://symfony.com';
  33. $crawler = new Crawler(null, null, $baseHref);
  34. $this->assertEquals($baseHref, $crawler->getBaseHref());
  35. }
  36. public function testAdd()
  37. {
  38. $crawler = new Crawler();
  39. $crawler->add($this->createDomDocument());
  40. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMDocument');
  41. $crawler = new Crawler();
  42. $crawler->add($this->createNodeList());
  43. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList');
  44. $list = [];
  45. foreach ($this->createNodeList() as $node) {
  46. $list[] = $node;
  47. }
  48. $crawler = new Crawler();
  49. $crawler->add($list);
  50. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an array of nodes');
  51. $crawler = new Crawler();
  52. $crawler->add($this->createNodeList()->item(0));
  53. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNode');
  54. $crawler = new Crawler();
  55. $crawler->add('<html><body>Foo</body></html>');
  56. $this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string');
  57. }
  58. /**
  59. * @expectedException \InvalidArgumentException
  60. */
  61. public function testAddInvalidType()
  62. {
  63. $crawler = new Crawler();
  64. $crawler->add(1);
  65. }
  66. /**
  67. * @expectedException \InvalidArgumentException
  68. * @expectedExceptionMessage Attaching DOM nodes from multiple documents in the same crawler is forbidden.
  69. */
  70. public function testAddMultipleDocumentNode()
  71. {
  72. $crawler = $this->createTestCrawler();
  73. $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
  74. }
  75. public function testAddHtmlContent()
  76. {
  77. $crawler = new Crawler();
  78. $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
  79. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
  80. }
  81. public function testAddHtmlContentWithBaseTag()
  82. {
  83. $crawler = new Crawler();
  84. $crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
  85. $this->assertEquals('http://symfony.com', $crawler->filterXPath('//base')->attr('href'), '->addHtmlContent() adds nodes from an HTML string');
  86. $this->assertEquals('http://symfony.com/contact', $crawler->filterXPath('//a')->link()->getUri(), '->addHtmlContent() adds nodes from an HTML string');
  87. }
  88. /**
  89. * @requires extension mbstring
  90. */
  91. public function testAddHtmlContentCharset()
  92. {
  93. $crawler = new Crawler();
  94. $crawler->addHtmlContent('<html><div class="foo">Tiếng Việt</html>', 'UTF-8');
  95. $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
  96. }
  97. public function testAddHtmlContentInvalidBaseTag()
  98. {
  99. $crawler = new Crawler(null, 'http://symfony.com');
  100. $crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8');
  101. $this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute');
  102. }
  103. public function testAddHtmlContentUnsupportedCharset()
  104. {
  105. $crawler = new Crawler();
  106. $crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/windows-1250.html'), 'Windows-1250');
  107. $this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
  108. }
  109. /**
  110. * @requires extension mbstring
  111. */
  112. public function testAddHtmlContentCharsetGbk()
  113. {
  114. $crawler = new Crawler();
  115. //gbk encode of <html><p>中文</p></html>
  116. $crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
  117. $this->assertEquals('中文', $crawler->filterXPath('//p')->text());
  118. }
  119. public function testAddHtmlContentWithErrors()
  120. {
  121. $internalErrors = libxml_use_internal_errors(true);
  122. $crawler = new Crawler();
  123. $crawler->addHtmlContent(<<<'EOF'
  124. <!DOCTYPE html>
  125. <html>
  126. <head>
  127. </head>
  128. <body>
  129. <nav><a href="#"><a href="#"></nav>
  130. </body>
  131. </html>
  132. EOF
  133. , 'UTF-8');
  134. $errors = libxml_get_errors();
  135. $this->assertCount(1, $errors);
  136. $this->assertEquals("Tag nav invalid\n", $errors[0]->message);
  137. libxml_clear_errors();
  138. libxml_use_internal_errors($internalErrors);
  139. }
  140. public function testAddXmlContent()
  141. {
  142. $crawler = new Crawler();
  143. $crawler->addXmlContent('<html><div class="foo"></div></html>', 'UTF-8');
  144. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addXmlContent() adds nodes from an XML string');
  145. }
  146. public function testAddXmlContentCharset()
  147. {
  148. $crawler = new Crawler();
  149. $crawler->addXmlContent('<html><div class="foo">Tiếng Việt</div></html>', 'UTF-8');
  150. $this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
  151. }
  152. public function testAddXmlContentWithErrors()
  153. {
  154. $internalErrors = libxml_use_internal_errors(true);
  155. $crawler = new Crawler();
  156. $crawler->addXmlContent(<<<'EOF'
  157. <!DOCTYPE html>
  158. <html>
  159. <head>
  160. </head>
  161. <body>
  162. <nav><a href="#"><a href="#"></nav>
  163. </body>
  164. </html>
  165. EOF
  166. , 'UTF-8');
  167. $this->assertGreaterThan(1, libxml_get_errors());
  168. libxml_clear_errors();
  169. libxml_use_internal_errors($internalErrors);
  170. }
  171. public function testAddContent()
  172. {
  173. $crawler = new Crawler();
  174. $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8');
  175. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string');
  176. $crawler = new Crawler();
  177. $crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8; dir=RTL');
  178. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string with extended content type');
  179. $crawler = new Crawler();
  180. $crawler->addContent('<html><div class="foo"></html>');
  181. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() uses text/html as the default type');
  182. $crawler = new Crawler();
  183. $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml; charset=UTF-8');
  184. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
  185. $crawler = new Crawler();
  186. $crawler->addContent('<html><div class="foo"></div></html>', 'text/xml');
  187. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
  188. $crawler = new Crawler();
  189. $crawler->addContent('foo bar', 'text/plain');
  190. $this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml');
  191. $crawler = new Crawler();
  192. $crawler->addContent('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>');
  193. $this->assertEquals('中文', $crawler->filterXPath('//span')->text(), '->addContent() guess wrong charset');
  194. }
  195. /**
  196. * @requires extension iconv
  197. */
  198. public function testAddContentNonUtf8()
  199. {
  200. $crawler = new Crawler();
  201. $crawler->addContent(iconv('UTF-8', 'SJIS', '<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html>'));
  202. $this->assertEquals('日本語', $crawler->filterXPath('//body')->text(), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag');
  203. }
  204. public function testAddDocument()
  205. {
  206. $crawler = new Crawler();
  207. $crawler->addDocument($this->createDomDocument());
  208. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument');
  209. }
  210. public function testAddNodeList()
  211. {
  212. $crawler = new Crawler();
  213. $crawler->addNodeList($this->createNodeList());
  214. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList');
  215. }
  216. public function testAddNodes()
  217. {
  218. $list = [];
  219. foreach ($this->createNodeList() as $node) {
  220. $list[] = $node;
  221. }
  222. $crawler = new Crawler();
  223. $crawler->addNodes($list);
  224. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodes() adds nodes from an array of nodes');
  225. }
  226. public function testAddNode()
  227. {
  228. $crawler = new Crawler();
  229. $crawler->addNode($this->createNodeList()->item(0));
  230. $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMNode');
  231. }
  232. public function testClear()
  233. {
  234. $doc = new \DOMDocument();
  235. $node = $doc->createElement('test');
  236. $crawler = new Crawler($node);
  237. $crawler->clear();
  238. $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
  239. }
  240. public function testEq()
  241. {
  242. $crawler = $this->createTestCrawler()->filterXPath('//li');
  243. $this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler');
  244. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler');
  245. $this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list');
  246. $this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
  247. }
  248. public function testEach()
  249. {
  250. $data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
  251. return $i.'-'.$node->text();
  252. });
  253. $this->assertEquals(['0-One', '1-Two', '2-Three'], $data, '->each() executes an anonymous function on each node of the list');
  254. }
  255. public function testIteration()
  256. {
  257. $crawler = $this->createTestCrawler()->filterXPath('//li');
  258. $this->assertInstanceOf('Traversable', $crawler);
  259. $this->assertContainsOnlyInstancesOf('DOMElement', iterator_to_array($crawler), 'Iterating a Crawler gives DOMElement instances');
  260. }
  261. public function testSlice()
  262. {
  263. $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
  264. $this->assertNotSame($crawler->slice(), $crawler, '->slice() returns a new instance of a crawler');
  265. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler->slice(), '->slice() returns a new instance of a crawler');
  266. $this->assertCount(3, $crawler->slice(), '->slice() does not slice the nodes in the list if any param is entered');
  267. $this->assertCount(1, $crawler->slice(1, 1), '->slice() slices the nodes in the list');
  268. }
  269. public function testReduce()
  270. {
  271. $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
  272. $nodes = $crawler->reduce(function ($node, $i) {
  273. return 1 !== $i;
  274. });
  275. $this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler');
  276. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler');
  277. $this->assertCount(2, $nodes, '->reduce() filters the nodes in the list');
  278. }
  279. public function testAttr()
  280. {
  281. $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list');
  282. try {
  283. $this->createTestCrawler()->filterXPath('//ol')->attr('class');
  284. $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty');
  285. } catch (\InvalidArgumentException $e) {
  286. $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
  287. }
  288. }
  289. public function testMissingAttrValueIsNull()
  290. {
  291. $crawler = new Crawler();
  292. $crawler->addContent('<html><div non-empty-attr="sample value" empty-attr=""></div></html>', 'text/html; charset=UTF-8');
  293. $div = $crawler->filterXPath('//div');
  294. $this->assertEquals('sample value', $div->attr('non-empty-attr'), '->attr() reads non-empty attributes correctly');
  295. $this->assertEquals('', $div->attr('empty-attr'), '->attr() reads empty attributes correctly');
  296. $this->assertNull($div->attr('missing-attr'), '->attr() reads missing attributes correctly');
  297. }
  298. public function testNodeName()
  299. {
  300. $this->assertEquals('li', $this->createTestCrawler()->filterXPath('//li')->nodeName(), '->nodeName() returns the node name of the first element of the node list');
  301. try {
  302. $this->createTestCrawler()->filterXPath('//ol')->nodeName();
  303. $this->fail('->nodeName() throws an \InvalidArgumentException if the node list is empty');
  304. } catch (\InvalidArgumentException $e) {
  305. $this->assertTrue(true, '->nodeName() throws an \InvalidArgumentException if the node list is empty');
  306. }
  307. }
  308. public function testText()
  309. {
  310. $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');
  311. try {
  312. $this->createTestCrawler()->filterXPath('//ol')->text();
  313. $this->fail('->text() throws an \InvalidArgumentException if the node list is empty');
  314. } catch (\InvalidArgumentException $e) {
  315. $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
  316. }
  317. $this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value'));
  318. }
  319. public function testHtml()
  320. {
  321. $this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
  322. $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())));
  323. try {
  324. $this->createTestCrawler()->filterXPath('//ol')->html();
  325. $this->fail('->html() throws an \InvalidArgumentException if the node list is empty');
  326. } catch (\InvalidArgumentException $e) {
  327. $this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
  328. }
  329. $this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->html('my value'));
  330. }
  331. public function testExtract()
  332. {
  333. $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
  334. $this->assertEquals(['One', 'Two', 'Three'], $crawler->extract('_text'), '->extract() returns an array of extracted data from the node list');
  335. $this->assertEquals([['One', 'first'], ['Two', ''], ['Three', '']], $crawler->extract(['_text', 'class']), '->extract() returns an array of extracted data from the node list');
  336. $this->assertEquals([[], [], []], $crawler->extract([]), '->extract() returns empty arrays if the attribute list is empty');
  337. $this->assertEquals([], $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
  338. $this->assertEquals([['One', 'li'], ['Two', 'li'], ['Three', 'li']], $crawler->extract(['_text', '_name']), '->extract() returns an array of extracted data from the node list');
  339. }
  340. public function testFilterXpathComplexQueries()
  341. {
  342. $crawler = $this->createTestCrawler()->filterXPath('//body');
  343. $this->assertCount(0, $crawler->filterXPath('/input'));
  344. $this->assertCount(0, $crawler->filterXPath('/body'));
  345. $this->assertCount(1, $crawler->filterXPath('./body'));
  346. $this->assertCount(1, $crawler->filterXPath('.//body'));
  347. $this->assertCount(5, $crawler->filterXPath('.//input'));
  348. $this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
  349. $this->assertCount(1, $crawler->filterXPath('body'));
  350. $this->assertCount(6, $crawler->filterXPath('//button | //input'));
  351. $this->assertCount(1, $crawler->filterXPath('//body'));
  352. $this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
  353. $this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
  354. $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child');
  355. $this->assertCount(3, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child');
  356. $this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
  357. $this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
  358. $this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
  359. $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'])]"));
  360. }
  361. public function testFilterXPath()
  362. {
  363. $crawler = $this->createTestCrawler();
  364. $this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler');
  365. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
  366. $crawler = $this->createTestCrawler()->filterXPath('//ul');
  367. $this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');
  368. $crawler = $this->createTestCrawler();
  369. $this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
  370. }
  371. public function testFilterRemovesDuplicates()
  372. {
  373. $crawler = $this->createTestCrawler()->filter('html, body')->filter('li');
  374. $this->assertCount(6, $crawler, 'The crawler removes duplicates when filtering.');
  375. }
  376. public function testFilterXPathWithDefaultNamespace()
  377. {
  378. $crawler = $this->createTestXmlCrawler()->filterXPath('//default:entry/default:id');
  379. $this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace');
  380. $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
  381. }
  382. public function testFilterXPathWithCustomDefaultNamespace()
  383. {
  384. $crawler = $this->createTestXmlCrawler();
  385. $crawler->setDefaultNamespacePrefix('x');
  386. $crawler = $crawler->filterXPath('//x:entry/x:id');
  387. $this->assertCount(1, $crawler, '->filterXPath() lets to override the default namespace prefix');
  388. $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
  389. }
  390. public function testFilterXPathWithNamespace()
  391. {
  392. $crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');
  393. $this->assertCount(2, $crawler, '->filterXPath() automatically registers a namespace');
  394. }
  395. public function testFilterXPathWithMultipleNamespaces()
  396. {
  397. $crawler = $this->createTestXmlCrawler()->filterXPath('//media:group/yt:aspectRatio');
  398. $this->assertCount(1, $crawler, '->filterXPath() automatically registers multiple namespaces');
  399. $this->assertSame('widescreen', $crawler->text());
  400. }
  401. public function testFilterXPathWithManuallyRegisteredNamespace()
  402. {
  403. $crawler = $this->createTestXmlCrawler();
  404. $crawler->registerNamespace('m', 'http://search.yahoo.com/mrss/');
  405. $crawler = $crawler->filterXPath('//m:group/yt:aspectRatio');
  406. $this->assertCount(1, $crawler, '->filterXPath() uses manually registered namespace');
  407. $this->assertSame('widescreen', $crawler->text());
  408. }
  409. public function testFilterXPathWithAnUrl()
  410. {
  411. $crawler = $this->createTestXmlCrawler();
  412. $crawler = $crawler->filterXPath('//media:category[@scheme="http://gdata.youtube.com/schemas/2007/categories.cat"]');
  413. $this->assertCount(1, $crawler);
  414. $this->assertSame('Music', $crawler->text());
  415. }
  416. public function testFilterXPathWithFakeRoot()
  417. {
  418. $crawler = $this->createTestCrawler();
  419. $this->assertCount(0, $crawler->filterXPath('.'), '->filterXPath() returns an empty result if the XPath references the fake root node');
  420. $this->assertCount(0, $crawler->filterXPath('self::*'), '->filterXPath() returns an empty result if the XPath references the fake root node');
  421. $this->assertCount(0, $crawler->filterXPath('self::_root'), '->filterXPath() returns an empty result if the XPath references the fake root node');
  422. }
  423. public function testFilterXPathWithAncestorAxis()
  424. {
  425. $crawler = $this->createTestCrawler()->filterXPath('//form');
  426. $this->assertCount(0, $crawler->filterXPath('ancestor::*'), 'The fake root node has no ancestor nodes');
  427. }
  428. public function testFilterXPathWithAncestorOrSelfAxis()
  429. {
  430. $crawler = $this->createTestCrawler()->filterXPath('//form');
  431. $this->assertCount(0, $crawler->filterXPath('ancestor-or-self::*'), 'The fake root node has no ancestor nodes');
  432. }
  433. public function testFilterXPathWithAttributeAxis()
  434. {
  435. $crawler = $this->createTestCrawler()->filterXPath('//form');
  436. $this->assertCount(0, $crawler->filterXPath('attribute::*'), 'The fake root node has no attribute nodes');
  437. }
  438. public function testFilterXPathWithAttributeAxisAfterElementAxis()
  439. {
  440. $this->assertCount(3, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis');
  441. }
  442. public function testFilterXPathWithChildAxis()
  443. {
  444. $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]');
  445. $this->assertCount(1, $crawler->filterXPath('child::div'), 'A child selection finds only the current div');
  446. }
  447. public function testFilterXPathWithFollowingAxis()
  448. {
  449. $crawler = $this->createTestCrawler()->filterXPath('//a');
  450. $this->assertCount(0, $crawler->filterXPath('following::div'), 'The fake root node has no following nodes');
  451. }
  452. public function testFilterXPathWithFollowingSiblingAxis()
  453. {
  454. $crawler = $this->createTestCrawler()->filterXPath('//a');
  455. $this->assertCount(0, $crawler->filterXPath('following-sibling::div'), 'The fake root node has no following nodes');
  456. }
  457. public function testFilterXPathWithNamespaceAxis()
  458. {
  459. $crawler = $this->createTestCrawler()->filterXPath('//button');
  460. $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'The fake root node has no namespace nodes');
  461. }
  462. public function testFilterXPathWithNamespaceAxisAfterElementAxis()
  463. {
  464. $crawler = $this->createTestCrawler()->filterXPath('//div[@id="parent"]/namespace::*');
  465. $this->assertCount(0, $crawler->filterXPath('namespace::*'), 'Namespace axes cannot be requested');
  466. }
  467. public function testFilterXPathWithParentAxis()
  468. {
  469. $crawler = $this->createTestCrawler()->filterXPath('//button');
  470. $this->assertCount(0, $crawler->filterXPath('parent::*'), 'The fake root node has no parent nodes');
  471. }
  472. public function testFilterXPathWithPrecedingAxis()
  473. {
  474. $crawler = $this->createTestCrawler()->filterXPath('//form');
  475. $this->assertCount(0, $crawler->filterXPath('preceding::*'), 'The fake root node has no preceding nodes');
  476. }
  477. public function testFilterXPathWithPrecedingSiblingAxis()
  478. {
  479. $crawler = $this->createTestCrawler()->filterXPath('//form');
  480. $this->assertCount(0, $crawler->filterXPath('preceding-sibling::*'), 'The fake root node has no preceding nodes');
  481. }
  482. public function testFilterXPathWithSelfAxes()
  483. {
  484. $crawler = $this->createTestCrawler()->filterXPath('//a');
  485. $this->assertCount(0, $crawler->filterXPath('self::a'), 'The fake root node has no "real" element name');
  486. $this->assertCount(0, $crawler->filterXPath('self::a/img'), 'The fake root node has no "real" element name');
  487. $this->assertCount(10, $crawler->filterXPath('self::*/a'));
  488. }
  489. public function testFilter()
  490. {
  491. $crawler = $this->createTestCrawler();
  492. $this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler');
  493. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler');
  494. $crawler = $this->createTestCrawler()->filter('ul');
  495. $this->assertCount(6, $crawler->filter('li'), '->filter() filters the node list with the CSS selector');
  496. }
  497. public function testFilterWithDefaultNamespace()
  498. {
  499. $crawler = $this->createTestXmlCrawler()->filter('default|entry default|id');
  500. $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
  501. $this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
  502. }
  503. public function testFilterWithNamespace()
  504. {
  505. $crawler = $this->createTestXmlCrawler()->filter('yt|accessControl');
  506. $this->assertCount(2, $crawler, '->filter() automatically registers namespaces');
  507. }
  508. public function testFilterWithMultipleNamespaces()
  509. {
  510. $crawler = $this->createTestXmlCrawler()->filter('media|group yt|aspectRatio');
  511. $this->assertCount(1, $crawler, '->filter() automatically registers namespaces');
  512. $this->assertSame('widescreen', $crawler->text());
  513. }
  514. public function testFilterWithDefaultNamespaceOnly()
  515. {
  516. $crawler = new Crawler('<?xml version="1.0" encoding="UTF-8"?>
  517. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  518. <url>
  519. <loc>http://localhost/foo</loc>
  520. <changefreq>weekly</changefreq>
  521. <priority>0.5</priority>
  522. <lastmod>2012-11-16</lastmod>
  523. </url>
  524. <url>
  525. <loc>http://localhost/bar</loc>
  526. <changefreq>weekly</changefreq>
  527. <priority>0.5</priority>
  528. <lastmod>2012-11-16</lastmod>
  529. </url>
  530. </urlset>
  531. ');
  532. $this->assertEquals(2, $crawler->filter('url')->count());
  533. }
  534. public function testSelectLink()
  535. {
  536. $crawler = $this->createTestCrawler();
  537. $this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler');
  538. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler');
  539. $this->assertCount(1, $crawler->selectLink('Fabien\'s Foo'), '->selectLink() selects links by the node values');
  540. $this->assertCount(1, $crawler->selectLink('Fabien\'s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
  541. $this->assertCount(2, $crawler->selectLink('Fabien"s Foo'), '->selectLink() selects links by the node values');
  542. $this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
  543. $this->assertCount(1, $crawler->selectLink('\' Fabien"s Foo'), '->selectLink() selects links by the node values');
  544. $this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
  545. $this->assertCount(4, $crawler->selectLink('Foo'), '->selectLink() selects links by the node values');
  546. $this->assertCount(4, $crawler->selectLink('Bar'), '->selectLink() selects links by the node values');
  547. }
  548. public function testSelectImage()
  549. {
  550. $crawler = $this->createTestCrawler();
  551. $this->assertNotSame($crawler, $crawler->selectImage('Bar'), '->selectImage() returns a new instance of a crawler');
  552. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectImage() returns a new instance of a crawler');
  553. $this->assertCount(1, $crawler->selectImage('Fabien\'s Bar'), '->selectImage() selects images by alt attribute');
  554. $this->assertCount(2, $crawler->selectImage('Fabien"s Bar'), '->selectImage() selects images by alt attribute');
  555. $this->assertCount(1, $crawler->selectImage('\' Fabien"s Bar'), '->selectImage() selects images by alt attribute');
  556. }
  557. public function testSelectButton()
  558. {
  559. $crawler = $this->createTestCrawler();
  560. $this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
  561. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler');
  562. $this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
  563. $this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
  564. $this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
  565. $this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
  566. $this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
  567. $this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
  568. $this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
  569. $this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
  570. }
  571. public function testSelectButtonWithSingleQuotesInNameAttribute()
  572. {
  573. $html = <<<'HTML'
  574. <!DOCTYPE html>
  575. <html lang="en">
  576. <body>
  577. <div id="action">
  578. <a href="/index.php?r=site/login">Login</a>
  579. </div>
  580. <form id="login-form" action="/index.php?r=site/login" method="post">
  581. <button type="submit" name="Click 'Here'">Submit</button>
  582. </form>
  583. </body>
  584. </html>
  585. HTML;
  586. $crawler = new Crawler($html);
  587. $this->assertCount(1, $crawler->selectButton('Click \'Here\''));
  588. }
  589. public function testSelectButtonWithDoubleQuotesInNameAttribute()
  590. {
  591. $html = <<<'HTML'
  592. <!DOCTYPE html>
  593. <html lang="en">
  594. <body>
  595. <div id="action">
  596. <a href="/index.php?r=site/login">Login</a>
  597. </div>
  598. <form id="login-form" action="/index.php?r=site/login" method="post">
  599. <button type="submit" name='Click "Here"'>Submit</button>
  600. </form>
  601. </body>
  602. </html>
  603. HTML;
  604. $crawler = new Crawler($html);
  605. $this->assertCount(1, $crawler->selectButton('Click "Here"'));
  606. }
  607. public function testLink()
  608. {
  609. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
  610. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
  611. $this->assertEquals('POST', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
  612. $crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink');
  613. $this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance');
  614. try {
  615. $this->createTestCrawler()->filterXPath('//ol')->link();
  616. $this->fail('->link() throws an \InvalidArgumentException if the node list is empty');
  617. } catch (\InvalidArgumentException $e) {
  618. $this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty');
  619. }
  620. }
  621. /**
  622. * @expectedException \InvalidArgumentException
  623. * @expectedExceptionMessage The selected node should be instance of DOMElement
  624. */
  625. public function testInvalidLink()
  626. {
  627. $crawler = $this->createTestCrawler('http://example.com/bar/');
  628. $crawler->filterXPath('//li/text()')->link();
  629. }
  630. /**
  631. * @expectedException \InvalidArgumentException
  632. * @expectedExceptionMessage The selected node should be instance of DOMElement
  633. */
  634. public function testInvalidLinks()
  635. {
  636. $crawler = $this->createTestCrawler('http://example.com/bar/');
  637. $crawler->filterXPath('//li/text()')->link();
  638. }
  639. public function testImage()
  640. {
  641. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
  642. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $crawler->image(), '->image() returns an Image instance');
  643. try {
  644. $this->createTestCrawler()->filterXPath('//ol')->image();
  645. $this->fail('->image() throws an \InvalidArgumentException if the node list is empty');
  646. } catch (\InvalidArgumentException $e) {
  647. $this->assertTrue(true, '->image() throws an \InvalidArgumentException if the node list is empty');
  648. }
  649. }
  650. public function testSelectLinkAndLinkFiltered()
  651. {
  652. $html = <<<'HTML'
  653. <!DOCTYPE html>
  654. <html lang="en">
  655. <body>
  656. <div id="action">
  657. <a href="/index.php?r=site/login">Login</a>
  658. </div>
  659. <form id="login-form" action="/index.php?r=site/login" method="post">
  660. <button type="submit">Submit</button>
  661. </form>
  662. </body>
  663. </html>
  664. HTML;
  665. $crawler = new Crawler($html);
  666. $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']");
  667. $this->assertCount(0, $filtered->selectLink('Login'));
  668. $this->assertCount(1, $filtered->selectButton('Submit'));
  669. $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']");
  670. $this->assertCount(1, $filtered->selectLink('Login'));
  671. $this->assertCount(0, $filtered->selectButton('Submit'));
  672. $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login'));
  673. $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit'));
  674. }
  675. public function testChaining()
  676. {
  677. $crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>');
  678. $this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name'));
  679. }
  680. public function testLinks()
  681. {
  682. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
  683. $this->assertInternalType('array', $crawler->links(), '->links() returns an array');
  684. $this->assertCount(4, $crawler->links(), '->links() returns an array');
  685. $links = $crawler->links();
  686. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances');
  687. $this->assertEquals([], $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
  688. }
  689. public function testImages()
  690. {
  691. $crawler = $this->createTestCrawler('http://example.com/bar/')->selectImage('Bar');
  692. $this->assertInternalType('array', $crawler->images(), '->images() returns an array');
  693. $this->assertCount(4, $crawler->images(), '->images() returns an array');
  694. $images = $crawler->images();
  695. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Image', $images[0], '->images() returns an array of Image instances');
  696. $this->assertEquals([], $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
  697. }
  698. public function testForm()
  699. {
  700. $testCrawler = $this->createTestCrawler('http://example.com/bar/');
  701. $crawler = $testCrawler->selectButton('FooValue');
  702. $crawler2 = $testCrawler->selectButton('FooBarValue');
  703. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
  704. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance');
  705. $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute');
  706. $this->assertEquals(['FooName' => 'FooBar', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'], $crawler->form(['FooName' => 'FooBar'])->getValues(), '->form() takes an array of values to submit as its first argument');
  707. $this->assertEquals(['FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'], $crawler->form()->getValues(), '->getValues() returns correct form values');
  708. $this->assertEquals(['FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'], $crawler2->form()->getValues(), '->getValues() returns correct form values');
  709. try {
  710. $this->createTestCrawler()->filterXPath('//ol')->form();
  711. $this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
  712. } catch (\InvalidArgumentException $e) {
  713. $this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty');
  714. }
  715. }
  716. /**
  717. * @expectedException \InvalidArgumentException
  718. * @expectedExceptionMessage The selected node should be instance of DOMElement
  719. */
  720. public function testInvalidForm()
  721. {
  722. $crawler = $this->createTestCrawler('http://example.com/bar/');
  723. $crawler->filterXPath('//li/text()')->form();
  724. }
  725. public function testLast()
  726. {
  727. $crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
  728. $this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler');
  729. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler');
  730. $this->assertEquals('Three', $crawler->last()->text());
  731. }
  732. public function testFirst()
  733. {
  734. $crawler = $this->createTestCrawler()->filterXPath('//li');
  735. $this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler');
  736. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler');
  737. $this->assertEquals('One', $crawler->first()->text());
  738. }
  739. public function testSiblings()
  740. {
  741. $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
  742. $this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler');
  743. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler');
  744. $nodes = $crawler->siblings();
  745. $this->assertEquals(2, $nodes->count());
  746. $this->assertEquals('One', $nodes->eq(0)->text());
  747. $this->assertEquals('Three', $nodes->eq(1)->text());
  748. $nodes = $this->createTestCrawler()->filterXPath('//li')->eq(0)->siblings();
  749. $this->assertEquals(2, $nodes->count());
  750. $this->assertEquals('Two', $nodes->eq(0)->text());
  751. $this->assertEquals('Three', $nodes->eq(1)->text());
  752. try {
  753. $this->createTestCrawler()->filterXPath('//ol')->siblings();
  754. $this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty');
  755. } catch (\InvalidArgumentException $e) {
  756. $this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty');
  757. }
  758. }
  759. public function testNextAll()
  760. {
  761. $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
  762. $this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler');
  763. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler');
  764. $nodes = $crawler->nextAll();
  765. $this->assertEquals(1, $nodes->count());
  766. $this->assertEquals('Three', $nodes->eq(0)->text());
  767. try {
  768. $this->createTestCrawler()->filterXPath('//ol')->nextAll();
  769. $this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty');
  770. } catch (\InvalidArgumentException $e) {
  771. $this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty');
  772. }
  773. }
  774. public function testPreviousAll()
  775. {
  776. $crawler = $this->createTestCrawler()->filterXPath('//li')->eq(2);
  777. $this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler');
  778. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler');
  779. $nodes = $crawler->previousAll();
  780. $this->assertEquals(2, $nodes->count());
  781. $this->assertEquals('Two', $nodes->eq(0)->text());
  782. try {
  783. $this->createTestCrawler()->filterXPath('//ol')->previousAll();
  784. $this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty');
  785. } catch (\InvalidArgumentException $e) {
  786. $this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty');
  787. }
  788. }
  789. public function testChildren()
  790. {
  791. $crawler = $this->createTestCrawler()->filterXPath('//ul');
  792. $this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler');
  793. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler');
  794. $nodes = $crawler->children();
  795. $this->assertEquals(3, $nodes->count());
  796. $this->assertEquals('One', $nodes->eq(0)->text());
  797. $this->assertEquals('Two', $nodes->eq(1)->text());
  798. $this->assertEquals('Three', $nodes->eq(2)->text());
  799. try {
  800. $this->createTestCrawler()->filterXPath('//ol')->children();
  801. $this->fail('->children() throws an \InvalidArgumentException if the node list is empty');
  802. } catch (\InvalidArgumentException $e) {
  803. $this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
  804. }
  805. try {
  806. $crawler = new Crawler('<p></p>');
  807. $crawler->filter('p')->children();
  808. $this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
  809. } catch (\PHPUnit\Framework\Error\Notice $e) {
  810. $this->fail('->children() does not trigger a notice if the node has no children');
  811. } catch (\PHPUnit_Framework_Error_Notice $e) {
  812. $this->fail('->children() does not trigger a notice if the node has no children');
  813. }
  814. }
  815. public function testFilteredChildren()
  816. {
  817. $html = <<<'HTML'
  818. <!DOCTYPE html>
  819. <html lang="en">
  820. <body>
  821. <div id="foo">
  822. <div class="lorem">
  823. <p class="lorem"></p>
  824. </div>
  825. <div class="lorem">
  826. <span class="lorem"></span>
  827. </div>
  828. <span class="ipsum"></span>
  829. </div>
  830. </body>
  831. </html>
  832. HTML;
  833. $crawler = new Crawler($html);
  834. $foo = $crawler->filter('#foo');
  835. $this->assertEquals(3, $foo->children()->count());
  836. $this->assertEquals(2, $foo->children('.lorem')->count());
  837. $this->assertEquals(2, $foo->children('div')->count());
  838. $this->assertEquals(2, $foo->children('div.lorem')->count());
  839. $this->assertEquals(1, $foo->children('span')->count());
  840. $this->assertEquals(1, $foo->children('span.ipsum')->count());
  841. $this->assertEquals(1, $foo->children('.ipsum')->count());
  842. }
  843. public function testParents()
  844. {
  845. $crawler = $this->createTestCrawler()->filterXPath('//li[1]');
  846. $this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
  847. $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
  848. $nodes = $crawler->parents();
  849. $this->assertEquals(3, $nodes->count());
  850. $nodes = $this->createTestCrawler()->filterXPath('//html')->parents();
  851. $this->assertEquals(0, $nodes->count());
  852. try {
  853. $this->createTestCrawler()->filterXPath('//ol')->parents();
  854. $this->fail('->parents() throws an \InvalidArgumentException if the node list is empty');
  855. } catch (\InvalidArgumentException $e) {
  856. $this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty');
  857. }
  858. }
  859. /**
  860. * @dataProvider getBaseTagData
  861. */
  862. public function testBaseTag($baseValue, $linkValue, $expectedUri, $currentUri = null, $description = '')
  863. {
  864. $crawler = new Crawler('<html><base href="'.$baseValue.'"><a href="'.$linkValue.'"></a></html>', $currentUri);
  865. $this->assertEquals($expectedUri, $crawler->filterXPath('//a')->link()->getUri(), $description);
  866. }
  867. public function getBaseTagData()
  868. {
  869. return [
  870. ['http://base.com', 'link', 'http://base.com/link'],
  871. ['//base.com', 'link', 'https://base.com/link', 'https://domain.com', '<base> tag can use a schema-less URL'],
  872. ['path/', 'link', 'https://domain.com/path/link', 'https://domain.com', '<base> tag can set a path'],
  873. ['http://base.com', '#', 'http://base.com#', 'http://domain.com/path/link', '<base> tag does work with links to an anchor'],
  874. ['http://base.com', '', 'http://base.com', 'http://domain.com/path/link', '<base> tag does work with empty links'],
  875. ];
  876. }
  877. /**
  878. * @dataProvider getBaseTagWithFormData
  879. */
  880. public function testBaseTagWithForm($baseValue, $actionValue, $expectedUri, $currentUri = null, $description = null)
  881. {
  882. $crawler = new Crawler('<html><base href="'.$baseValue.'"><form method="post" action="'.$actionValue.'"><button type="submit" name="submit"/></form></html>', $currentUri);
  883. $this->assertEquals($expectedUri, $crawler->filterXPath('//button')->form()->getUri(), $description);
  884. }
  885. public function getBaseTagWithFormData()
  886. {
  887. return [
  888. ['https://base.com/', 'link/', 'https://base.com/link/', 'https://base.com/link/', '<base> tag does work with a path and relative form action'],
  889. ['/basepath', '/registration', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and form action'],
  890. ['/basepath', '', 'http://domain.com/registration', 'http://domain.com/registration', '<base> tag does work with a path and empty form action'],
  891. ['http://base.com/', '/registration', 'http://base.com/registration', 'http://domain.com/registration', '<base> tag does work with a URL and form action'],
  892. ['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'],
  893. ['http://base.com/path', '/registration', 'http://base.com/registration', 'http://domain.com/path/form', '<base> tag does work with a URL and form action'],
  894. ];
  895. }
  896. public function testCountOfNestedElements()
  897. {
  898. $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>');
  899. $this->assertCount(1, $crawler->filter('li:contains("List item 1")'));
  900. }
  901. public function testEvaluateReturnsTypedResultOfXPathExpressionOnADocumentSubset()
  902. {
  903. $crawler = $this->createTestCrawler();
  904. $result = $crawler->filterXPath('//form/input')->evaluate('substring-before(@name, "Name")');
  905. $this->assertSame(['Text', 'Foo', 'Bar'], $result);
  906. }
  907. public function testEvaluateReturnsTypedResultOfNamespacedXPathExpressionOnADocumentSubset()
  908. {
  909. $crawler = $this->createTestXmlCrawler();
  910. $result = $crawler->filterXPath('//yt:accessControl/@action')->evaluate('string(.)');
  911. $this->assertSame(['comment', 'videoRespond'], $result);
  912. }
  913. public function testEvaluateReturnsTypedResultOfNamespacedXPathExpression()
  914. {
  915. $crawler = $this->createTestXmlCrawler();
  916. $crawler->registerNamespace('youtube', 'http://gdata.youtube.com/schemas/2007');
  917. $result = $crawler->evaluate('string(//youtube:accessControl/@action)');
  918. $this->assertSame(['comment'], $result);
  919. }
  920. public function testEvaluateReturnsACrawlerIfXPathExpressionEvaluatesToANode()
  921. {
  922. $crawler = $this->createTestCrawler()->evaluate('//form/input[1]');
  923. $this->assertInstanceOf(Crawler::class, $crawler);
  924. $this->assertCount(1, $crawler);
  925. $this->assertSame('input', $crawler->first()->nodeName());
  926. }
  927. /**
  928. * @expectedException \LogicException
  929. */
  930. public function testEvaluateThrowsAnExceptionIfDocumentIsEmpty()
  931. {
  932. (new Crawler())->evaluate('//form/input[1]');
  933. }
  934. /**
  935. * @group legacy
  936. * @expectedDeprecation The "Symfony\Component\DomCrawler\Crawler::children()" method will have a new "string $selector = null" argument in version 5.0, not defining it is deprecated since Symfony 4.2.
  937. */
  938. public function testInheritedClassCallChildrenWithoutArgument()
  939. {
  940. $dom = new \DOMDocument();
  941. $dom->loadHTML('
  942. <html>
  943. <body>
  944. <a href="foo">Foo</a>
  945. <a href="/foo"> Fabien\'s Foo </a>
  946. <a href="/foo">Fabien"s Foo</a>
  947. <a href="/foo">\' Fabien"s Foo</a>
  948. <a href="/bar"><img alt="Bar"/></a>
  949. <a href="/bar"><img alt=" Fabien\'s Bar "/></a>
  950. <a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
  951. <a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
  952. <a href="?get=param">GetLink</a>
  953. <a href="/example">Klausi|Claudiu</a>
  954. <form action="foo" id="FooFormId">
  955. <input type="text" value="TextValue" name="TextName" />
  956. <input type="submit" value="FooValue" name="FooName" id="FooId" />
  957. <input type="button" value="BarValue" name="BarName" id="BarId" />
  958. <button value="ButtonValue" name="ButtonName" id="ButtonId" />
  959. </form>
  960. <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
  961. <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
  962. <ul class="first">
  963. <li class="first">One</li>
  964. <li>Two</li>
  965. <li>Three</li>
  966. </ul>
  967. <ul>
  968. <li>One Bis</li>
  969. <li>Two Bis</li>
  970. <li>Three Bis</li>
  971. </ul>
  972. <div id="parent">
  973. <div id="child"></div>
  974. <div id="child2" xmlns:foo="http://example.com"></div>
  975. </div>
  976. <div id="sibling"><img /></div>
  977. </body>
  978. </html>
  979. ');
  980. $crawlerChild = new ClassThatInheritCrawler($dom);
  981. $crawlerChild->children();
  982. }
  983. public function createTestCrawler($uri = null)
  984. {
  985. $dom = new \DOMDocument();
  986. $dom->loadHTML('
  987. <html>
  988. <body>
  989. <a href="foo">Foo</a>
  990. <a href="/foo"> Fabien\'s Foo </a>
  991. <a href="/foo">Fabien"s Foo</a>
  992. <a href="/foo">\' Fabien"s Foo</a>
  993. <a href="/bar"><img alt="Bar"/></a>
  994. <a href="/bar"><img alt=" Fabien\'s Bar "/></a>
  995. <a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
  996. <a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
  997. <a href="?get=param">GetLink</a>
  998. <a href="/example">Klausi|Claudiu</a>
  999. <form action="foo" id="FooFormId">
  1000. <input type="text" value="TextValue" name="TextName" />
  1001. <input type="submit" value="FooValue" name="FooName" id="FooId" />
  1002. <input type="button" value="BarValue" name="BarName" id="BarId" />
  1003. <button value="ButtonValue" name="ButtonName" id="ButtonId" />
  1004. </form>
  1005. <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
  1006. <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
  1007. <ul class="first">
  1008. <li class="first">One</li>
  1009. <li>Two</li>
  1010. <li>Three</li>
  1011. </ul>
  1012. <ul>
  1013. <li>One Bis</li>
  1014. <li>Two Bis</li>
  1015. <li>Three Bis</li>
  1016. </ul>
  1017. <div id="parent">
  1018. <div id="child"></div>
  1019. <div id="child2" xmlns:foo="http://example.com"></div>
  1020. </div>
  1021. <div id="sibling"><img /></div>
  1022. </body>
  1023. </html>
  1024. ');
  1025. return new Crawler($dom, $uri);
  1026. }
  1027. protected function createTestXmlCrawler($uri = null)
  1028. {
  1029. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  1030. <entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">
  1031. <id>tag:youtube.com,2008:video:kgZRZmEc9j4</id>
  1032. <yt:accessControl action="comment" permission="allowed"/>
  1033. <yt:accessControl action="videoRespond" permission="moderated"/>
  1034. <media:group>
  1035. <media:title type="plain">Chordates - CrashCourse Biology #24</media:title>
  1036. <yt:aspectRatio>widescreen</yt:aspectRatio>
  1037. </media:group>
  1038. <media:category label="Music" scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music</media:category>
  1039. </entry>';
  1040. return new Crawler($xml, $uri);
  1041. }
  1042. protected function createDomDocument()
  1043. {
  1044. $dom = new \DOMDocument();
  1045. $dom->loadXML('<html><div class="foo"></div></html>');
  1046. return $dom;
  1047. }
  1048. protected function createNodeList()
  1049. {
  1050. $dom = new \DOMDocument();
  1051. $dom->loadXML('<html><div class="foo"></div></html>');
  1052. $domxpath = new \DOMXPath($dom);
  1053. return $domxpath->query('//div');
  1054. }
  1055. }
  1056. class ClassThatInheritCrawler extends Crawler
  1057. {
  1058. public function children()
  1059. {
  1060. parent::children();
  1061. }
  1062. }