Symfony2 功能测试 - 检查 table 内容
Symfony2 functional test - check table contents
我只在断言中使用过 contains()
之类的东西,所以我不确定如何处理像这样复杂的东西。
假设我有一系列预期答案 - 在本例中是“是”、“是”、“否”。
所以这意味着有效,对于第一个和第二个问题,我希望在第三个 <td>
中看到 <span class="glyphicon glyphicon-ok"></span>
,对于第三个问题,我希望在第四个中看到它<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>
很难吗?
谢谢
我认为您应该查看有关测试和 DomCrawler 组件的文档页面:
有非常简单的方法可以过滤html或xml内容。
请注意,我根本不使用 Symfony,但这是一个使用纯 PHP DOM 的答案;它需要 $values 作为一个数组,其中包含 'pass'(跳过此 <tr>
)或该列的索引应包含 glyphicon-ok
class:
<?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;
}
参考资料:
- http://symfony.com/doc/current/book/testing.html#your-first-functional-test
- http://symfony.com/doc/current/components/dom_crawler.html#node-traversing
<?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());
}
}
}
我只在断言中使用过 contains()
之类的东西,所以我不确定如何处理像这样复杂的东西。
假设我有一系列预期答案 - 在本例中是“是”、“是”、“否”。
所以这意味着有效,对于第一个和第二个问题,我希望在第三个 <td>
中看到 <span class="glyphicon glyphicon-ok"></span>
,对于第三个问题,我希望在第四个中看到它<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>
很难吗?
谢谢
我认为您应该查看有关测试和 DomCrawler 组件的文档页面:
有非常简单的方法可以过滤html或xml内容。
请注意,我根本不使用 Symfony,但这是一个使用纯 PHP DOM 的答案;它需要 $values 作为一个数组,其中包含 'pass'(跳过此 <tr>
)或该列的索引应包含 glyphicon-ok
class:
<?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;
}
参考资料:
- http://symfony.com/doc/current/book/testing.html#your-first-functional-test
- http://symfony.com/doc/current/components/dom_crawler.html#node-traversing
<?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());
}
}
}