dongyan5815 2014-06-25 07:56
浏览 56
已采纳

too long

I always get the following error when I try to fill out a form with PHPUnit Functional Testing: LogicException: The selected node does not have a form ancestor. error with symfony2 phpunit testing

Here is the code, the error shows up at the last line of code:

        $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();
  • 写回答

1条回答 默认 最新

  • dpea85385 2016-05-27 13:26
    关注

    This problem appears, when HTML isn't structured properly. The setNode method has the following structure (I am using 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;
    

    }

    • As you see, the method first checks if node name matches button/input, while going further for input tag by verifying the 'type' attribute.
    • Next it checks for the form attribute. Otherwise it throws exception. Now $this->node is set.
    • Here comes the final explanation Currently you have $node (you can use dd() or var_dump() to see output) set, as next parentNode will be checked. With a do while loop it searches so long, until the nodeName wont be named "form".

    As said in the beginning, your HTML isn't structured properly.

    INVALID EXAMPLE

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

    VALID EXAMPLE

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

    Thats a simple explanation why it wont work. In my case, I was starting form inside the bootstrap panel-body div, but had submit button in panel-footer div.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀