Yes, you can perform both form submissions and file uploads using Symfony Panther. Symfony Panther is a powerful PHP library that leverages the WebDriver protocol to control real browsers like Chrome and Firefox, making it ideal for automated form interactions and file handling.
Form Submissions
Basic Form Submission
The most straightforward way to submit forms is using the form()
method to populate fields and submit()
to send the data:
use Symfony\Component\Panther\PantherTestCase;
class FormSubmissionTest extends PantherTestCase
{
public function testLoginForm()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/login');
// Fill and submit form in one step
$form = $crawler->selectButton('Login')->form([
'username' => 'john_doe',
'password' => 'secure_password',
'remember_me' => true // Checkboxes accept boolean values
]);
$client->submit($form);
// Verify successful login
$this->assertStringContainsString('Dashboard', $client->getTitle());
}
}
Advanced Form Handling
For more complex forms, you can fill fields individually:
public function testContactForm()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/contact');
$form = $crawler->selectButton('Send Message')->form();
// Fill individual fields
$form['name'] = 'Jane Smith';
$form['email'] = 'jane@example.com';
$form['subject'] = 'Product Inquiry';
$form['message'] = 'I would like more information about your services.';
// Handle select dropdowns
$form['category']->select('sales');
$client->submit($form);
// Wait for success message
$client->waitFor('.success-message');
}
Dynamic Forms with JavaScript
For forms that require JavaScript interaction:
public function testDynamicForm()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/dynamic-form');
// Fill form fields using direct element interaction
$client->findElement(WebDriverBy::id('username'))->sendKeys('user123');
$client->findElement(WebDriverBy::id('password'))->sendKeys('pass456');
// Click submit button (useful for forms with JavaScript validation)
$client->findElement(WebDriverBy::cssSelector('button[type="submit"]'))->click();
// Wait for page to load
$client->waitFor('.dashboard');
}
File Uploads
Single File Upload
Handle file uploads by setting the file path on file input elements:
public function testSingleFileUpload()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/upload');
// Create a temporary test file
$tempFile = tempnam(sys_get_temp_dir(), 'test_upload_');
file_put_contents($tempFile, 'Test file content for upload');
$form = $crawler->selectButton('Upload File')->form();
// Set file path for upload
$form['file']->upload($tempFile);
$client->submit($form);
// Verify upload success
$this->assertStringContainsString('Upload successful', $client->getPageSource());
// Clean up
unlink($tempFile);
}
Multiple File Upload
For forms accepting multiple files:
public function testMultipleFileUpload()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/multi-upload');
// Create test files
$files = [];
for ($i = 1; $i <= 3; $i++) {
$tempFile = tempnam(sys_get_temp_dir(), "test_file_{$i}_");
file_put_contents($tempFile, "Content for file {$i}");
$files[] = $tempFile;
}
$form = $crawler->selectButton('Upload Files')->form();
// Upload multiple files
foreach ($files as $index => $file) {
$form["files[{$index}]"]->upload($file);
}
$client->submit($form);
// Verify all files uploaded
$this->assertStringContainsString('3 files uploaded', $client->getPageSource());
// Clean up
array_map('unlink', $files);
}
File Upload with Additional Form Data
Combine file uploads with other form fields:
public function testFileUploadWithMetadata()
{
$client = static::createPantherClient();
$crawler = $client->request('GET', 'https://example.com/document-upload');
$testFile = tempnam(sys_get_temp_dir(), 'document_');
file_put_contents($testFile, 'Important document content');
$form = $crawler->selectButton('Submit Document')->form([
'title' => 'Project Proposal',
'description' => 'Q4 business proposal document',
'category' => 'business',
'public' => false
]);
// Add file to the existing form data
$form['document']->upload($testFile);
$client->submit($form);
$this->assertStringContainsString('Document uploaded successfully', $client->getPageSource());
unlink($testFile);
}
Error Handling and Best Practices
Robust Form Submission with Error Handling
public function testFormSubmissionWithErrorHandling()
{
$client = static::createPantherClient();
try {
$crawler = $client->request('GET', 'https://example.com/form');
// Wait for form to load
$client->waitFor('form[name="contact"]', 10);
$form = $crawler->filter('form[name="contact"]')->form([
'name' => 'Test User',
'email' => 'test@example.com'
]);
$client->submit($form);
// Wait for either success or error message
$client->waitForAnyElement(['.success', '.error'], 10);
// Check result
if ($client->findElements(WebDriverBy::cssSelector('.success'))) {
$this->assertTrue(true, 'Form submitted successfully');
} else {
$errorMessage = $client->findElement(WebDriverBy::cssSelector('.error'))->getText();
$this->fail("Form submission failed: {$errorMessage}");
}
} catch (Exception $e) {
$this->fail("Form submission error: " . $e->getMessage());
}
}
Tips for Reliable Form Automation
- Wait for elements: Use
waitFor()
to ensure forms are fully loaded - Handle validation: Account for client-side validation that might prevent submission
- Use absolute file paths: Ensure file paths are accessible to the browser
- Clean up test files: Always delete temporary files after tests
- Verify results: Check for success/error messages after form submission
Prerequisites
- WebDriver: ChromeDriver or GeckoDriver must be installed and running
- Browser: Chrome or Firefox installed on the test machine
- File permissions: Ensure test files are readable by the browser process
- Network access: Forms must be accessible from the test environment
Symfony Panther handles form submissions and file uploads exactly as a real browser would, making it an excellent choice for testing complex web applications with dynamic forms and file handling requirements.
Installation
composer require symfony/panther
For more information, visit the official Symfony Panther documentation.