2

I'm doing functional testing and I am getting the error

InvalidArgumentException: The current node list is empty

This is my code

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')
    );
}

I think it is getting error in this :

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

Note that $requestContent values are coming from a hidden input type which are all inside the form tag.

Matteo
  • 37,680
  • 11
  • 100
  • 115
momori14
  • 169
  • 1
  • 2
  • 14

2 Answers2

1

You have to provide the text of the button to $crawler->selectButton().

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

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

Side note, your regex pattern '/\/nextPage' is invalid. You need to add a closing / --> '/\/nextPage/' // This will match the exact string: "/nextPage"

rhinosforhire
  • 1,305
  • 11
  • 20
1

You have not posted your HTML so now i assumed your HTML such as :

<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');

to change

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

Because selectButton() accept the button value instead of id or name.

Make sure this is a helpful to you.

Kishan Patel
  • 485
  • 4
  • 17