Symfony Panther is a browser testing and web scraping library for PHP that leverages the WebDriver protocol. It allows you to write tests that perform browser automation to navigate through a multi-language website, interact with it, and assert its behavior.
Testing a multi-language website involves:
- Navigating to the page that offers language selection.
- Changing the language (by clicking a link, selecting from a dropdown, etc.).
- Asserting that the content is displayed in the selected language.
Here's a step-by-step guide to using Symfony Panther to test a multi-language website:
- Install Symfony Panther: If you haven't already installed Symfony Panther, you can do so using Composer:
composer require --dev symfony/panther
Create a Test Case: Create a PHP file for your test case, extending from
Symfony\Component\Panther\PantherTestCase
.Write the Test: Within the test, use Panther's client to navigate and interact with your website.
Here's a basic example to illustrate the process:
<?php
namespace Tests;
use Symfony\Component\Panther\PantherTestCase;
class MultiLanguageTest extends PantherTestCase
{
public function testChangeLanguage(): void
{
// Start the browser and navigate to your website
$client = static::createPantherClient();
$client->request('GET', 'http://your-website.com');
// Find the element that changes the language (e.g., a link or a dropdown)
// In this example, we're assuming the language toggle is a link with an id "language-switch"
$crawler = $client->clickLink('Deutsch'); // Example for changing to German
// Wait for the page to load or for the language switch to occur (if necessary)
// $client->waitFor('.some-element-that-appears-after-language-change');
// Assert the content is in German
// This assertion will depend on the content of your website
// For example, you might check for the presence of a specific text string
$this->assertContains('Willkommen', $crawler->filter('body')->text());
// You can also take a screenshot to visually confirm the result
$client->takeScreenshot('screenshot.png');
}
}
- Run the Test: Execute the test using PHPUnit.
./vendor/bin/phpunit tests/MultiLanguageTest.php
Things to Consider:
- The way you switch languages will depend on how your application implements this feature.
- You may need to use the
waitFor
method to wait for AJAX requests to complete if the language switch involves asynchronous operations. - If your application uses cookies or sessions to remember the selected language, you may need to test that the language preference persists across different pages or visits.
- Make sure your test environment has a web server running and that it can serve your Symfony application.
- Depending on the complexity of your application, you might need to log in or perform other actions to reach the language switch.
Please note that the above code is a simple example and may need to be adapted to fit the specific structure and requirements of your multi-language website.