Error trying to select form from symfony2 crawler?

I keep getting the following error:

There was 1 error: 1) Caremonk\MainSiteBundle\Tests\Controller\SecurityControllerFunctionalTest::testIndex InvalidArgumentException: The current node list is empty. 

But when I go to localhost / login, my form is populated with the right content. A line that says ...

 $form = $crawler->selectButton('login')->form(); 

causes an error. What is wrong with my test?

Functional Test:

 <?php namespace Caremonk\MainSiteBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class SecurityControllerFunctionalTest extends WebTestCase { public function testIndex() { $client = static::createClient(); $crawler = $client->request('GET', '/login'); $form = $crawler->selectButton('login')->form(); $form['username'] = 'testActive'; $form['password'] = 'passwordActive'; } } 

View Twig:

 {# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #} {% if error %} <div>{{ error.message }}</div> {% endif %} <form action="{{ path('caremonk_mainsite_login_check') }}" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="_username" value="{{ last_username }}" /> <label for="password">Password:</label> <input type="password" id="password" name="_password" /> {# If you want to control the URL the user is redirected to on success (more details below) <input type="hidden" name="_target_path" value="/account" /> #} <button type="submit">login</button> </form> 
+6
source share
4 answers

I started doing functional testing in symfony2 and found your question unanswered, so here we go,

The selectButton method accepts the actual button text as a string argument. Therefore, if your button "send my form please", it will be your text.

 $form = $crawler->selectButton('submit my form please')->form(); 

Check out Andrew's answer fooobar.com/questions/589232 / ...

+7
source

Vineet's answer is right, but ... in my opinion, it is not a good idea to take the form with a button. Better try:

 $crawler->filter('form[name=yourAwesomeFormName]')->form(); 

or change the name for the class, id, etc. There can be more than 1 form on one submission with an identifying value

+16
source

selectButton() selects the name or alt button for images.

0
source

Well, that’s why I had this error and how I solved it.

(Using Goutte ) The scanner works like a browser with GET.

So when you do:

 $client = new Client(); $crawler = $client->request('GET', '/login'); 

The crawler tries to reach http://localhost/login

So basically, I changed it to:

 $client = new Client(); $crawler = $client->request('GET', 'http://site.dev/app_dev.php/login'); 

site.dev is my virtual host for my symfony project.

Hope this helps!

0
source

Source: https://habr.com/ru/post/949353/


All Articles