Assert DOM Elements in Laravel Tests

Published on by

Assert DOM Elements in Laravel Tests image

The Laravel DOM Assertions package by René Sinnbeck adds document object model (DOM) assertion helpers to Laravel's TestResponse class:

This package provides some extra assertion helpers to use in HTTP Tests. If you have ever needed more control over your view assertions than assertSee, assertSeeInOrder, assertSeeText, assertSeeTextInOrder, assertDontSee, and assertDontSeeText then this is the package for you.

The package's README has an example of asserting a navigation menu to ensure the correct li element has an active class, but also ensure the "Home" li element does not:

$this->get(route('about'))
->assertOk()
->assertElementExists('nav > ul', function(AssertElement $ul) {
$ul->contains('li', [
'class' => 'active',
'text' => 'About'
]);
$ul->doesntContain('li', [
'class' => 'active',
'text' => 'Home'
]);
});

The snippet illustrates the granular control provided by this package, including testing DOM elements, nested elements, forms, and more. At the time of the writing, this package's main assertions include:

  • Assert if an element exists
  • Assert if an element has a given attribute
  • Assert if an element contains in another element
  • Assert that an element does not contain another element
  • Find an element to process further assertions
  • Assert if a form exists
  • Assert that a form has a CSRF token
  • Assert for select options

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

As a side note, this package's source code has an example of using the macroable mixin method to add multiple macro methods into another object from a service provider, which might be a helpful inspiration for others writing packages for Laravel.

Paul Redmond photo

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Oh Dear

Oh Dear is the best all-in-one monitoring tool for all your Laravel apps.

Visit Oh Dear
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. Partner with Lucky Media, your favorite Laravel Development Agency!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Oh Dear logo

Oh Dear

Oh Dear is the best all-in-one monitoring tool for all your Laravel apps.

Oh Dear
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell

The latest

View all →
FrankenPHP v1.0 is Here image

FrankenPHP v1.0 is Here

Read article
Self-healing URLs in Laravel image

Self-healing URLs in Laravel

Read article
Laravel 10.35 Released image

Laravel 10.35 Released

Read article
Solve n+1 queries in PHP with Scout APM image

Solve n+1 queries in PHP with Scout APM

Read article
Show Outdated Composer Dependencies in Laravel Pulse image

Show Outdated Composer Dependencies in Laravel Pulse

Read article
Announcing Laravel Pulse - A New Performance Monitoring Tool for Laravel Apps image

Announcing Laravel Pulse - A New Performance Monitoring Tool for Laravel Apps

Read article