import json
import os
import wx.html2
import wx
from utils.log.log_helper import LogHelper
class ReportDialog(wx.Dialog):
def __init__(self, parent=None, title="测试报告", report_blocks=[], style=wx.DEFAULT_DIALOG_STYLE):
super(ReportDialog, self).__init__(parent, -1, title=title, style=style)
self.report_blocks = report_blocks
report_dialog_box = wx.FlexGridSizer(rows=1, cols=1, vgap=10, hgap=10)
self.report_web_view = wx.html2.WebView.New(self, id=3)
self.report_web_view.LoadURL(os.path.abspath('public/log/log.html'))
# 添加监听事件,以便在页面加载完以后在运行jsvascript
self.report_web_view.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.loaded)
report_dialog_box.Add(self.report_web_view, 1, wx.EXPAND)
report_dialog_box.AddGrowableRow(0)
report_dialog_box.AddGrowableCol(0)
self.SetSizer(report_dialog_box)
self.SetSize((650, 650))
self.Center()
def loaded(self, e):
if e.GetURL() == 'about:blank': # Skip if it's a blank page.
return
self.init_page()
def init_page(self):
if len(self.report_blocks) != 0:
for block in self.report_blocks:
self.run_script(LogHelper.add_log_script(block.title, 1, is_title=True))
block_logs = json.loads(block.logs_json)
for block_log in block_logs:
self.run_script(LogHelper.add_log_script(block_log["msg"], block_log["type"]))
self.run_script("changeResultColor('" + block.result.name + "')")
def run_script(self, message):
success, ret = self.report_web_view.RunScript(message)
print(message, success, ret, '==')