我只想对下面的HTML数据运行一些断言。在$this->assertEquals(1, ..., "failure");
中,我尝试过:
$crawler->filter('table.table-general > tr > td:contains("<strong>Task ID</strong> 6")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Customer</strong> ABC Inc.")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Location</strong> New York City")')
$crawler->filter('table.table-general > tr > td:contains("<strong>Phone Number</strong> 555-1234")')
但不幸的是它不起作用(断言失败)。我做错了什么?
<table class="table table-curved table-general">
<tr>
<td rowspan="5" width="10%" id="general-task-id"><strong>Task ID</strong><br /><h3>6</h3></td>
<td width="30%"><strong>Customer</strong> ABC Inc.</td>
<td width="30%"><strong>Location</strong> New York City</td>
<td width="30%"><strong>Phone Number</strong> 555-1234</td>
</tr>
...
过滤器方法使用CssSelector,并且包含基于节点的xpath选择,因此不可能检查内部的其他html标记。下面是您的示例的工作解决方案:
$i = $crawler->filter('table.table-general > tr > td > strong:contains("Task ID")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td > h3:contains("6")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td > strong:contains("Customer")')->count();
$this->assertEquals(1, $i);
$i = $crawler->filter('table.table-general > tr > td:contains("ABC Inc.")')->count();
$this->assertEquals(1, $i);
希望对您有所帮助
您的第一个断言查找<strong>Task ID</strong> 6
,但实际上您的HTML显示为<strong>Task ID</strong><br /><h3>6</h3>