【问题标题】:Functional testing with phpunit 6.5.1 symfony 3.4 button issuephpunit 6.5.1 symfony 3.4 按钮问题的功能测试
【发布时间】:2019-01-14 18:15:28
【问题描述】:

我正在尝试使用 phpunit 测试我的小型 Web 应用程序,但我在提交表单和使用爬虫访问按钮时遇到了困难。 我似乎无法登录,因为节点总是报告为空。我认为问题可能出在提交按钮上,可能试图引用它。

private $client = null;

 public function setUp()
 {
    $this->client = static::createClient();
    $this->testlogIn();
 }

   public function testLogIn()
{
    $crawler = $this->client->request('GET', '/login');
    $form = $crawler->filter('button#logIn')
      ->form(
            [
              '_username' => 'AdminAccountKyleTest',
              '_password' => 'test'
            ],
            'POST'
        );

    $this->client->submit($form);

    $this->assertContains('Homepage', $crawler->filter('body > div.main > div > div > div:nth-child(2) > h1')->text());
}    

这是按钮的树枝模板:

  <button class="btn btn-lg btn-primary btn-block" id="logIn" 
type="submit">Sign in</button>

当按钮呈现在树枝中而不是表单上的 SubmitType 时,phpunit 是否有问题?

【问题讨论】:

  • 我也试过 $form = $crawler->selectButton('Sign in') 但这也不起作用

标签: php symfony twig phpunit functional-testing


【解决方案1】:

使用以下 sn-p 测试您的表单:

    //selectButton accepts the button id|name|text
    $form = $crawler->selectButton("logIn")->form();

    $form['_username'] = 'AdminAccountKyleTest';
    $form['_password'] = 'test';

    $client->submit($form);

    //follow the form request redirect after login_check
    $client->followRedirect();

【讨论】:

  • 非常感谢您的评论,但是,我仍然收到此错误。 1)Tests\AppBundle\Controller\DefaultControllerTest::testLogIn InvalidArgumentException:当前节点列表为空。 /swiftcase/vendor/symfony/symfony/src/Symfony/Component/DomCrawler/Crawler.php:565 /swiftcase/tests/AppBundle/Controller/DefaultControllerTest.php:114 /swiftcase/tests/AppBundle/Controller/DefaultControllerTest.php:15
  • 我认为它似乎无法找到按钮名称或 ID。
  • 检查您的实际响应是否使用 die($client->getResponse()->getContent());在测试用例中
  • 您好对不起,基本上我需要在树枝上给按钮一个名称,而不是我从未意识到的 id,但是感谢您的帮助,非常感谢。
猜你喜欢
  • 2021-02-06
  • 2019-11-22
  • 2019-06-11
  • 2018-08-04
  • 2019-07-17
  • 2019-04-05
  • 2018-03-18
  • 1970-01-01
  • 2013-07-08
相关资源
最近更新 更多