Download the PHP package sinnbeck/laravel-dom-assertions without Composer

On this page you can find all versions of the php package sinnbeck/laravel-dom-assertions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-dom-assertions

DOM Assertions for Laravel

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Installation

You can install the package via composer:

Example

Imagine we have a view with this html

Now we want to make sure that the correct menu item is selected when on this route. We could try with some regex to match it, but it might be easily break.

But this can be very brittle, and a simple linebreak can cause it to fail.

With this package you can now use an expressive syntax like this.

Usage

Testing the DOM

When calling a route in a test you might want to make sure that the view contains certain elements. To test this you can use the ->assertElementExists() method on the test response. The following will ensure that there is a body tag in the parsed response. Be aware that this package assumes a proper html structure and will wrap your html in a html, head and body tag if they are missing!

In case you want to get a specific element on the page, you can supply a css selector as the first argument to get a specific one.

The second argument of ->assertElementExists() is a closure that receives an instance of \Sinnbeck\DomAssertions\Asserts\AssertElement. This allows you to assert things about the element itself. Here we are asserting that the element is a div.

Just like with forms you can assert that certain attributes are present

or doesnt exist

You can also ensure that certain children exist.

If you need to be more specific you can use a css selector.

You can also check that the child element has certain attributes.

or ensure that certain children does not exist

Contains also allow a third argument to specify how many times the element should be matched.

If you just want to check for the element type you can leave out the second argument.

You can also find a certain element and do assertions on it. Be aware that it will only check the first matching element.

You can add a closure as the second argument which receives an instance of \Sinnbeck\DomAssertions\Asserts\AssertElement.

If you want to make an assertion against all elements that match the selection, you may use 'each'.

You can also infinitely assert down the dom structure.

Testing forms

Testing forms allows using all the dom asserts from above, but has a few special helpers to help test for forms. Instead of using ->assertElementExists() we will use ->assertFormExists() method on the test response.

The ->assertFormExists() method will check the first form it finds. In case you have more than one form, and want to use a different form that the first, you can supply a css selector as the first argument to get a specific one.

If there is more than one hit, it will return the first matching form.

The second argument of ->assertFormExists() is a closure that receives an instance of \Sinnbeck\DomAssertions\Asserts\AssertForm. This allows you to assert things about the form itself. Here we are asserting that it has a certain action and method

If you leave out the css selector, it will automatically default to finding the first form on the page

You can also check for csrf and method spoofing

Checking for methods other than GET and POST will automatically forward the call to ->hasSpoofMethod()

Or even arbitrary attributes

You can also easily test for inputs or text areas

Or arbitrary children

You can also ensure that certain children does not exist.

Testing for selects is also easy and works a bit like the assertFormExists(). It takes a selector as the first argument, and closure as the second argument. The second argument returns an instance of \Sinnbeck\DomAssertions\Asserts\AssertSelect. This can be used to assert that the select has certain attributes.

You can also assert that it has certain options. You can either check for one specific or an array of options

You can check if a select has a value.

You can also check selects with multiple values

Testing for datalists works mostly the same as selects. Only difference is that the selector needs to be either datalist or an id (eg. #my-list). The assertion uses the \Sinnbeck\DomAssertions\Asserts\AssertDatalist class.

Usage with Livewire

As livewire uses the TestResponse class from laravel, you can easily use this package with Livewire without any changes

Usage with Blade views

You can also use this package to test blade views.

Overview of methods

Base methods Description
->has($attribute, $value) Checks if element has a certain attribute with a certain value. Value is optional
->hasXdata('foo') Magic method. Same as ->has('x-data', 'foo')
->doesntHave($attribute, $value) Checks if element doesnt a certain attribute with a certain value. Value is optional
->is($type) Checks if the element is of a specific type (div, span etc)
->isDiv() Magic method. Same as ->is('div')
->contains($selector, $attributes, $count) Checks for any children of the current element
->containsDiv, ['class' => 'foo'], 3) Magic method. Same as ->contains('div', ['class' => 'foo'], 3)
->containsText($needle, $ignoreCase) Checks if the element's text content contains a specified string
->doesntContain($selector, $attributes) Ensures that there are no matching children
->doesntContainDiv, ['class' => 'foo']) Magic method. Same as ->doesntContain('div', ['class' => 'foo'])
->doesntContainText($needle, $ignoreCase) Checks if the element's text content doesn't contain a specified string
->find($selector, $callback) Find a specific child element and get a new AssertElement. Returns the first match.
->findDiv(fn (AssertElement $element) => {}) Magic method. Same as ->find('div', fn (AssertElement $element) => {})
Form specific methods Description
->hasAction($url) Ensures the form has a specific action
->hasMethod($method) Ensures a form has a specific method
->hasSpoofMethod($method) Ensures form has a spoofed method
->hasCSRF() Ensures form has a csrf token
->findSelect($selector, $callback) Finds a select to run assertions on
Select specific methods Description
->hasValue($value) Ensures a select has a specific value
->hasValues($values) Ensures a select has an array of values (multiple select)
->containsOption($attributes) Checks for an option with the given attributes
->containsOptions($attributes) Checks for any options with the given attributes (array of arrays)

Testing this package

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-dom-assertions with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-dom Version *
ext-libxml Version *
illuminate/testing Version ^9.0|^10.0|^11.0
symfony/css-selector Version ^6.0|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sinnbeck/laravel-dom-assertions contains the following files

Loading the files please wait ....