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 navigate to the localhost/login, my form populates with the correct content. The line that says...
$form = $crawler->selectButton('login')->form();
is causing the 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';
}
}
Twig view:
{# 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>
Answer of Vineet is right but... in my opinion is not good idea to take form from button. Better try:
$crawler->filter('form[name=yourAwesomeFormName]')->form();
or change name for class,id etc. On one view can be more then 1 form with identic submit value
Started doing functional testing in symfony2 and found your question unanswered, so here we go,
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.
$form = $crawler->selectButton('submit my form please')->form();
Checkout the answer by Andrew Stackoverflow
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With