I have a problem with writing functional tests and DOMCrawler. My issue is to crawl through mail content with link. From docs i saw that the crawler can be created with html content as parameter. So this is my chunk of code:
$mailCrawler = new Crawler($message->getBody());
$linkCrawler = $mailCrawler->selectLink('Link name');
$client->click($linkCrawler->link());
On third line I have an exception because $linkCrawler has empty $uri field. Exception message:
InvalidArgumentException: Current URI must be an absolute URL ("").
Can anyone tell me why crawler cant get that link?
I can only tell that the $message var getBody method returns correct content.
Regards
You need to specify root crawler url. 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With