【问题标题】:WebTestCase: Use crawler to test a stringWebTestCase:使用爬虫测试字符串
【发布时间】:2012-07-03 14:02:44
【问题描述】:

有没有办法在字符串上使用 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")'));
    ...
}

谢谢!

【问题讨论】:

    标签: php testing symfony functional-testing web-crawler


    【解决方案1】:

    如果您从 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")'));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多