[英]Symfony2 functional test - check table contents
我在斷言中只使用過contains()
這樣的東西,所以我不確定如何處理像這樣復雜的東西。
假設我有一系列預期答案 - 在這種情況下,它是YES,YES,NO。
所以這意味着有效的,對於第一個和第二個問題,我希望在第三個<td>
內看到<span class="glyphicon glyphicon-ok"></span>
<td>
,對於我希望看到的第三個問題在第四個<td>
。
這是我的HTML代碼:
<table class="table table-curved">
<tr>
<th width="10%">Item</th>
<th width="60%">Description</th>
<th width="10%">YES</th>
<th width="10%">NO</th>
<th width="10%">NOT APPLICABLE</th>
</tr>
<tr>
<td class="report-table-inner report-centre">1</td>
<td class="report-table-inner">Check cargo is secure and undamaged.</td>
<td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
<td class="report-centre"></td>
</tr>
<tr>
<td class="report-table-inner report-centre">2</td>
<td class="report-table-inner">Is all cargo accounted for.</td>
<td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
<td class="report-centre"></td>
</tr>
<tr>
<td class="report-table-inner report-centre">3</td>
<td class="report-table-inner">Is all cargo checked by customs.</td>
<td class="report-centre"></td>
<td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
</tr>
...
我該如何為此編寫測試? 以編程方式迭代<tr>
是難的嗎?
謝謝
參考文獻:
<?php
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class PageTest extends WebTestCase
{
public function testPage()
{
// create a client to get the content of the page
$client = static::createClient();
$crawler = $client->request('GET', '/page');
// retrieve table rows
$rows = $crawler->filter('.table-curved tr');
$statesColumnIndex = array(
// 0 indexed
'ok' => 2,
'ko' => 3,
'na' => 4,
);
$expectedValues = array(
// 0 indexed, row index => [$values]
1 => ['identifier' => 1, 'state' => 'ok'],
2 => ['identifier' => 2, 'state' => 'ok'],
3 => ['identifier' => 3, 'state' => 'ko'],
);
foreach ($expectedValues as $rowIndex => $values) {
// retrieve columns for row
$columns = $rows->eq($rowIndex)->filter('td');
// check item identifier
$identifierColumn = $columns->eq(0);
$this->assertEquals(
(string) $values['identifier'],
trim($identifierColumn->text())
);
// check state
$stateColumn = $columns->eq($statesColumnIndex[$values['state']]);
$this->assertEquals(1, $stateColumn->filter('.glyphicon-ok')->count());
}
}
}
請注意,我根本不是Symfony,但這是一個使用純PHP DOM的答案; 它需要$ values作為一個數組,其中包含'pass'(跳過此<tr>
)或者哪個列應該包含glyphicon-ok
類的索引:
<?php
$data = <<<DATA
<table class="table table-curved">
<tr>
<th width="10%">Item</th>
<th width="60%">Description</th>
<th width="10%">YES</th>
<th width="10%">NO</th>
<th width="10%">NOT APPLICABLE</th>
</tr>
<tr>
<td class="report-table-inner report-centre">1</td>
<td class="report-table-inner">Check cargo is secure and undamaged.</td>
<td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
<td class="report-centre"></td>
</tr>
<tr>
<td class="report-table-inner report-centre">2</td>
<td class="report-table-inner">Is all cargo accounted for.</td>
<td class="report-centre success"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
<td class="report-centre"></td>
</tr>
<tr>
<td class="report-table-inner report-centre">3</td>
<td class="report-table-inner">Is all cargo checked by customs.</td>
<td class="report-centre"></td>
<td class="report-centre danger"><span class="glyphicon glyphicon-ok"></span></td>
<td class="report-centre"></td>
</tr>
</table>
DATA;
$dom = new DOMDocument();
$dom->loadXML($data);
$xpath = new DOMXPath($dom);
$values = ['pass', 2, 2, 3];
$idx = 0;
foreach($xpath->query('//tr') as $tr) {
if ($values[$idx] != 'pass') {
$tds = $tr->getElementsByTagName('td');
$td = $tds->item($values[$idx]);
if ($td instanceof DOMNode && $td->hasChildNodes()) {
if (FALSE !== strpos($td->firstChild->getAttribute('class'), 'glyphicon-ok')) {
echo "Matched on ", $tds->item(1)->textContent, "\n";
} else {
echo "Not matched on ", $tds->item(1)->textContent, "\n";
}
}
}
++$idx;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.