Top Related Projects
A browser testing and web crawling library for PHP and Symfony
Fast, easy and reliable testing for anything that runs in a browser.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
JavaScript API for Chrome and Firefox
Full-stack testing PHP framework
A browser automation framework and ecosystem.
Quick Overview
Laravel Dusk is an expressive, easy-to-use browser automation and testing API for Laravel applications. It provides a fluent interface for interacting with web elements, making assertions, and testing JavaScript-heavy applications without the need for complex setup or external dependencies.
Pros
- Easy to set up and integrate with Laravel projects
- Supports testing of JavaScript-heavy applications
- Provides a fluent, expressive API for browser automation
- Includes built-in support for taking screenshots and console logs
Cons
- Limited to testing Laravel applications
- Can be slower compared to other testing frameworks due to browser automation
- May require additional configuration for CI/CD environments
- Potential flakiness in tests due to timing issues or network latency
Code Examples
- Basic test example:
<?php
namespace Tests\Browser;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class ExampleTest extends DuskTestCase
{
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/')
->assertSee('Laravel')
->clickLink('Documentation')
->assertPathIs('/docs');
});
}
}
- Testing form submission:
<?php
public function testFormSubmission()
{
$this->browse(function (Browser $browser) {
$browser->visit('/register')
->type('name', 'John Doe')
->type('email', 'john@example.com')
->type('password', 'password')
->type('password_confirmation', 'password')
->press('Register')
->assertPathIs('/home');
});
}
- Testing with multiple browsers:
<?php
public function testChatApplication()
{
$this->browse(function (Browser $first, Browser $second) {
$first->loginAs(User::find(1))
->visit('/chat');
$second->loginAs(User::find(2))
->visit('/chat');
$first->type('message', 'Hey!')
->press('Send');
$second->waitForText('Hey!')
->assertSee('Hey!');
});
}
Getting Started
To get started with Laravel Dusk, follow these steps:
-
Install Dusk via Composer:
composer require --dev laravel/dusk
-
Run the Dusk installer:
php artisan dusk:install
-
Create a test file:
php artisan dusk:make ExampleTest
-
Run your Dusk tests:
php artisan dusk
For more detailed instructions and advanced usage, refer to the official Laravel Dusk documentation.
Competitor Comparisons
A browser testing and web crawling library for PHP and Symfony
Pros of Panther
- Supports multiple browsers (Chrome, Firefox, Safari) out of the box
- Integrates seamlessly with Symfony framework and PHPUnit
- Can be used for both testing and web scraping
Cons of Panther
- Steeper learning curve for developers not familiar with Symfony
- Less extensive documentation compared to Dusk
- May require additional setup for non-Symfony projects
Code Comparison
Panther:
$client = Client::createChromeClient();
$crawler = $client->request('GET', 'https://example.com');
$client->takeScreenshot('screenshot.png');
$client->waitFor('#my-element');
$client->executeScript('window.scrollTo(0, 1000);');
Dusk:
$browser->visit('https://example.com')
->screenshot('screenshot')
->waitFor('#my-element')
->script('window.scrollTo(0, 1000);');
Both Panther and Dusk provide powerful browser automation capabilities for PHP applications. Panther offers broader browser support and versatility, while Dusk is more tightly integrated with Laravel and has a gentler learning curve for Laravel developers. The code comparison shows that both libraries offer similar functionality, with slightly different syntax and method names. The choice between the two often depends on the specific framework and requirements of the project.
Fast, easy and reliable testing for anything that runs in a browser.
Pros of Cypress
- More comprehensive and feature-rich testing framework
- Better documentation and larger community support
- Supports multiple programming languages and frameworks
Cons of Cypress
- Steeper learning curve for beginners
- Limited cross-browser testing capabilities
- Can be slower for large test suites compared to Dusk
Code Comparison
Cypress example:
describe('Login Test', () => {
it('should log in successfully', () => {
cy.visit('/login')
cy.get('#email').type('user@example.com')
cy.get('#password').type('password123')
cy.get('button[type="submit"]').click()
cy.url().should('include', '/dashboard')
})
})
Dusk example:
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
use Laravel\Dusk\Browser;
class LoginTest extends DuskTestCase
{
public function testLogin()
{
$this->browse(function (Browser $browser) {
$browser->visit('/login')
->type('email', 'user@example.com')
->type('password', 'password123')
->press('Login')
->assertPathIs('/dashboard');
});
}
}
Both examples demonstrate a simple login test, but Cypress uses JavaScript and a more concise syntax, while Dusk uses PHP and Laravel's testing structure.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Pros of Playwright
- Cross-browser support for Chromium, Firefox, and WebKit
- Language-agnostic, supporting JavaScript, TypeScript, Python, .NET, and Java
- Built-in auto-wait functionality for improved test stability
Cons of Playwright
- Steeper learning curve for developers new to end-to-end testing
- Requires more setup and configuration compared to Dusk's Laravel integration
Code Comparison
Playwright:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await browser.close();
})();
Dusk:
<?php
namespace Tests\Browser;
use Tests\DuskTestCase;
class ExampleTest extends DuskTestCase
{
public function testBasicExample()
{
$this->browse(function ($browser) {
$browser->visit('/')
->assertSee('Laravel');
});
}
}
Summary
Playwright offers broader language and browser support, making it suitable for diverse projects. However, Dusk provides a more streamlined experience for Laravel developers with its tight framework integration. The choice between the two depends on project requirements and team expertise.
JavaScript API for Chrome and Firefox
Pros of Puppeteer
- More versatile and can be used with any JavaScript framework, not limited to Laravel
- Offers more advanced browser automation features, including PDF generation and performance analysis
- Larger community and more frequent updates
Cons of Puppeteer
- Steeper learning curve for developers not familiar with Node.js
- Requires more setup and configuration compared to Dusk's out-of-the-box Laravel integration
- May be overkill for simple testing scenarios in Laravel applications
Code Comparison
Dusk (PHP):
$browser->visit('/login')
->type('email', 'user@example.com')
->type('password', 'password')
->press('Login')
->assertPathIs('/dashboard');
Puppeteer (JavaScript):
await page.goto('/login');
await page.type('#email', 'user@example.com');
await page.type('#password', 'password');
await page.click('button[type="submit"]');
await page.waitForNavigation();
expect(page.url()).toBe('/dashboard');
Both Dusk and Puppeteer provide powerful browser automation capabilities, but they cater to different ecosystems. Dusk is tightly integrated with Laravel and offers a smoother experience for PHP developers working within the Laravel framework. Puppeteer, on the other hand, provides more flexibility and advanced features but requires JavaScript knowledge and additional setup.
Full-stack testing PHP framework
Pros of Codeception
- Framework-agnostic, supports multiple PHP frameworks and applications
- Comprehensive testing suite with unit, functional, and acceptance testing capabilities
- Extensive documentation and active community support
Cons of Codeception
- Steeper learning curve due to its extensive feature set
- May require more setup and configuration for specific project needs
- Can be overkill for smaller projects or those focused solely on browser testing
Code Comparison
Codeception test example:
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('ensure that home page works');
$I->amOnPage('/');
$I->see('Welcome');
Dusk test example:
<?php
$browser->visit('/')
->assertSee('Welcome');
Key Differences
- Codeception offers a more comprehensive testing solution, while Dusk focuses specifically on browser testing for Laravel applications
- Dusk provides a simpler syntax and easier setup for Laravel projects, but lacks the versatility of Codeception
- Codeception supports multiple testing types and frameworks, making it more suitable for complex or diverse projects
A browser automation framework and ecosystem.
Pros of Selenium
- Broader language support (Java, Python, C#, Ruby, JavaScript)
- More extensive ecosystem with wider community support
- Can be used for cross-browser testing on various platforms
Cons of Selenium
- Steeper learning curve, especially for non-developers
- Requires more setup and configuration
- Can be slower due to its broader compatibility
Code Comparison
Selenium (Python):
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com")
element = driver.find_element(By.ID, "submit-button")
element.click()
Dusk (PHP):
$browser->visit('https://example.com')
->click('#submit-button');
Selenium offers more flexibility but requires more code, while Dusk provides a more concise syntax for Laravel applications. Selenium's broader language support allows for integration with various tech stacks, but Dusk's tight integration with Laravel makes it easier to use within that ecosystem. Selenium's extensive community support means more resources and third-party tools are available, but it also comes with a steeper learning curve compared to Dusk's Laravel-specific approach.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Introduction
Laravel Dusk provides an expressive, easy-to-use browser automation and testing API. By default, Dusk does not require you to install JDK or Selenium on your machine. Instead, Dusk uses a standalone Chromedriver. However, you are free to utilize any other Selenium driver you wish.
Official Documentation
Documentation for Dusk can be found on the Laravel website.
Contributing
Thank you for considering contributing to Dusk! The contribution guide can be found in the Laravel documentation.
Code of Conduct
In order to ensure that the Laravel community is welcoming to all, please review and abide by the Code of Conduct.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
Laravel Dusk is open-sourced software licensed under the MIT license.
Top Related Projects
A browser testing and web crawling library for PHP and Symfony
Fast, easy and reliable testing for anything that runs in a browser.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
JavaScript API for Chrome and Firefox
Full-stack testing PHP framework
A browser automation framework and ecosystem.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot