phpsymfonyphpunitsymfony-panther

Testing Symfony validation with Panther


I'm testing my validations and I send wrong values in all my input :

$crawler = $this->client->getCrawler();

        $form = $crawler->selectButton('Créer')->form();

        $form->setValues([
            'Contractor[lastName]' => str_repeat('maxLength', self::OVER_MAX_LENGTH,),
            'Contractor[firstName]' => str_repeat('maxLength', self::OVER_MAX_LENGTH,),
            'Contractor[email]' => str_repeat('maxLength', self::OVER_MAX_LENGTH,).'@society.com',
            'Contractor[phone]' => str_repeat('0', self::UNDER_MIN_LENGTH,),
            'Contractor[password][password][first]' => 'first',
            'Contractor[password][password][second]' => 'second',
            'Contractor[status]' => 'admin.crud.user.field.choices.boss'
        ]);

        $this->client->submitForm('Créer');

        $this->client->waitFor('.invalid-feedback');
        $this->client->waitForVisibility('.invalid-feedback');

        $this->client->takeScreenshot('add.png');

        $totalErrors = $crawler->filter('div.invalid-feedback')->count();
        $errorExpected = 5;

        $this->assertNotCount($totalErrors, [$errorExpected]);

When I test I ask to wait until the errors are displayed. Then I count the number of errors and I compare. The problem is when this line is test $totalErrors = $crawler->filter('div.invalid-feedback')->count(); I've got an error which say :
Facebook\WebDriver\Exception\StaleElementReferenceException: stale element reference: element is not attached to the page document.
In the screenshot, the errors are displayed.
I really don't understand why because I asked to wait for the element to be in the DOM and I had no errors.
Any idea ?


Solution

  • It's possible that the Crawler instance you have has a "stale" HTML in its state. I don't know the exact internals, but what helped me with a similar case was to get a fresh crawler from the Client object:

    // some actions that redraws the Client HTML
    
    $crawler = $client->getCrawler();
    $totalErrors = $crawler->filter('div.invalid-feedback')->count();