Search code examples
symfonyphpunitdomcrawler

How to check a site after redirection? DomCrawler. Functional Testing


How to properly make assertion after redirection?

$crawler = $client->submit($form);
$client->followRedirect();
//$response = $client->getResponse()->getContent();
$this->assertTrue($crawler->filter('html:contains("foo")')->count() > 0);

Debugger show that $response have content that I expect, with a foo word, but assertion failed.


Solution

  • Assign the redirect to the crawler. Try with this code:

        $crawler = $client->submit($form);
        $this->assertTrue($client->getResponse()->isRedirect(),'Submit ok');
        // Assign the redirect to the crawler 
        $crawler = $client->followRedirect();
    
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Correct redirect to page ok");
    
        $this->assertTrue($crawler->filter('html:contains("foo")')->count() > 0);
    

    Hope this help