Symfony2 functional testing InvalidArgumentException: The current node list is empty

24,088

Solution 1

You can debug the problem by using var_dump($client->getResponse()->getContent());

Additionally, I think you should write this:

$crawler = $client->submit($form);

Otherwise you'll be testing the response of the first url, before form submission.

Solution 2

I was also struggling with this, and It appeared that the selectButton method triggered this error.

After reading up on the DOM Crawler docs I found that the selectButton method takes the actual button text as a string argument. So if your button is 'submit my form please', that will be your text.

It does take different parameters too, as shown below (taken from the docs)

A selectButton() method is available on the Crawler which returns another 
Crawler that matches a button (input[type=submit], input[type=image], 
or a button) with the given text.

EDIT

After finally successfully completing the test I would also recommend you follow this example for testing forms:

use Goutte\Client;

$client = new Client();
$crawler = $client->request('GET', 'https://github.com/login');

$form = $crawler->selectButton('Log in')->form();
$form['login'] = 'symfonyfan';
$form['password'] = 'anypass';

$crawler = $client->submit($form);
$this->assertTrue($crawler->filter('html:contains("Welcome Back")')->count() > 0);

The main difference being, I have used the Goutte bundle, which I installed with composer from the packagist (in my case I added "fabpot/goutte": "1.0.*@dev")

Solution 3

As a follow up to what @greg0ire wrote, check to see if

var_dump($client->getResponse()->getContent());

Returns a redirect page instead of the actual content. If so, you can add this:

$client->followRedirects(true);

Solution 4

I see question still dont have answer. I had the same problem.

In my case goutte was not able to do this request because input name is changed by javascript on the fly.

When goutte received html it saw one form. And when submitting with pre filled params, form input elements could not be matched by $form->setValues($params) so \InvalidArgumentException was thrown.

Solved by doing request by hand.

// $form->setValues($data);
// $this->getGoutte()->submit($form);

$data = array(
    'input_name[key]' => 'value'
);
$this->getGoutte()->request($form->getMethod(), $form->getUri(), $params);

Solution 5

I had the same problem with Silex application. I was looking for

$buttonCrawler = $crawler->selectButton('input[type="submit"]');

Instead, the correct way to do it is give the value of the button

$buttonCrawler = $crawler->selectButton('value_of_the_button');


For example, in your page:

<form>
    ...
    <input type="submit" value="Click Me">
</form>


And in your tests:

$buttonCrawler = $crawler->selectButton('Click Me');
$form = $buttonCrawler->form();
...
Share:
24,088
VitalyP
Author by

VitalyP

Updated on March 13, 2020

Comments

  • VitalyP
    VitalyP about 4 years

    I get "InvalidArgumentException: The current node list is empty." running functional tests through PHPUnit. Here is test i wrote:

    public function testAdd()
    {
        $client = static::createClientWithAuthentication('main');
    
        $crawler = $client->request('GET', 'en/manage');
    
        $send_button = $crawler->selectButton('submit');
    
        $form = $send_button->form(array(
            'PrCompany[email]' => '[email protected]',
            'PrCompany[first_name]' => 'Anton',
            'PrCompany[last_name]' => 'Tverdiuh',
            'PrCompany[timezone]' => 'Europe/Amsterdam'
        ));
    
        $form['PrCompany[companies][1]']->tick();
    
        $client->submit($form);
    
    
        $this->assertTrue($crawler->filter('html:contains("User is invited")')->count() > 0);
    
    }