I'm trying to show a custom message (the URL being tested) when a PHPUnit 9.5.11 test fails. in my Symfony 4.4 app.
The class is simple:
class BaseTestCase extends PantherTestCase
In my test, I've:
$client = static::createPantherClient();
$crawler = $client->request('GET', $url);
$this->assertSelectorExists('.some-class', $url); // <- this should display the tested $url, since the second argument is supposed to be the message to show on failure
But when the test fails, all I get is:
- App\Tests\PropertyListingTest::testListingWithFullQueryString with data set #0 ('') Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".some-class"} (Session info: headless chrome=97.0.4692.71)
What's going on here? If I run this:
$this->assertEquals("a", "b", "!!!!TEST FAILED!!!!");
It works as expected:
!!!!TEST FAILED!!!! Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'a' +'b'
That behaviour actually looks like a bug to me.
Because if you look at what
assertSelectorExistsdoes internally (see below)......you'll notice that it expects
findElementto return null in case if no element was found. But the problem is thatfindElementdoesn't return null in that case, it throws an exception instead.To fix this, I would make my own version of assertSelectorExists like this: