zenstruck / browser

A fluent interface for your Symfony functional tests.
MIT License
185 stars 17 forks source link

Unexpected behaviour for assertSeeIn() #112

Open benr77 opened 1 year ago

benr77 commented 1 year ago

I keep getting annoyed when using assertSeeIn() as the CSS selection often does not do what I expect.

Say I have a table in my page:

<tr>
    <td>Foo</td>
    <td>Bar</td>
</tr>

If I want to check that Bar is present, I would expect to be able to use:

$this->assertSeeIn('td', 'Bar').

This will work for Foo as it's the first td that the crawler will encounter, but to match Bar I have to use:

$this->assertSeeIn('tr td:last-child', 'Bar')

I expect assertSeeIn to find a string in ANY element that matches the $selector.

Is this the expected/desired behaviour? If so then something in the docs about it would be good, but personally I don't think it's the right behaviour - I don't want to couple my tests so tightly to the exact HTML - in this case I just want to check the Bar is shown somewhere in the table, I don't care about its exact position.

Any thoughts?

Cheers

kbond commented 1 year ago

Yes, I've been burned by this before as well. I agree with your conclusion on how it should work.

benr77 commented 1 year ago

Is this just how behat/mink normally works? I guess you can't really change this behaviour now as it would break a lot of people's existing tests...

kbond commented 1 year ago

Is this just how behat/mink normally works?

Yes

I guess you can't really change this behaviour now as it would break a lot of people's existing tests...

That's my concern. Maybe a new method?

nikophil commented 1 year ago

assertSeeInAny()?

benr77 commented 1 year ago

I was also thinking of assertSeeInAny() but I think the issue is more the underlying use of behat/mink than the method name. I don't know Mink at all - maybe it also has another method that mirrors our requirement here...

kbond commented 1 year ago

I don't recall mink having such a method but we should be able to add this behaviour manually (find all the elements via the dom crawler, get their text, then assert if the expected text is contained within)