Repository: pyscript/pyscript
File: pyscriptjs/tests/integration/support.py (Python)

Code before:

        if expected_messages:
            # expected-but-not-found
            raise JsErrorsDidNotRaise(expected_messages, js_errors)

        if js_errors:
            # found-but-not-expected
            raise JsErrors(js_errors)

    def clear_js_errors(self):
        """
        Clear all JS errors.
        """
        self._js_errors = []

    def writefile(self, filename, content):
        """
        Very thin helper to write a file in the tmpdir
        """
        f = self.tmpdir.join(filename)
        f.dirpath().ensure(dir=True)
        f.write(content)

    def goto(self, path):
        self.logger.reset()
        self.logger.log("page.goto", path, color="yellow")
        url = f"{self.http_server_addr}/{path}"
        self.page.goto(url, timeout=0)

    def wait_for_console(self, text, *, timeout=None, check_js_errors=True):
        """
        Wait until the given message appear in the console.

        Note: it must be the *exact* string as printed by e.g. console.log.
        If you need more control on the predicate (e.g. if you want to match a
        substring), use self.page.expect_console_message directly.

        timeout is expressed in milliseconds. If it's None, it will use
        playwright's own default value, which is 30 seconds).

        If check_js_errors is True (the default), it also checks that no JS
        errors were raised during the waiting.
        """

        def pred(msg):
            return msg.text == text

        try:
            with self.page.expect_console_message(pred, timeout=timeout):
                pass
        finally:

Review comment (suggestion):
see my comment above. If it's called `disable_cors_headers()` here, it should also be called `disable_cors_headers` in `dev_server`.
And probably in `router`, it should be called `use_cors_headers`.

Code after:
            )
            pdb.set_trace()

    def init_page(self, page):
        self.page = page

        # set default timeout to 60000 millliseconds from 30000
        page.set_default_timeout(60000)

        self.console = ConsoleMessageCollection(self.logger)
        self._js_errors = []
        page.on("console", self._on_console)
        page.on("pageerror", self._on_pageerror)

    @property
    def headers(self):
        if self.dev_server is None:
            return self.router.headers
        return self.dev_server.RequestHandlerClass.my_headers()

    def disable_cors_headers(self):
        if self.dev_server is None:
            self.router.enable_cors_headers = False
        else:
            self.dev_server.RequestHandlerClass.enable_cors_headers = False

    def run_js(self, code):
        """
        allows top level await to be present in the `code` parameter
        """
        self.page.evaluate(
            """(async () => {
            try {%s}
            catch(e) {
                console.error(e);
            }
            })();"""
            % code
        )

    def teardown_method(self):
        # we call check_js_errors on teardown: this means that if there are still
        # non-cleared errors, the test will fail. If you expect errors in your
        # page and they should not cause the test to fail, you should call
        # self.check_js_errors() in the test itself.
        self.check_js_errors()

    def _on_console(self, msg):
        self.console.add_message(msg.type, msg.text)

    def _on_pageerror(self, error):