Antoine D
Antoine D

Reputation: 128

Call to undefined method assertPageTitleContains() when testing with symfony/panther

I am developing a website without any framework and I want to integrate some test using panther.

Infos

My folder structure

myproject/
├── src/
│   ├── myprojectrootfile-1.php
│   ├── ...
├── Test/
│   ├── E2eTest.php
├── vendor/
├── composer.json
├── composer.lock
├── phpunit.xml.dist

My composer.json

{
    "require-dev": {
        "phpunit/phpunit": "9",
        "symfony/panther": "^0.7.1"
    }
}

My phpunit.xml.dist

<phpunit>
<extensions>
    <extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>
<php>
    <server name="KERNEL_DIR" value="./Tests/App" />
    <server name="PANTHER_WEB_SERVER_DIR" value="./src/" />
</php>
</phpunit>

My E2eTest.php

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $client = static::createPantherClient(); // Your app is automatically started using the built-in web server
        $client->request('GET', '/src/index.php');

        // Use any PHPUnit assertion, including the ones provided by Symfony
        $this->assertPageTitleContains('My Title');
        $this->assertSelectorTextContains('#main', 'My body');
    }
}

Problem

When I run .\vendor\bin\phpunit .\Tests\E2eTest.php I have an error :

1) App\Tests\E2eTest::testMyApp Error: Call to undefined method App\Tests\E2eTest::assertPageTitleContains()

I don't understand why.

Thanks for your help

Upvotes: 1

Views: 972

Answers (2)

Daishi
Daishi

Reputation: 14289

This is what worked for me to get the title with a standalone Panther test suite (without symfony).

Packages:

  • phpunit (9.5 in my case)
  • symfony/panther (1.1 in my case)

Install the packages.

composer require phpunit/phpunit symfony/panther

Then create your tests.

<?php declare(strict_types=1);

use Symfony\Component\Panther\PantherTestCase;

final class MyTests extends PantherTestCase
{
    // @warning baseUri is already in use by Symfony\Component\Panther\PantherTestCas as a static variable
    private $myBaseUri = 'http://localhost:8000/';

    public function testWebsiteHomePage(): void
    {
        // external_base_uri prevents from exception Symfony\Component\Process\Exception\RuntimeException: The provided cwd "vendor/symfony/panther/src/../../../../public" does not exist.
        $client = static::createPantherClient(['external_base_uri' => $this->myBaseUri]);

        // load home page
        $client->request('GET', $this->myBaseUri);
        $crawler = $client->waitFor('html');

        // test home page title
        $title = $client->getTitle();
        $this->assertStringContainsString('MyAppName', $title);

        // you can also retrieve current url
        $url = $client->getCurrentURL();
        $this->assertEquals($this->myBaseUri, $url);

        // or retrieve current source code
        // but it's better practice to use the crawler to inspect the web page content
        $source = $client->getPageSource();
        $this->assertStringContainsString('html', $source);
    }
}

Start a web server with php -S localhost:8000 then start your tests with vendor/bin/phpunit tests/MyTests.php.

If you want to learn more about how to use the $client and the $crawler, read this:

Upvotes: 1

robbash
robbash

Reputation: 1093

I know it's a bit later but just had the same issue while trying Panther after the recent release of 1.0. I'm using it on an app using Slim + JS SPA frontend.

  1. Install symfony/framework-bundle as a dev dependency, needs to be 4.4+.
composer require --dev symfony/framework-bundle
  1. Add the following methods to your test case or put them into a base test case class
<?php

namespace Tests;

use Symfony\Component\Panther\PantherTestCase;

abstract class BasePantherTestCase extends PantherTestCase {

  public static function createKernel(array $options = [])
  {
    return null;
  }

  public static function bootKernel(array $options = [])
  {
  }

}
  1. Set the base uri when creating the Panther client
$client = static::createPantherClient([
  'external_base_uri' => 'http://url-to-your-test-env:port'
]);

Hope it helps ;)

Upvotes: 0

Related Questions