Symfony Crawler: how to check that a link to a particular page exists

3.6k views Asked by At

I'm writing some functional tests and i want to verify that the Edit link exists on the page if the user is logged in.

The link is a simple <a href="/profile/22/edit">Edit</a>.

How can I filter it using the Crawler component of Symfony?

One solution is this:

$this->assertEquals(1, $crawler->filter('html:contains("<a href="/profile/22/edit">")')->count());

But I'd like to use, instead, the tag selection, so how can I do this?

1

There are 1 answers

0
redbirdo On BEST ANSWER

You can use Crawler::filterXPath() to check for the presence or even absence of html elements matching all sorts of criteria. To check for the presence of a link I prefer to use the element id as that is most likely to remain constant. For example, if you modify your link as follows:

<a id="edit-profile-link" href="/profile/22/edit">Edit</a>

Then you can check the link exists like this:

$node = $crawler->filterXPath('//a[@id="edit-profile-link"]');
$this->assertTrue($node->count() == 1), "Edit profile link exists");

Here are some good examples of the sort of filters you can use with XPath.