有没有办法在字符串上使用WebTestCase的爬虫?通常,如果我想使用 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")'));
}
}