Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 functional testing InvalidArgumentException: The current node list is empty

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

}
like image 969
VitalyP Avatar asked Feb 27 '12 17:02

VitalyP


5 Answers

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")

like image 137
Andrew Atkinson Avatar answered Nov 14 '22 11:11

Andrew Atkinson


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();
...
like image 32
Nicole Bartolini Avatar answered Sep 19 '22 15:09

Nicole Bartolini


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.

like image 13
greg0ire Avatar answered Nov 14 '22 11:11

greg0ire


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);
like image 2
Matt Avatar answered Nov 14 '22 12:11

Matt


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);
like image 1
wormhit Avatar answered Nov 14 '22 11:11

wormhit