Symfony2:在功能测试中获取输入值 [英] Symfony2: Get input value in functional testing

查看:27
本文介绍了Symfony2:在功能测试中获取输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能测试(我只是展示一个片段):

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:

任何帮助将不胜感激

推荐答案

如果您的表单中有按钮,您可以试试这个.

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

这篇关于Symfony2:在功能测试中获取输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆