This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

873 lines
37 KiB
PHP
Raw Normal View History

2010-04-15 13:41:42 +01:00
<?php
/*
2010-04-25 16:06:54 +01:00
* This file is part of the Symfony package.
2010-04-15 13:41:42 +01:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2010-04-15 13:41:42 +01:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DomCrawler\Tests;
2010-04-15 13:41:42 +01:00
use Symfony\Component\DomCrawler\Crawler;
2010-04-15 13:41:42 +01:00
class CrawlerTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
2010-04-15 13:41:42 +01:00
{
$crawler = new Crawler();
2012-04-12 09:09:52 +01:00
$this->assertCount(0, $crawler, '__construct() returns an empty crawler');
2010-04-15 13:41:42 +01:00
$crawler = new Crawler(new \DOMNode());
2012-04-12 09:09:52 +01:00
$this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
2010-04-15 13:41:42 +01:00
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::add
*/
public function testAdd()
2010-04-15 13:41:42 +01:00
{
$crawler = new Crawler();
$crawler->add($this->createDomDocument());
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMDocument');
$crawler = new Crawler();
$crawler->add($this->createNodeList());
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList');
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
$crawler = new Crawler();
$crawler->add($list);
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an array of nodes');
$crawler = new Crawler();
$crawler->add($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an \DOMNode');
2010-06-27 18:44:04 +01:00
$crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>');
$this->assertEquals('Foo', $crawler->filterXPath('//body')->text(), '->add() adds nodes from a string');
2010-04-15 13:41:42 +01:00
}
/**
* @expectedException \InvalidArgumentException
*/
public function testAddInvalidNode()
{
$crawler = new Crawler();
$crawler->add(1);
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContent()
2010-04-15 13:41:42 +01:00
{
$crawler = new Crawler();
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
2011-05-07 15:15:13 +01:00
$crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
$this->assertEquals('http://symfony.com', $crawler->filterXPath('//base')->attr('href'), '->addHtmlContent() adds nodes from an HTML string');
$this->assertEquals('http://symfony.com/contact', $crawler->filterXPath('//a')->link()->getUri(), '->addHtmlContent() adds nodes from an HTML string');
2010-04-15 13:41:42 +01:00
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentCharset()
{
$crawler = new Crawler();
$crawler->addHtmlContent('<html><div class="foo">Tiếng Việt</html>', 'UTF-8');
$this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentInvalidBaseTag()
{
$crawler = new Crawler(null, 'http://symfony.com');
$crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8');
$this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute');
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentUnsupportedCharset()
{
$crawler = new Crawler();
$crawler->addHtmlContent(file_get_contents(__DIR__.'/Fixtures/windows-1250.html'), 'Windows-1250');
$this->assertEquals('Žťčýů', $crawler->filterXPath('//p')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentCharsetGbk()
{
$crawler = new Crawler();
//gbk encode of <html><p>中文</p></html>
$crawler->addHtmlContent(base64_decode('PGh0bWw+PHA+1tDOxDwvcD48L2h0bWw+'), 'gbk');
$this->assertEquals('中文', $crawler->filterXPath('//p')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentWithErrors()
{
$internalErrors = libxml_use_internal_errors(true);
$crawler = new Crawler();
$crawler->addHtmlContent(<<<EOF
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<nav><a href="#"><a href="#"></nav>
</body>
</html>
EOF
, 'UTF-8');
$errors = libxml_get_errors();
$this->assertCount(1, $errors);
$this->assertEquals("Tag nav invalid\n", $errors[0]->message);
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addXmlContent
*/
public function testAddXmlContent()
2010-04-15 13:41:42 +01:00
{
$crawler = new Crawler();
$crawler->addXmlContent('<html><div class="foo"></div></html>', 'UTF-8');
2010-04-15 13:41:42 +01:00
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addXmlContent() adds nodes from an XML string');
}
2010-04-15 13:41:42 +01:00
/**
* @covers Symfony\Component\DomCrawler\Crawler::addXmlContent
*/
public function testAddXmlContentCharset()
{
$crawler = new Crawler();
$crawler->addXmlContent('<html><div class="foo">Tiếng Việt</div></html>', 'UTF-8');
$this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addXmlContent
*/
public function testAddXmlContentWithErrors()
{
$internalErrors = libxml_use_internal_errors(true);
$crawler = new Crawler();
$crawler->addXmlContent(<<<EOF
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<nav><a href="#"><a href="#"></nav>
</body>
</html>
EOF
, 'UTF-8');
$this->assertTrue(count(libxml_get_errors()) > 1);
libxml_clear_errors();
libxml_use_internal_errors($internalErrors);
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addContent
*/
public function testAddContent()
{
$crawler = new Crawler();
$crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string');
2010-04-15 13:41:42 +01:00
$crawler = new Crawler();
$crawler->addContent('<html><div class="foo"></html>', 'text/html; charset=UTF-8; dir=RTL');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an HTML string with extended content type');
$crawler = new Crawler();
$crawler->addContent('<html><div class="foo"></html>');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() uses text/html as the default type');
2010-04-15 13:41:42 +01:00
$crawler = new Crawler();
$crawler->addContent('<html><div class="foo"></div></html>', 'text/xml; charset=UTF-8');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
2010-04-15 13:41:42 +01:00
$crawler = new Crawler();
$crawler->addContent('<html><div class="foo"></div></html>', 'text/xml');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addContent() adds nodes from an XML string');
2010-04-15 13:41:42 +01:00
$crawler = new Crawler();
$crawler->addContent('foo bar', 'text/plain');
2012-04-12 09:09:52 +01:00
$this->assertCount(0, $crawler, '->addContent() does nothing if the type is not (x|ht)ml');
$crawler = new Crawler();
$crawler->addContent('<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><span>中文</span></html>');
$this->assertEquals('中文', $crawler->filterXPath('//span')->text(), '->addContent() guess wrong charset');
$crawler = new Crawler();
$crawler->addContent(mb_convert_encoding('<html><head><meta charset="Shift_JIS"></head><body>日本語</body></html>', 'SJIS', 'UTF-8'));
$this->assertEquals('日本語', $crawler->filterXPath('//body')->text(), '->addContent() can recognize "Shift_JIS" in html5 meta charset tag');
}
2010-04-15 13:41:42 +01:00
/**
* @covers Symfony\Component\DomCrawler\Crawler::addDocument
*/
public function testAddDocument()
{
$crawler = new Crawler();
$crawler->addDocument($this->createDomDocument());
2010-04-15 13:41:42 +01:00
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addDocument() adds nodes from a \DOMDocument');
}
2010-04-15 13:41:42 +01:00
/**
* @covers Symfony\Component\DomCrawler\Crawler::addNodeList
*/
public function testAddNodeList()
{
$crawler = new Crawler();
$crawler->addNodeList($this->createNodeList());
2010-04-15 13:41:42 +01:00
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodeList() adds nodes from a \DOMNodeList');
}
2010-04-15 13:41:42 +01:00
/**
* @covers Symfony\Component\DomCrawler\Crawler::addNodes
*/
public function testAddNodes()
{
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
2010-04-15 13:41:42 +01:00
$crawler = new Crawler();
$crawler->addNodes($list);
2010-04-15 13:41:42 +01:00
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNodes() adds nodes from an array of nodes');
}
2010-04-15 13:41:42 +01:00
/**
* @covers Symfony\Component\DomCrawler\Crawler::addNode
*/
public function testAddNode()
{
$crawler = new Crawler();
$crawler->addNode($this->createNodeList()->item(0));
2010-04-15 13:41:42 +01:00
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from an \DOMNode');
}
2010-04-15 13:41:42 +01:00
public function testClear()
{
$crawler = new Crawler(new \DOMNode());
$crawler->clear();
2012-04-12 09:09:52 +01:00
$this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
}
2010-04-15 13:41:42 +01:00
public function testEq()
{
$crawler = $this->createTestCrawler()->filterXPath('//li');
$this->assertNotSame($crawler, $crawler->eq(0), '->eq() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->eq() returns a new instance of a crawler');
2010-04-15 13:41:42 +01:00
$this->assertEquals('Two', $crawler->eq(1)->text(), '->eq() returns the nth node of the list');
$this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
}
2010-04-15 13:41:42 +01:00
public function testEach()
{
$data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
return $i.'-'.$node->text();
});
2010-04-15 13:41:42 +01:00
$this->assertEquals(array('0-One', '1-Two', '2-Three'), $data, '->each() executes an anonymous function on each node of the list');
}
2010-04-15 13:41:42 +01:00
public function testReduce()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
2012-05-20 17:15:10 +01:00
$nodes = $crawler->reduce(function ($node, $i) {
return $i == 1 ? false : true;
});
$this->assertNotSame($nodes, $crawler, '->reduce() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $nodes, '->reduce() returns a new instance of a crawler');
2012-04-12 09:09:52 +01:00
$this->assertCount(2, $nodes, '->reduce() filters the nodes in the list');
2010-04-15 13:41:42 +01:00
}
public function testAttr()
2010-04-15 13:41:42 +01:00
{
$this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list');
try {
$this->createTestCrawler()->filterXPath('//ol')->attr('class');
$this->fail('->attr() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testText()
{
$this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');
try {
$this->createTestCrawler()->filterXPath('//ol')->text();
$this->fail('->text() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
}
}
2010-04-15 13:41:42 +01:00
public function testHtml()
{
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
$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($this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html()));
try {
$this->createTestCrawler()->filterXPath('//ol')->html();
$this->fail('->html() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
}
}
public function testExtract()
{
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
2010-04-15 13:41:42 +01:00
$this->assertEquals(array('One', 'Two', 'Three'), $crawler->extract('_text'), '->extract() returns an array of extracted data from the node list');
$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');
2010-04-15 13:41:42 +01:00
$this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
}
2010-04-15 13:41:42 +01:00
public function testFilterXpathComplexQueries()
{
$crawler = $this->createTestCrawler()->filterXPath('//body');
$this->assertCount(0, $crawler->filterXPath('/input'));
$this->assertCount(0, $crawler->filterXPath('/body'));
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
$this->assertCount(1, $crawler->filterXPath('./body'));
$this->assertCount(1, $crawler->filterXPath('.//body'));
$this->assertCount(5, $crawler->filterXPath('.//input'));
$this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
$this->assertCount(1, $crawler->filterXPath('body'));
$this->assertCount(6, $crawler->filterXPath('//button | //input'));
$this->assertCount(1, $crawler->filterXPath('//body'));
$this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
$this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
$this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child');
$this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child');
$this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
$this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
$this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::filterXPath
*/
public function testFilterXPath()
{
$crawler = $this->createTestCrawler();
$this->assertNotSame($crawler, $crawler->filterXPath('//li'), '->filterXPath() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');
2010-04-15 13:41:42 +01:00
$crawler = $this->createTestCrawler()->filterXPath('//ul');
$this->assertCount(6, $crawler->filterXPath('//li'), '->filterXPath() filters the node list with the XPath expression');
$crawler = $this->createTestCrawler();
$this->assertCount(3, $crawler->filterXPath('//body')->filterXPath('//button')->parents(), '->filterXpath() preserves parents when chained');
}
2010-04-15 13:41:42 +01:00
public function testFilterXPathWithAncestorAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
$this->assertCount(2, $crawler->filterXPath('ancestor::*'));
}
public function testFilterXPathWithAncestorOrSelfAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
$this->assertCount(3, $crawler->filterXPath('ancestor-or-self::*'));
}
public function testFilterXPathWithAttributeAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
$this->assertCount(2, $crawler->filterXPath('attribute::*'));
}
public function testFilterXPathWithChildAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//body');
$this->assertCount(2, $crawler->filterXPath('child::input'));
}
public function testFilterXPathWithFollowingAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//a');
$this->assertCount(3, $crawler->filterXPath('following::div'));
}
public function testFilterXPathWithFollowingSiblingAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//a');
$this->assertCount(2, $crawler->filterXPath('following-sibling::div'));
}
public function testFilterXPathWithParentAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//button');
$this->assertEquals('foo', $crawler->filterXPath('parent::*')->attr('action'));
}
public function testFilterXPathWithPrecedingAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
$this->assertCount(13, $crawler->filterXPath('preceding::*'));
}
public function testFilterXPathWithPrecedingSiblingAxis()
{
$crawler = $this->createTestCrawler()->filterXPath('//form');
$this->assertCount(9, $crawler->filterXPath('preceding-sibling::*'));
}
public function testFilterXPathWithSelfAxes()
{
$this->assertCount(1, $this->createTestCrawler()->filterXPath('self::*'));
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::filter
*/
public function testFilter()
2010-04-15 13:41:42 +01:00
{
if (!class_exists('Symfony\Component\CssSelector\CssSelector')) {
$this->markTestSkipped('The "CssSelector" component is not available');
}
$crawler = $this->createTestCrawler();
$this->assertNotSame($crawler, $crawler->filter('li'), '->filter() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filter() returns a new instance of a crawler');
$crawler = $this->createTestCrawler()->filter('ul');
$this->assertCount(6, $crawler->filter('li'), '->filter() filters the node list with the CSS selector');
2010-04-15 13:41:42 +01:00
}
public function testSelectLink()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler();
$this->assertNotSame($crawler, $crawler->selectLink('Foo'), '->selectLink() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectLink() returns a new instance of a crawler');
2010-04-15 13:41:42 +01:00
$this->assertCount(1, $crawler->selectLink('Fabien\'s Foo'), '->selectLink() selects links by the node values');
$this->assertCount(1, $crawler->selectLink('Fabien\'s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
2010-04-15 13:41:42 +01:00
$this->assertCount(2, $crawler->selectLink('Fabien"s Foo'), '->selectLink() selects links by the node values');
$this->assertCount(2, $crawler->selectLink('Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
2010-04-15 13:41:42 +01:00
$this->assertCount(1, $crawler->selectLink('\' Fabien"s Foo'), '->selectLink() selects links by the node values');
$this->assertCount(1, $crawler->selectLink('\' Fabien"s Bar'), '->selectLink() selects links by the alt attribute of a clickable image');
2010-04-15 13:41:42 +01:00
$this->assertCount(4, $crawler->selectLink('Foo'), '->selectLink() selects links by the node values');
$this->assertCount(4, $crawler->selectLink('Bar'), '->selectLink() selects links by the node values');
}
2010-04-15 13:41:42 +01:00
public function testSelectButton()
{
$crawler = $this->createTestCrawler();
$this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->selectButton() returns a new instance of a crawler');
2010-04-15 13:41:42 +01:00
$this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
2010-04-15 13:41:42 +01:00
$this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
$this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
}
2010-04-15 13:41:42 +01:00
public function testSelectButtonWithSingleQuotesInNameAttribute()
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit" name="Click 'Here'">Submit</button>
</form>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$this->assertCount(1, $crawler->selectButton('Click \'Here\''));
}
public function testSelectButtonWithDoubleQuotesInNameAttribute()
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit" name='Click "Here"'>Submit</button>
</form>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$this->assertCount(1, $crawler->selectButton('Click "Here"'));
}
public function testLink()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $crawler->link(), '->link() returns a Link instance');
2011-05-25 14:38:02 +01:00
$this->assertEquals('POST', $crawler->link('post')->getMethod(), '->link() takes a method as its argument');
$crawler = $this->createTestCrawler('http://example.com/bar')->selectLink('GetLink');
$this->assertEquals('http://example.com/bar?get=param', $crawler->link()->getUri(), '->link() returns a Link instance');
try {
$this->createTestCrawler()->filterXPath('//ol')->link();
$this->fail('->link() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->link() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testSelectLinkAndLinkFiltered()
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit">Submit</button>
</form>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']");
$this->assertCount(0, $filtered->selectLink('Login'));
$this->assertCount(1, $filtered->selectButton('Submit'));
$filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']");
$this->assertCount(1, $filtered->selectLink('Login'));
$this->assertCount(0, $filtered->selectButton('Submit'));
$this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login'));
$this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit'));
}
public function testChaining()
{
$crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>');
$this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name'));
}
public function testLinks()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
$this->assertInternalType('array', $crawler->links(), '->links() returns an array');
2010-04-15 13:41:42 +01:00
$this->assertCount(4, $crawler->links(), '->links() returns an array');
$links = $crawler->links();
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Link', $links[0], '->links() returns an array of Link instances');
2010-04-15 13:41:42 +01:00
$this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->links(), '->links() returns an empty array if the node selection is empty');
}
2010-04-15 13:41:42 +01:00
public function testForm()
2010-04-15 13:41:42 +01:00
{
$testCrawler = $this->createTestCrawler('http://example.com/bar/');
$crawler = $testCrawler->selectButton('FooValue');
$crawler2 = $testCrawler->selectButton('FooBarValue');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance');
$this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute');
$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');
$this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->getValues() returns correct form values');
$this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->getValues() returns correct form values');
try {
$this->createTestCrawler()->filterXPath('//ol')->form();
$this->fail('->form() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->form() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testLast()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
$this->assertNotSame($crawler, $crawler->last(), '->last() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->last() returns a new instance of a crawler');
$this->assertEquals('Three', $crawler->last()->text());
2010-04-15 13:41:42 +01:00
}
public function testFirst()
{
$crawler = $this->createTestCrawler()->filterXPath('//li');
$this->assertNotSame($crawler, $crawler->first(), '->first() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->first() returns a new instance of a crawler');
2010-04-15 13:41:42 +01:00
$this->assertEquals('One', $crawler->first()->text());
}
2010-04-15 13:41:42 +01:00
public function testSiblings()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
$this->assertNotSame($crawler, $crawler->siblings(), '->siblings() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->siblings() returns a new instance of a crawler');
$nodes = $crawler->siblings();
$this->assertEquals(2, $nodes->count());
$this->assertEquals('One', $nodes->eq(0)->text());
$this->assertEquals('Three', $nodes->eq(1)->text());
$nodes = $this->createTestCrawler()->filterXPath('//li')->eq(0)->siblings();
$this->assertEquals(2, $nodes->count());
$this->assertEquals('Two', $nodes->eq(0)->text());
$this->assertEquals('Three', $nodes->eq(1)->text());
try {
$this->createTestCrawler()->filterXPath('//ol')->siblings();
$this->fail('->siblings() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->siblings() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testNextAll()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//li')->eq(1);
$this->assertNotSame($crawler, $crawler->nextAll(), '->nextAll() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->nextAll() returns a new instance of a crawler');
$nodes = $crawler->nextAll();
$this->assertEquals(1, $nodes->count());
$this->assertEquals('Three', $nodes->eq(0)->text());
try {
$this->createTestCrawler()->filterXPath('//ol')->nextAll();
$this->fail('->nextAll() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->nextAll() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testPreviousAll()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//li')->eq(2);
$this->assertNotSame($crawler, $crawler->previousAll(), '->previousAll() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->previousAll() returns a new instance of a crawler');
$nodes = $crawler->previousAll();
$this->assertEquals(2, $nodes->count());
$this->assertEquals('Two', $nodes->eq(0)->text());
try {
$this->createTestCrawler()->filterXPath('//ol')->previousAll();
$this->fail('->previousAll() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->previousAll() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testChildren()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//ul');
$this->assertNotSame($crawler, $crawler->children(), '->children() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->children() returns a new instance of a crawler');
$nodes = $crawler->children();
$this->assertEquals(3, $nodes->count());
$this->assertEquals('One', $nodes->eq(0)->text());
$this->assertEquals('Two', $nodes->eq(1)->text());
$this->assertEquals('Three', $nodes->eq(2)->text());
try {
$this->createTestCrawler()->filterXPath('//ol')->children();
$this->fail('->children() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->children() throws an \InvalidArgumentException if the node list is empty');
}
try {
$crawler = new Crawler('<p></p>');
$crawler->filter('p')->children();
$this->assertTrue(true, '->children() does not trigger a notice if the node has no children');
} catch (\PHPUnit_Framework_Error_Notice $e) {
$this->fail('->children() does not trigger a notice if the node has no children');
}
2010-04-15 13:41:42 +01:00
}
public function testParents()
2010-04-15 13:41:42 +01:00
{
$crawler = $this->createTestCrawler()->filterXPath('//li[1]');
$this->assertNotSame($crawler, $crawler->parents(), '->parents() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->parents() returns a new instance of a crawler');
$nodes = $crawler->parents();
$this->assertEquals(3, $nodes->count());
$nodes = $this->createTestCrawler()->filterXPath('//html')->parents();
$this->assertEquals(0, $nodes->count());
try {
$this->createTestCrawler()->filterXPath('//ol')->parents();
$this->fail('->parents() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->parents() throws an \InvalidArgumentException if the node list is empty');
}
2010-04-15 13:41:42 +01:00
}
public function testBaseTag()
{
$crawler = new Crawler('<html><base href="http://base.com"><a href="link"></a></html>');
$this->assertEquals('http://base.com/link', $crawler->filterXPath('//a')->link()->getUri());
$crawler = new Crawler('<html><base href="//base.com"><a href="link"></a></html>', 'https://domain.com');
2013-12-28 08:32:39 +00:00
$this->assertEquals('https://base.com/link', $crawler->filterXPath('//a')->link()->getUri(), '<base> tag can use a schema-less URL');
$crawler = new Crawler('<html><base href="path/"><a href="link"></a></html>', 'https://domain.com');
$this->assertEquals('https://domain.com/path/link', $crawler->filterXPath('//a')->link()->getUri(), '<base> tag can set a path');
}
public function createTestCrawler($uri = null)
2010-04-15 13:41:42 +01:00
{
$dom = new \DOMDocument();
$dom->loadHTML('
<html>
<body>
<a href="foo">Foo</a>
<a href="/foo"> Fabien\'s Foo </a>
<a href="/foo">Fabien"s Foo</a>
<a href="/foo">\' Fabien"s Foo</a>
<a href="/bar"><img alt="Bar"/></a>
<a href="/bar"><img alt=" Fabien\'s Bar "/></a>
<a href="/bar"><img alt="Fabien&quot;s Bar"/></a>
<a href="/bar"><img alt="\' Fabien&quot;s Bar"/></a>
<a href="?get=param">GetLink</a>
<form action="foo" id="FooFormId">
<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" />
</form>
<input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
<input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
<ul class="first">
<li class="first">One</li>
<li>Two</li>
<li>Three</li>
</ul>
<ul>
<li>One Bis</li>
<li>Two Bis</li>
<li>Three Bis</li>
</ul>
<div id="parent">
<div id="child"></div>
</div>
<div id="sibling"><img /></div>
</body>
</html>
');
return new Crawler($dom, $uri);
2010-04-15 13:41:42 +01:00
}
protected function createDomDocument()
{
$dom = new \DOMDocument();
$dom->loadXML('<html><div class="foo"></div></html>');
2010-04-15 13:41:42 +01:00
return $dom;
}
2010-04-15 13:41:42 +01:00
protected function createNodeList()
{
$dom = new \DOMDocument();
$dom->loadXML('<html><div class="foo"></div></html>');
$domxpath = new \DOMXPath($dom);
2010-04-15 13:41:42 +01:00
return $domxpath->query('//div');
}
2010-04-15 13:41:42 +01:00
}