2

我正在做功能测试,但我收到了错误

InvalidArgumentException:当前节点列表为空

这是我的代码

public function testThis(){
    $requestContent = [
        'val1' => '324343',
        'valname' => '"Benjamin"',
        'valLast' => '"A"',
        'valnum' => '44343',
        'hndval1' => '0000',
        'hdnval2' => '0000',
        'hndval3' => '1111',
        'hndref' => '"ThisIsAtest"',
        'hdnMessage' => '"I am a message"'

    ];

    $crawler = $this->client->request('GET', '/');
    $submitButton = $crawler->selectButton('btnSubmit');
    $form = $submitButton->form($requestContent);

    print_r($form);
    $this->client->followRedirects(true);
    $crawler = $this->client->submit($form);

    $response = $this->client->getResponse();
    $request = $this->client->getRequest();
    print_r($crawler->html());
    $this->assertRegExp('/\/nextPage', $request->getUri());
    print_r($request->getUri());
    $a = $crawler->filter('input[name="pageName"]');

    $this->assertContains(
        "True",
        $a->attr('value')
    );
}

我认为这是错误的:

$form = $submitButton->form($requestContent);

请注意,$requestContent 值来自隐藏的输入类型,它们都在表单标签内。

4

2 回答 2

1

您必须将按钮的文本提供给$crawler->selectButton().

// For a given HTML button:
// <input type="button" value="Upload File" />
// or
// <button type="button">Upload File</button>

$crawler->selectButton("Upload File");

旁注,您的正则表达式模式'/\/nextPage'无效。您需要添加一个结束/->'/\/nextPage/' // This will match the exact string: "/nextPage"

于 2016-11-08T07:03:40.923 回答
1

您还没有发布您的 HTML,所以现在我假设您的 HTML 如下:

<html>
    <form method='GET' action='your action here'>

        /*
        * all other html here
        */

        <input type='submit' value='Submit' id='btnSubmit' name='btnSubmit'>

    </form>

</html>

$submitButton = $crawler->selectButton('btnSubmit');

改变

$submitButton = $crawler->selectButton('Submit');

因为 selectButton() 接受按钮值而不是 id 或 name。

确保这对您有帮助。

于 2016-11-09T05:24:22.240 回答