【发布时间】:2013-10-26 03:28:56
【问题描述】:
在由 CRUD 生成器生成的简单且稍加修改的功能测试中,爬虫测试错误地失败。这是通过检查断言之前创建的外部文件中的内容来确定的。并通过页面的元素检查。而通过手动运行jquery代码$("td:contains('Test')").length;那么为什么会测试失败呢?
测试类:
class ApplianceControllerTest extends WebTestCase {
private $client;
public function __construct() {
$this->client = static::createClient(array(), array(
...
));
$this->client->followRedirects();
}
public function testCompleteScenario() {
// Create a new entry in the database
$crawler = $this->client->request('GET', '/appliance/');
$crawler = $this->client->click($crawler->selectLink('Create a new entry')->link());
// Fill in the form and submit it
$form = $crawler->selectButton('Create')->form(array(
'appliance[appliance]' => 'Test',
// ... other fields to fill
));
$this->client->submit($form);
// $crawler = $this->client->followRedirect();
$content = $this->client->getResponse()->getContent();
file_put_contents('somefile', $content);
// Check data in the show view
$this->assertGreaterThan(0, $crawler->filter('td:contains("Test")')->count(), 'Missing element td:contains("Test")');
}
输出:
缺少元素 td:contains("Test") 断言 0 更大时失败 大于 0。
捕获的内容摘录:
<div class="title">Appliance</div>
<div class="width40">
<table class="record_properties">
<tbody>
<tr>
<th>Id</th>
<td>14</td>
</tr>
<tr>
<th>Appliance</th>
<td>Test</td>
</tr>
<tr>
<th>Enabled</th>
<td>Yes</td>
</tr>
</tbody>
</table>
【问题讨论】:
-
你为什么注释掉
$crawler = $this->client->followRedirect();? -
@mattexx:它被注释掉了,因为它抛出了一个错误:“LogicException:请求没有被重定向。”
-
嗯。你调查过那个错误吗?
-
@mattexx:不,主要是因为我不知道去哪里看或如何处理它。 CRUD 的东西都很稀疏。
-
提示:表示您的表单提交不成功。
标签: php symfony functional-testing