Symfony2 functional testing InvalidArgumentException: The current node list is empty

前端 未结 7 540
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 23:41

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

public function testAdd()         


        
相关标签:
7条回答
  • 2020-12-19 00:19

    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);
    
    0 讨论(0)
  • 2020-12-19 00:22

    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();
    ...
    
    0 讨论(0)
  • 2020-12-19 00:27

    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);
    
    0 讨论(0)
  • 2020-12-19 00:31

    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.

    0 讨论(0)
  • 2020-12-19 00:31

    You can try to use Codeception with Symfony2 module. It provides flexible interface to Symfony2 functional tests and has better debugging features.

    0 讨论(0)
  • 2020-12-19 00:40

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

    0 讨论(0)
提交回复
热议问题