Question

I have this functional test (I'm just showing a fragment):

namespace Just\An\ExampleBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ExampleControllerTest extends WebTestCase
{
  public function testCrearTipus()
  {
    $client = static::createClient(array(), array(
            'PHP_AUTH_USER' => 'login',
            'PHP_AUTH_PW' => 'pa$$sword'
    ));
    $client->followRedirects();
    $url = '/admin/type/new';
    $crawler = $client->request('GET', $url);
    $token = $crawler->filter('input[name="name_of_form[_token]"]')->first()->getValue();
    .....
  }
}

I just cannot get the value of the input defined in my view as:

Any help would be appreciated

Was it helpful?

Solution 2

If you have a button in your form you can try this instead.

    $crawler = $client->request('GET', $url);
    $buttonCrawlerNode = $crawler->selectButton('Save');

    $form = $buttonCrawlerNode->form();

    $token = $form->get('name_of_form[_token]')->getValue()

where Save is the text you have in your button

OTHER TIPS

If you want to use name selector,

   $appClient        = static::createClient();
   $requestCrawler   = $appClient->request('GET', $url;
   $tokenExtract     = $requestCrawler->filter('input[name="name_of_form[_token]"]')
                                    ->extract(array('value'));
   $csrfToken        = $tokenExtract[0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top