我有一个没有操作的表单(用 javascript 提交),我试图为其编写一个单元测试,但它失败了,因为缺少“action”属性:
InvalidArgumentException :当前 URI 必须是绝对 URL ("")。
有没有办法在单元测试中添加它或使用爬虫修改html内容?
<form id="form_search_page">
<input type="text" name="keyword" value="" />
<button type="submit" name="searchBtn" id="searchBtn">Search</button>
</form>
$client = $this->makeClient(true);
$url = $this->createRoute("page_index"));
$crawler = $client->request('GET', $url);
$response = $client->getResponse();
$form = $crawler->filter('#form_search_page')->form();
$params = array(
"form[text]" => "dummy title"
);
$form->setValues($params);
$crawler = $client->submit($form);
$response = $client->getResponse();
$this->assertGreaterThan(0, $crawler->filter('.pages li')->count());
我找到了解决方案:
$crawler
->filter('form#form_search_page')
->reduce(function (Crawler $form) use ($router) {
$url = $router->generate('search_page', array(), true);
$node = $form->getNode(0);
if (!$node->hasAttribute('action')){
$node->setAttribute('action', $url);
$node->setAttribute('method', 'POST');
return true;
}
return false;
})
->first();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)