Skip to content

Commit

Permalink
Merge pull request #4 from joshhanley/feature/console-error-assertions
Browse files Browse the repository at this point in the history
Add console error assertions
  • Loading branch information
joshhanley authored Jul 16, 2024
2 parents 039e915 + c194b9f commit f99bbb7
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ In addition, the package provides the following:
- `assertHasClasses('@selector', ['class1', 'class2', 'class3'])`
- `assertHasOnlyClasses('@selector', ['class1', 'class2', 'class3'])`
- `assertMissingClasses('@selector', ['class1', 'class2', 'class3'])`
- `assertConsoleLogHasErrors()`
- `assertConsoleLogMissingErrors()`
- `assertConsoleLogHasError($message)`
- `assertConsoleLogMissingError($message)`

## Demo Package

Expand Down
96 changes: 96 additions & 0 deletions src/DuskBrowserMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,102 @@ public function assertMissingClasses()
};
}

public function assertConsoleLogHasErrors()
{
return function ($includeFavicon = false) {
$logs = $this->driver->manage()->getLog('browser');

$containsError = false;

foreach ($logs as $log) {
if (! isset($log['message']) || ! isset($log['level']) || ($log['level'] !== 'ERROR' && $log['level'] !== 'SEVERE')) {
continue;
}

// Ignore default favicon.ico error unless specified to include
if (! str($log['message'])->contains('favicon.ico') || $includeFavicon) {
$containsError = true;
break;
}
}

PHPUnit::assertTrue($containsError, 'Console log does not contain any error messages');

return $this;
};
}

public function assertConsoleLogMissingErrors()
{
return function ($includeFavicon = false) {
$logs = $this->driver->manage()->getLog('browser');

$containsError = false;

foreach ($logs as $log) {
if (! isset($log['message']) || ! isset($log['level']) || ($log['level'] !== 'ERROR' && $log['level'] !== 'SEVERE')) {
continue;
}

// Ignore default favicon.ico error unless specified to include
if (! str($log['message'])->contains('favicon.ico') || $includeFavicon) {
$containsError = true;
break;
}
}

PHPUnit::assertFalse($containsError, 'Console log contains an error message');

return $this;
};
}

public function assertConsoleLogHasError()
{
return function ($expectedMessage) {
$logs = $this->driver->manage()->getLog('browser');

$containsError = false;

foreach ($logs as $log) {
if (! isset($log['message']) || ! isset($log['level']) || ($log['level'] !== 'ERROR' && $log['level'] !== 'SEVERE')) {
continue;
}

if (str($log['message'])->contains($expectedMessage)) {
$containsError = true;
}
}

PHPUnit::assertTrue($containsError, "Console log error message \"{$expectedMessage}\" was not found");

return $this;
};
}

public function assertConsoleLogMissingError()
{
return function ($expectedMessage) {
$logs = $this->driver->manage()->getLog('browser');

$containsError = false;

foreach ($logs as $log) {
if (! isset($log['message']) || ! isset($log['level']) || ($log['level'] !== 'ERROR' && $log['level'] !== 'SEVERE')) {
continue;
}

if (str($log['message'])->contains($expectedMessage)) {
$containsError = true;
}
}

PHPUnit::assertFalse($containsError, "Console log error message \"{$expectedMessage}\" was found");

return $this;
};
}

protected function isVisibleScript()
{
return '
Expand Down
136 changes: 136 additions & 0 deletions tests/Browser/DuskBrowserMixinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,140 @@ public function render()
})
->assertMissingClasses('@item', ['test', 'sample']);
}

/** @test */
public function assert_console_log_has_errors_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('test')">
</div>
HTML;
}
})
->assertConsoleLogHasErrors();
}

/** @test */
public function assert_console_log_has_errors_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.log('test')">
</div>
HTML;
}
})
->assertConsoleLogHasErrors();
}

/** @test */
public function assert_console_log_missing_errors_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.log('test')">
</div>
HTML;
}
})
->assertConsoleLogMissingErrors();
}

/** @test */
public function assert_console_log_missing_errors_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('test')">
</div>
HTML;
}
})
->assertConsoleLogMissingErrors();
}

/** @test */
public function assert_console_log_has_error_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('test')">
</div>
HTML;
}
})
->assertConsoleLogHasError('test');
}

/** @test */
public function assert_console_log_has_error_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('other')">
</div>
HTML;
}
})
->assertConsoleLogHasError('test');
}

/** @test */
public function assert_console_log_missing_error_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('other')">
</div>
HTML;
}
})
->assertConsoleLogMissingError('test');
}

/** @test */
public function assert_console_log_missing_error_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div x-init="console.error('test')">
</div>
HTML;
}
})
->assertConsoleLogMissingError('test');
}
}

0 comments on commit f99bbb7

Please sign in to comment.