简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM