Symfony2 DOMCrawler selectLink returns null uri

I have a problem writing functional tests and DOMCrawler. My problem is to bypass email content with a link. In the docs, I saw that a crawler can be created with html content as a parameter. So this is my piece of code:

$mailCrawler = new Crawler($message->getBody());
$linkCrawler = $mailCrawler->selectLink('Link name');
$client->click($linkCrawler->link());

On the third line, I have an exception, because $ linkCrawler has an empty $ uri field. Exception Message:

InvalidArgumentException: Current URI must be an absolute URL ("").

Can someone tell me why the caterpillar cannot get this link?

I can only say that the $ message var getBody method returns the correct content.

Hi

+5
source share
1 answer

You need to provide the URL of the root crawler. Example:

$crawler = new Crawler('', 'http://www.example.com');
$crawler->addHtmlContent("
    <!DOCTYPE html>
    <html>
        <body>
            <a href=\"/rel-link\">rel-link-text</a>
            <a href=\"http://another.com/abs-link\">abs-link-text</a>
        </body>
    </html>
", 'UTF-8');

$cLink1 = $crawler->selectLink('rel-link-text')->eq(0);
$l1 = $cLink1->link();
echo $l1->getUri(); // http://www.example.com/rel-link

$cLink2 = $crawler->selectLink('abs-link-text')->eq(0);
$l2 = $cLink2->link();
echo $l2->getUri(); // http://another.com/abs-link
+6
source

All Articles