【问题标题】:LogicException: The selected node does not have a form ancestor. error with symfony2 phpunit testingLogicException:所选节点没有表单祖先。 symfony2 phpunit 测试出错
【发布时间】:2014-06-25 07:56:49
【问题描述】:

当我尝试使用 PHPUnit 功能测试填写表单时,我总是收到以下错误:LogicException: The selected node does not have a form ancestor. error with symfony2 phpunit testing

这是代码,错误显示在代码的最后一行:

        $editusercrawler = $client->request('GET', '/profile');
    $this->assertTrue($editusercrawler->filter('html:contains("Edit Profile")')->count() > 0);


    // Go To Edit
    $link = $editusercrawler->filter('a:contains("Edit Profile")')->eq(0)->link();
    $editusercrawler = $client->click($link);
    //var_dump($client->getResponse()->getContent());

    // Check if User Edit was called
    $this->assertTrue($editusercrawler->filter('html:contains("Edit User")')->count() > 0);
    //var_dump($client->getResponse()->getContent());
    // Fill out form

    //var_dump($client->getResponse()->getContent());
    $editusercrawler = $client->click($link);
    $editusercrawler = $client->request('GET', '/profile/edit');
    var_dump($client->getResponse()->getContent());

    $editUserForm = $editusercrawler->selectButton('update')->form();

【问题讨论】:

  • 那么问题是有表单但是爬虫看不到?
  • 我是这么认为的,虽然我可以通过打印客户端的内容看到:var_dump($client->getResponse()->getContent());

标签: php symfony testing phpunit


【解决方案1】:

当 HTML 的结构不正确时,就会出现此问题。 setNode 方法的结构如下(我使用的是 Laravel)

protected function setNode(\DOMElement $node)
{
    $this->button = $node;
    if ('button' === $node->nodeName || ('input' === $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
        if ($node->hasAttribute('form')) {
            // if the node has the HTML5-compliant 'form' attribute, use it
            $formId = $node->getAttribute('form');
            $form = $node->ownerDocument->getElementById($formId);
            if (null === $form) {
                throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
            }
            $this->node = $form;
        return;
    }
    // we loop until we find a form ancestor
    do {
        if (null === $node = $node->parentNode) {
            throw new \LogicException('The selected node does not have a form ancestor.');
        }
    } while ('form' !== $node->nodeName);
} elseif ('form' !== $node->nodeName) {
    throw new \LogicException(sprintf('Unable to submit on a "%s" tag.', $node->nodeName));
}

$this->node = $node;

}

  • 如您所见,该方法首先检查节点名称是否匹配 按钮/输入,同时通过验证进一步输入标签 “类型”属性。
  • 接下来检查表单属性。否则抛出异常。 现在 $this->node 已经设置好了。
  • 最后的解释来了 当前您已设置 $node (您可以使用 dd() 或 var_dump() 查看输出),因为将检查下一个 parentNode。使用 do while 循环,它会搜索很长时间,直到 nodeName 不会被命名为“form”。

如开头所述,您的 HTML 结构不正确。

无效示例

<form>
<div id="first">
<input type="text" value="Wont check this">
<div/>
</form>
<div id="second">
<input type="submit" value="Working">
</div>

有效示例

<form>
<div id="first">
<input type="text" value="Wont check this">
<div/>
<div id="second">
<input type="submit" value="Working">
</div>
</form>

这就是为什么它不起作用的简单解释。就我而言,我是在引导面板主体 div 中启动表单,但在面板页脚 div 中有提交按钮。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-08
    相关资源
    最近更新 更多