1

In a functional test, I can use WebAssert::linkByHrefExists() to check that a link with a specific href exists on the page. And I can use WebAssert::linkExists() to check that a link with a specific link text exists on the page.

But there's no guarantee that these two will find the same link! How do I check that a link on the page has the expected href AND text?

2 Answers 2

1

Perhaps a better approach will be assert by XPath selector \Drupal\Tests\BrowserTestBase::xpath something like '//a[@href="' . $url .'"][contains(text()=\'Some text\')]' as XPath. Check this post about matching text and this about matching url.

1
  • I'd rather avoid xpath, as I find it really hard to write and read. See my answer for something I've come up with using Mink traversal.
    – joachim
    Commented Feb 6, 2020 at 11:18
0

I've done some poking around with Mink's selectors and the methods available on its element objects, and I can manage this:

$this->assertEquals('/expected/link/path', $page->findLink("Expected Link Text")->getAttribute('href'));

It's not quite as readable as the WebAssert methods though.

1
  • 1
    But since you get the link first by text, what happens if you have two links with same text on the page? But I think is a subject for another use case
    – d70rr3s
    Commented Feb 6, 2020 at 19:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.