Skip to content

Commit

Permalink
[TASK] Add possibility to define allowed browser console errors
Browse files Browse the repository at this point in the history
Allowed console errors are only valid for one test execution.
Thus, these exceptions need to be declared for every test case.

```
use TYPO3\TestingFramework\Core\Acceptance\Helper\Acceptance;

class SomeCest
{
    public function someTest(Scenario $scenario)
    {
        $acceptance = $scenario->current('modules')['\\' . Acceptance::class] ?? null;
        $acceptnance->allowBrowserError('/Found invalid token [[:digit:]]{40}/i');
        ...
```
  • Loading branch information
ohader committed Sep 7, 2020
1 parent af6e1e5 commit 28d10c8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Classes/Core/Acceptance/Helper/Acceptance.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@

use Codeception\Module;
use Codeception\Step;
use Codeception\TestInterface;

/**
* Helper class to verify javascript browser console does not throw errors.
*/
class Acceptance extends Module
{
private $allowedBrowserErrors = [];

public function _before(TestInterface $test)
{
parent::_before($test);
$this->allowedBrowserErrors = [];
}

/**
* Wait for backend progress bar to finish / disappear before "click" steps are performed.
*
Expand All @@ -48,6 +57,21 @@ public function _afterStep(Step $step)
$this->assertEmptyBrowserConsole();
}

public function allowBrowserError(string $pattern): void
{
$this->allowedBrowserErrors[] = $pattern;
}

public function isAllowedBrowserError(string $message): bool
{
foreach ($this->allowedBrowserErrors as $pattern) {
if (preg_match($pattern, $message)) {
return true;
}
}
return false;
}

/**
* Check browser console for errors and fail
*
Expand All @@ -64,6 +88,7 @@ public function assertEmptyBrowserConsole()
if (true === isset($logEntry['level'])
&& true === isset($logEntry['message'])
&& $this->isJSError($logEntry['level'], $logEntry['message'])
&& $this->isAllowedBrowserError((string)$logEntry['message']) === false
) {
// Timestamp is in milliseconds, but date() requires seconds.
$time = date('H:i:s', (int)($logEntry['timestamp'] / 1000));
Expand Down

0 comments on commit 28d10c8

Please sign in to comment.