Created
March 22, 2023 18:09
-
-
Save keithel/765269ed9800a846fc846b969fb7478c to your computer and use it in GitHub Desktop.
Testing QWebEngineView rendering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| try: | |
| from PySide2.QtWebEngineWidgets import QWebEngineView | |
| from PySide2.QtCore import QObject, Signal, Slot, QTimer, QEventLoop, QPoint | |
| from PySide2.QtWidgets import QLabel, QWidget | |
| from PySide2.QtGui import QImage, QRegion | |
| except ModuleNotFoundError: | |
| from PySide6.QtWebEngineWidgets import QWebEngineView | |
| from PySide6.QtCore import QObject, Signal, Slot, QTimer, QEventLoop, QPoint | |
| from PySide6.QtWidgets import QLabel, QWidget | |
| from PySide6.QtGui import QImage, QPainter, QRegion, QPixmap | |
| import sys | |
| class WebEngineViewTester(QObject): | |
| sample_webview_color_finished = Signal() | |
| def __init__(self, parent=None): | |
| super().__init__(parent) | |
| self.TEST_COLOR = "#FF00FF" # %237f7f7f | |
| self._webview = QWebEngineView() | |
| self._sample_webview_color_result = None | |
| self.event_loop = QEventLoop() | |
| def _load_webview(self): | |
| self._webview.load("data:text/html,<style>body{background-color:" + self.TEST_COLOR.replace(r"#", "%23") + ";}</style>") | |
| @Slot() | |
| def _sample_webview_color(self): | |
| screen = self._webview.screen() | |
| win_id = self._webview.window().windowHandle().winId() | |
| webview_pixmap = screen.grabWindow(win_id) | |
| pixel_color = webview_pixmap.toImage().pixelColor(webview_pixmap.width()/2,webview_pixmap.height()/2).name() | |
| if pixel_color == self.TEST_COLOR: | |
| print("QWebEngineView rendered properly.") | |
| self._webview.close() | |
| self._sample_webview_color_result = pixel_color | |
| self.sample_webview_color_finished.emit() | |
| def test_basic_render(self): | |
| self._load_webview() | |
| self._webview.setGeometry(0, 0, 100, 100) | |
| self._webview.show() | |
| self.sample_webview_color_finished.connect(self.event_loop.quit) | |
| QTimer.singleShot(500, self._sample_webview_color) | |
| self.event_loop.exec() | |
| if self._sample_webview_color_result is None: | |
| print("Error: WebEngineViewTester.test_basic_render didn't finish with result", file=sys.stderr) | |
| else: | |
| print(f"WebEngineViewTester.test_basic_render { 'PASSED' if self._sample_webview_color_result == self.TEST_COLOR else 'FAILED'}") | |
| def _sample_webview_color_two(self, ok): | |
| if ok: | |
| page = self._webview.page() | |
| size = page.contentsSize().toSize() | |
| w = size.width() | |
| h = size.height() | |
| image = QImage(w, h, QImage.Format_ARGB32) | |
| painter = QPainter(image) | |
| print(f"painter type: {type(painter)}") | |
| self._webview.render(painter, QPoint()) | |
| painter.end() | |
| label = QLabel() | |
| label.setPixmap(QPixmap(image)) | |
| label.show() | |
| pixel_color = image.pixelColor(w/2, h/2).name().upper() | |
| else: | |
| pixel_color = None | |
| if pixel_color and pixel_color == self.TEST_COLOR: | |
| print("QWebEngineView rendered properly.") | |
| else: | |
| print(f"QWebEngineView failed to render properly, expected {self.TEST_COLOR}, got {pixel_color}") | |
| self._sample_webview_color_result = pixel_color | |
| self.sample_webview_color_finished.emit() | |
| @Slot() | |
| def _save_loadFinished_result_start_timer(self, loadOk): | |
| # Still need a small delay after the load is finished before content shows on the view. | |
| QTimer.singleShot(500, lambda: self._sample_webview_color_two(loadOk)) | |
| def test_basic_renderTwo(self): | |
| self._load_webview() | |
| self._webview.setGeometry(0, 0, 100, 100) | |
| self._webview.show() | |
| self._webview.loadFinished.connect(self._save_loadFinished_result_start_timer) | |
| self.sample_webview_color_finished.connect(self.event_loop.quit) | |
| QTimer.singleShot(5000, lambda: print("test_basic_renderTwo timed out.") or self.event_loop.quit() if self.event_loop.isRunning() else None) | |
| self.event_loop.exec() | |
| if self._sample_webview_color_result is None: | |
| print("Error: WebEngineViewTester.test_basic_render didn't finish with result", file=sys.stderr) | |
| else: | |
| print(f"WebEngineViewTester.test_basic_render { 'PASSED' if self._sample_webview_color_result == self.TEST_COLOR else 'FAILED'}") | |
| tester = WebEngineViewTester() | |
| tester.test_basic_renderTwo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment