Last active
May 23, 2025 15:50
-
-
Save kanzure/f046196caffb7b2c5c8c77ac4dce28ea to your computer and use it in GitHub Desktop.
random wxpython testing example
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
import wx | |
import wx.adv | |
import time | |
# -------------------------- | |
# GUI Application Definition | |
# -------------------------- | |
class MyFrame(wx.Frame): | |
def __init__(self): | |
super().__init__(parent=None, title="Simple wxPython App") | |
panel = wx.Panel(self) | |
self.text_input = wx.TextCtrl(panel) | |
self.button = wx.Button(panel, label="Greet") | |
self.output = wx.StaticText(panel, label="") | |
sizer = wx.BoxSizer(wx.VERTICAL) | |
sizer.Add(self.text_input, 0, wx.ALL | wx.EXPAND, 5) | |
sizer.Add(self.button, 0, wx.ALL | wx.CENTER, 5) | |
sizer.Add(self.output, 0, wx.ALL | wx.CENTER, 5) | |
panel.SetSizer(sizer) | |
self.button.Bind(wx.EVT_BUTTON, self.on_greet) | |
def on_greet(self, event): | |
name = self.text_input.GetValue() | |
self.output.SetLabel(f"Hello, {name}!") | |
# -------------------------- | |
# GUI Testing Functions | |
# -------------------------- | |
def test_button_click_directly(app, frame): | |
frame.text_input.SetValue("Alice") | |
event = wx.CommandEvent(wx.EVT_BUTTON.typeId, frame.button.GetId()) | |
wx.PostEvent(frame.button, event) | |
wx.Yield() # let the event process | |
assert frame.output.GetLabel() == "Hello, Alice!" | |
print("[✔] Direct function call test passed.") | |
def test_button_click_simulator(app, frame): | |
sim = wx.UIActionSimulator() | |
pos = frame.button.ClientToScreen((10, 10)) | |
wx.CallLater(200, lambda: sim.MouseMove(pos)) | |
wx.CallLater(300, lambda: sim.MouseClick()) | |
frame.text_input.SetValue("Bob") | |
time.sleep(0.5) | |
wx.Yield() | |
assert "Bob" in frame.output.GetLabel() | |
print("[✔] UIActionSimulator click test passed.") | |
def test_default_state(frame): | |
assert frame.output.GetLabel() == "" | |
assert frame.text_input.GetValue() == "" | |
print("[✔] Default state test passed.") | |
# -------------------------- | |
# Run App and Tests | |
# -------------------------- | |
class App(wx.App): | |
def OnInit(self): | |
self.frame = MyFrame() | |
self.frame.Show() | |
return True | |
if __name__ == "__main__": | |
app = App(False) | |
wx.CallLater(500, lambda: test_default_state(app.frame)) | |
wx.CallLater(1000, lambda: test_button_click_directly(app, app.frame)) | |
wx.CallLater(1500, lambda: test_button_click_simulator(app, app.frame)) | |
wx.CallLater(2000, lambda: app.frame.Close()) # Close app after tests | |
app.MainLoop() |
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
import wx | |
import json | |
import os | |
import time | |
ACTION_FILE = "session.json" | |
# ----------------------------- | |
# Action Recorder / Replayer | |
# ----------------------------- | |
class ActionRecorder: | |
def __init__(self): | |
self.actions = [] | |
def record(self, action_type, widget_name, data=None): | |
action = { | |
"time": time.time(), | |
"type": action_type, | |
"widget": widget_name, | |
"data": data | |
} | |
self.actions.append(action) | |
def save(self, filename=ACTION_FILE): | |
with open(filename, "w") as f: | |
json.dump(self.actions, f, indent=2) | |
def load(self, filename=ACTION_FILE): | |
if os.path.exists(filename): | |
with open(filename) as f: | |
self.actions = json.load(f) | |
def generate_test(self): | |
lines = ["def test_gui_simulation(app, frame):"] | |
for a in self.actions: | |
if a["type"] == "click": | |
lines.append(f" wx.PostEvent(frame.{a['widget']}, wx.CommandEvent(wx.EVT_BUTTON.typeId, frame.{a['widget']}.GetId()))") | |
elif a["type"] == "text": | |
lines.append(f" frame.{a['widget']}.SetValue('{a['data']}')") | |
lines.append(" wx.Yield()") | |
return "\n".join(lines) | |
# ----------------------------- | |
# GUI App | |
# ----------------------------- | |
class MyFrame(wx.Frame): | |
def __init__(self, recorder): | |
super().__init__(None, title="Recorder App") | |
self.recorder = recorder | |
panel = wx.Panel(self) | |
self.input = wx.TextCtrl(panel, name="input") | |
self.btn = wx.Button(panel, label="Click Me", name="btn") | |
self.output = wx.StaticText(panel, label="") | |
sizer = wx.BoxSizer(wx.VERTICAL) | |
sizer.Add(self.input, 0, wx.ALL | wx.EXPAND, 5) | |
sizer.Add(self.btn, 0, wx.ALL | wx.CENTER, 5) | |
sizer.Add(self.output, 0, wx.ALL | wx.CENTER, 5) | |
panel.SetSizer(sizer) | |
self.input.Bind(wx.EVT_TEXT, self.on_text) | |
self.btn.Bind(wx.EVT_BUTTON, self.on_click) | |
self.Bind(wx.EVT_CLOSE, self.on_close) | |
def on_text(self, event): | |
self.recorder.record("text", "input", self.input.GetValue()) | |
event.Skip() | |
def on_click(self, event): | |
self.recorder.record("click", "btn") | |
name = self.input.GetValue() | |
self.output.SetLabel(f"Hello, {name}!") | |
event.Skip() | |
def on_close(self, event): | |
self.recorder.save() | |
self.Destroy() | |
# ----------------------------- | |
# Application Entry Point | |
# ----------------------------- | |
class MyApp(wx.App): | |
def OnInit(self): | |
self.recorder = ActionRecorder() | |
self.frame = MyFrame(self.recorder) | |
self.frame.Show() | |
return True | |
if __name__ == "__main__": | |
app = MyApp(False) | |
app.MainLoop() | |
print("\n# ----- GENERATED UNIT TEST -----") | |
recorder = ActionRecorder() | |
recorder.load() | |
print(recorder.generate_test()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment