是否有一种方法可以在图像上使用Symfony/Panther测试链接



thx为我提供帮助:(

IM在与Symfony/Panther的Symfony应用程序上开发了测试意甲。我正在寻找一种测试我的徽标是否重定向到正确页面的方法。我看到的方式是我必须测试链接,然后单击它以测试重定向。豹文档对链接测试的安静特定于此,请参见:

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

我还看到了如何通过domcrawler np找到图像...

所以我尝试的是将链接测试方法调整到图像,当然没有工作,因为该方法不是

所预期的字符串

因此,如果某人有想法如何测试图像链接上的重定向,那就太棒了。提前THX

<?php
namespace AppTests;
 use SymfonyComponentPantherPantherTestCase;  
 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);       
   
    }
}

运行测试显示此错误:

devtools在听ws://127.0.0.1:12947/devtools/browser/d3A0E57F-2B00-4EB3-97E3-64986CF0495EE 1/1(100%(/test1//test2//test3//test4/

时间:11.17秒,内存:38.00MB

有1个错误:

  1. app tests assertlogoisvisible ::类的测试对象Symfony component panther domcrawler 图像无法转换为字符串

在我的帖子上编辑...我找到了做到这一点的方法。

图像不是链接,链接是 <a href><button>,因此测试不在寻找图像为链接。因此,IVE尝试另一种试图通过其在页面中的位置来定位链接的方法。由于此链接是我的徽标,因此是页面上的第一个<a href>

这是我尝试过的代码,它可以正常工作!!!

namespace AppTests;
use SymfonyComponentPantherPantherTestCase;  
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());

 }
}

希望它能帮助未来的人;(

旧帖子,但它显示在Google中,所以替代方案:如果您的图像具有ID,则可以单击如下:

$this->client->getMouse()->clickTo("#yourIDHere");
$crawler = $this->client->refreshCrawler();  // reload content

最新更新