@@ -12,6 +12,9 @@ import 'package:puppeteer/puppeteer.dart';
1212import 'package:puppeteer_screenshots/puppeteer_utils.dart' ;
1313import 'package:retry/retry.dart' ;
1414
15+ /// Filters server or client messages, ignoring and not reporting them as errors.
16+ typedef IgnoreMessageFilter = bool Function (String message);
17+
1518/// Creates and tracks the headless Chrome environment, its temp directories and
1619/// and uncaught exceptions.
1720class TestBrowser {
@@ -31,14 +34,19 @@ class TestBrowser {
3134 /// The coverage report of CSS files.
3235 final _cssCoverages = < String , _Coverage > {};
3336
37+ /// Filters to ignore server errors.
38+ final List <IgnoreMessageFilter >? _ignoreServerErrors;
39+
3440 TestBrowser ({
3541 required String origin,
3642 String ? testName,
3743 String ? coverageDir,
3844 bool displayBrowser = false ,
45+ List <IgnoreMessageFilter >? ignoreServerErrors,
3946 }) : _displayBrowser = displayBrowser,
4047 _testName = testName,
4148 _origin = origin,
49+ _ignoreServerErrors = ignoreServerErrors,
4250 _coverageDir = coverageDir ?? Platform .environment['COVERAGE_DIR' ],
4351 _tempDir = Directory .systemTemp.createTempSync ('pub-headless' );
4452
@@ -177,10 +185,7 @@ class TestBrowserSession {
177185 TestBrowserSession (this ._browser, this ._context);
178186
179187 /// Creates a new page and setup overrides and tracking.
180- Future <R > withPage <R >({
181- required Future <R > Function (Page page) fn,
182- bool Function (String message)? serverErrorFilter,
183- }) async {
188+ Future <R > withPage <R >({required Future <R > Function (Page page) fn}) async {
184189 final clientErrors = < ClientError > [];
185190 final serverErrors = < String > [];
186191 final page = await _context.newPage ();
@@ -300,8 +305,10 @@ class TestBrowserSession {
300305 if (clientErrors.isNotEmpty) {
301306 throw Exception ('Client errors detected: ${clientErrors .first }' );
302307 }
303- if (serverErrorFilter != null ) {
304- serverErrors.retainWhere (serverErrorFilter);
308+ if (_browser._ignoreServerErrors != null ) {
309+ serverErrors.removeWhere (
310+ (msg) => _browser._ignoreServerErrors! .any ((fn) => fn (msg)),
311+ );
305312 }
306313 if (serverErrors.isNotEmpty) {
307314 throw Exception (
0 commit comments