PHP code example of sinnbeck / laravel-dom-assertions

1. Go to this page and download the library: Download sinnbeck/laravel-dom-assertions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sinnbeck / laravel-dom-assertions example snippets


$this->get('/some-route')
    ->assertElementExists();

$this->get('/some-route')
    ->assertElementExists('#nav');

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->is('div');
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->has('x-data', '{foo: 1}');
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->doesntHave('x-data', '{foo: 2}');
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->has([
            'data-x' => 'a',
            'data-y' => 'b',
        ]);
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->contains('div');
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->contains('div:nth-of-type(3)');
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->contains('li.list-item', ['x-data' => 'foobar']);
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->contains('li.list-item', ['x-data' => 'foobar'], 3);
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->contains('li.list-item', 3);
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->doesntContain('li.list-item', ['x-data' => 'foobar']);
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->find('li:nth-of-type(3)', function (AssertElement $element) {
            $element->is('li');
        });
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->each('li', function (AssertElement $element) {
            $element->has('class', 'list-item');
        });
    });

$this->get('/some-route')
    ->assertElementExists(function (AssertElement $element) {
        $element->find('div', function (AssertElement $element) {
            $element->is('div');

            $element->find('p', function (AssertElement $element) {
                $element->is('p');
                $element->find('#label', fn (AssertElement $element) => $element->is('span'));
            });

            $element->find('p:nth-of-type(2)', function (AssertElement $element) {
                $element->is('p');
                $element->find('.sub-header', fn (AssertElement $element) => $element->is('h4'));
            });
        });
    });

$assert->isDiv();                              // is('div')
$assert->hasXData('{foo: 1}');                 // has('x-data', '{foo: 1}')
$assert->containsDiv(['class' => 'foo'], 3);   // contains('div', ['class' => 'foo'], 3)
$assert->doesntContainSpan(['class' => 'foo']); // doesntContain('span', ['class' => 'foo'])
$assert->findDiv(fn (AssertElement $el) => $el->isDiv()); // find('div', ...)

$this->get('/some-route')
    ->assertFormExists();

$this->get('/some-route')
    ->assertFormExists('#users-form');

$this->get('/some-route')
    ->assertFormExists('#form1', function (AssertForm $form) {
        $form->hasAction('/logout')
            ->hasMethod('post');
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->hasAction('/logout')->hasMethod('post');
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->hasAction('/update-user')
            ->hasMethod('post')
            ->hasCSRF()
            ->hasSpoofMethod('PUT');
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->hasMethod('PUT');
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->has('x-data', 'foo')
            ->hasEnctype('multipart/form-data'); // magic method
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->containsInput(['name' => 'first_name', 'value' => 'Gunnar'])
            ->containsTextarea(['name' => 'comment', 'value' => '...']);
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->contains('label', ['for' => 'username'])
            ->containsButton(['type' => 'submit']) // magic method
            ->doesntContain('label', ['for' => 'password']);
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->findSelect('select:nth-of-type(2)', function (AssertSelect $select) {
            $select->has('name', 'country');
        });
    });

$this->get('/some-route')
    ->assertFormExists(function (AssertForm $form) {
        $form->findSelect('select:nth-of-type(2)', function (AssertSelect $select) {
            $select->containsOption([
                'x-data' => 'none',
                'value'  => 'none',
                'text'   => 'None',
            ])->containsOptions(
                ['value' => 'dk', 'text' => 'Denmark'],
                ['value' => 'us', 'text' => 'USA'],
            );
        });
    });

$this->get('/some-route')
    ->assertFormExists('#form1', function (AssertForm $form) {
        $form->findSelect('select', function (AssertSelect $select) {
            $select->hasValue('da');
            $select->hasValues(['da', 'en']);
        });
    });

$this->get('/some-route')
    ->assertFormExists('#form1', function (AssertForm $form) {
        $form->findDatalist('#skills', function (AssertDatalist $list) {
            $list->containsOptions(
                ['value' => 'PHP'],
                ['value' => 'Javascript'],
            );
        });
    });

$this->get('/some-route')
    ->assertElementExists('#overview', function (AssertElement $assert) {
        $assert->containsText('Hello World');
    });

$this->get('/some-route')
    ->assertElementContainsText('#overview', 'Hello World')
    ->assertElementContainsText('#overview', 'hello world', ignoreCase: true);

$this->get('/some-route')
    ->assertElementContainsText('#overview', 'Hello World', normalizeWhitespace: true);

$this->get('/some-route')
    ->assertElementContainsNormalizedText('#overview', 'Hello World');

$assert->containsText('Hello World', ignoreCase: false, normalizeWhitespace: true);

$assert->containsNormalizedText('Hello World');
$assert->doesntContainNormalizedText('Goodbye World');

config()->set('dom-assertions.normalize_whitespace', true);

return [
    /*
    | When enabled, text comparisons performed by `containsText` and
    | `doesntContainText` will collapse consecutive whitespace and trim
    | vertical whitespace from both the needle and haystack by default.
    |
    | This can still be overridden per-call by passing an explicit boolean
    | as the third argument to those assertions.
    */
    'normalize_whitespace' => true,
];

$this->get('/some-route')
    ->assertContainsElement('#content')
    ->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success'])
    ->assertDoesntExist('div.not-here');

$this->blade('<x-some-blade>')
    ->assertContainsElement('#content')
    ->ddContent();

Livewire::test(UserForm::class)
    ->assertElementExists('form', function (AssertElement $form) {
        $form->find('#submit', function (AssertElement $assert) {
            $assert->is('button');
            $assert->has('text', 'Submit');
        })->contains('[wire\:model="name"]', 1);
    });

$this->view('navigation')
    ->assertElementExists('nav > ul', function (AssertElement $ul) {
        $ul->contains('li', ['class' => 'active']);
    });

$this->component(Navigation::class)
    ->assertElementExists('nav > ul', function (AssertElement $ul) {
        $ul->contains('li', ['class' => 'active']);
    });

$this->component(OptionsComponent::class)
    ->wrap('select')
    ->assertSelect(function (AssertSelect $select) {
        $select->hasValue('2');
    });

$this->component(TextInput::class)
    ->wrap('form', ['method' => 'post', 'action' => '/login'])
    ->assertForm(function (AssertForm $form) {
        $form->hasMethod('post');
        $form->containsInput(['name' => 'email']);
    });

use Rector\Config\RectorConfig;
use Sinnbeck\DomAssertions\Rector\Rules\AssertElementToAssertContainsElementRule;

return RectorConfig::configure()
    ->withRules([
        AssertElementToAssertContainsElementRule::class,
    ]);

// Before
$response->assertElement('#content', function (AssertElement $element) {
    $element->find('h1', function (AssertElement $element) {
        $element->containsText('Hello World');
    });
    $element->contains('p', ['class' => 'foo']);
});

// After
$response->assertContainsElement('#content h1', ['text' => 'Hello World'])
         ->assertContainsElement('#content p', ['class' => 'foo']);
bash
php artisan vendor:publish --tag=dom-assertions-config