gpt4 book ai didi

php - Web测试用例: Use crawler to test a string

转载 作者:行者123 更新时间:2023-11-28 20:57:13 25 4
gpt4 key购买 nike

有没有办法在字符串上使用 WebTestCase 的 Crawler?通常,如果我想使用 WebTestCase 进行测试,我会使用客户端执行如下操作:

public function testInitialPage()
{
$client = $this->createClient();
$crawler = $client->request('GET', '/');

$this->assertCount(1, $crawler->filter('h1:contains("Contact us")'));
...
}

现在,我想知道,是否有可能以某种方式在字符串上使用爬虫,所以它会像下面这样:

public function testInitialPage()
{
...
$crawler = Crawler::createCrawler("<h1>Contact us</h1>");
$this->assertCount(1, $crawler->filter('h1:contains("Contact us")'));
...
}

谢谢!

最佳答案

如果您从 DomCrawler 中导入 Crawler 类组件,您可以在测试中使用它。

namespace Acme\Tests;

//...
use Symfony\Component\DomCrawler\Crawler;

class ContactTest extends WebTestCase
{
public function testHeadlineOnContactUs()
{
$crawler = new Crawler("<h1>Contact us</h1>");
$this->assertCount(1, $crawler->filter('h1:contains("Contact us")'));
}
}

关于php - Web测试用例: Use crawler to test a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11312621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com