imagehyperlinkfunctional-testingsymfony-panther

Is there a way to test a link on a image with Symfony/Panther


thx for helping me on this :)

Im developping a serie of test on a symfony app with Symfony/Panther. Im looking for a way to test if my logo redirect to the correct page. The way I see it, is I have to test the link and then click on it to test the redirection. The panther documentation is quiet specific about link testing see here:

https://symfony.com/blog/introducing-symfony-panther-a-browser-testing-and-web-scraping-library-for-php

I also saw how to find an image via DomCrawler np with that...

SO what I have tried, is to adapt the link testing method to an image, of course it didnt work cause the image is not a string as expected by the method

So if someone have an idea how to test the redirection on a image link It ll be awesome. Thx in advance

<?php
namespace App\Tests;

 use Symfony\Component\Panther\PantherTestCase;  

 class assertLogoRedirectTo extends PantherTestCase   
{
    public function test()
    {
    
        $client = static::createPantherClient();
        $crawler = $client->request('GET','https://my.sibluconnect.com');        
    
        $client->waitFor('.login');        
        $image = $crawler->selectImage('siblu')->image(); 
        $link = $crawler->selectLink($image)->link();        
        $crawler = $client->click($link);       
   
    }
}

Running the test shows this error:

DevTools listening on ws://127.0.0.1:12947/devtools/browser/d3a0e57f-2b00-4eb3-97e3-64986cf0495e E 1 / 1 (100%)/test1//test2//test3//test4/

Time: 11.17 seconds, Memory: 38.00MB

There was 1 error:

  1. App\Tests\assertLogoIsvisible::test Object of class Symfony\Component\Panther\DomCrawler\Image could not be converted to string

Solution

  • Edit on my post...I found the way to do it.

    A image is not a link, a link is a <a href> or <button> so the test shoudnt be looking for a image to be a link. So Ive try another method which is trying to target the link via its position in the page. As this link is my logo it is the first <a href> on the page.

    Here's the code Ive tried and it works just fine !!!

    namespace App\Tests;
    
    use Symfony\Component\Panther\PantherTestCase;  
    
    class assertLogoRedirectTo extends PantherTestCase   // Success
    {
    public function test()
    {
    
        echo'/test1/';
        $client = static::createPantherClient();
        $crawler = $client->request('GET', 'https://my.sibluconnect.com/fr/');       
    
        $link = $crawler->filter('a')->eq(0)->attr('href');
        $crawler = $client->request('GET',$link);
        $this->assertSame('http://sibluconnect.com/', $client->getCurrentURL());
    
    
     }
    }
    

    Hope it helps someone in the futur ;)