Search code examples
symfonyfunctional-testingdomcrawlersymfony3.x

How can I find a link by URL in my functional test?


In the Symfony testing documentation, it shows how to select a link containing specific text:

$crawler->selectLink('Click here');

But the link I'm looking for won't always have the same text. It will say one of a few different things depending on the status of the record, so instead, I need to find it by URL. I think I can figure out how to do it using an xpath filter. Is that what I should go for, or is there a better way?


Solution

  • You would be testing against a known state - and so that state will inform what text should be on the button.

    If not - you can also search by a CSS ID, or enough other information to uniquely identify the button, such as a class name of a button within a range specified by an ID, with a CSS Path, or XPath.

    Full CSS Path to the `<code>` block in your question
    #question > table > tbody > tr:nth-child(1) > td.postcell > div > div.post-text > pre > code
    
    Xpath
    //*[@id="question"]/table/tbody/tr[1]/td[2]/div/div[1]/pre/code
    

    A specific ID, or the text to look for would be a lot easier though, and far less likely to break!