agent-python-pytest icon indicating copy to clipboard operation
agent-python-pytest copied to clipboard

How to attach failed test screenshot to Report Portal using Playwright

Open Formartha opened this issue 1 year ago • 1 comments

Hello,

I have a fixture that implements opening browser and other activities, this is a fixture on a scope of a function. I would like to add the ability to upload a screenshot to report portal on failure, however, it seems like when it fails, the native report portal logger is unable to send logs from this fixture. the failure handling is on the finally stage.

What should be done?

    @pytest.mark.block_asyncio
    def setup_and_teardown(self, request):
        logging.setLoggerClass(RPLogger)
        logger.info("Starting PlayWright client")
        self.sb_actions.disable_email_notification_if_exists()
        # Determine if --headed was passed
        headed = request.config.getoption("--headed")
        headless = not headed
        self.video_dir = os.path.join(Config.data["logging_directory"], "videos")
        self.trace_dir = os.path.join(Config.data["logging_directory"], "traces")
        self.screenshot_dir = os.path.join(Config.data["logging_directory"], "screenshots")
        os.makedirs(self.video_dir, exist_ok=True)

        setup_successful = False

        with sync_playwright() as p:
            browser = p.chromium.launch(headless=headless)
            self.page = browser.new_page(record_video_dir=self.video_dir)

            self.start_tracing()
            try:
                self.setup_playwright_test()
                setup_successful = True
                yield  # opening browser session
                self.teardown_playwright_test()
            finally:
                if setup_successful:
                    self.stop_tracing()
                    self.cleanup(request)
                    browser.close()

Formartha avatar Nov 27 '24 15:11 Formartha

Hello @Formartha ! You are using the Pytest test framework. So, I'll move the issue to the right place.

AmsterGet avatar Nov 29 '24 09:11 AmsterGet