如何处理在测试 wxPython 应用程序时打开的对话框?
因为有人已经有类似的问题:
问题是,一旦应用程序启动模态对话框,控制不会返回,直到模态对话框退出,此时测试脚本将数据输入其中为时已晚
一般来说,我想为以下工作流程编写一个测试用例:
- 用户按下按钮“SomeProcessing”
- 在打开的对话框中,用户选择“Selection 1”并按 OK
- 根据选择处理数据并与已知结果进行比较 (
data_after_processing
)
如何执行第 2 步以使事情自动发生(下面的示例打开Dlg_GetUserInput
并等待手动输入)?可能是我对 GUI 测试的理解存在缺陷,并且不应将第 3 部分视为 GUI 测试?在那种情况下,我可能需要重写代码......
欢迎任何建议!
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title)
btn = wx.Button(self, label="SomeProcessing")
self.Bind(wx.EVT_BUTTON, self.SomeProcessing, btn)
def SomeProcessing(self,event):
self.dlg = Dlg_GetUserInput(self)
if self.dlg.ShowModal() == wx.ID_OK:
if self.dlg.sel1.GetValue():
print 'sel1 processing'
self.data_after_processing = 'boo'
if self.dlg.sel2.GetValue():
print 'sel2 processing'
self.data_after_processing = 'foo'
class Dlg_GetUserInput(wx.Dialog):
def __init__(self, parent):
wx.Dialog.__init__(self, parent)
self.sel1 = wx.CheckBox(self, label='Selection 1')
self.sel2 = wx.CheckBox(self, label='Selection 2')
self.OK = wx.Button(self, wx.ID_OK)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.sel1)
sizer.Add(self.sel2)
sizer.Add(self.OK)
self.SetSizer(sizer)
def test():
app = wx.PySimpleApp()
mf = MyFrame(None, 'testgui')
for item in mf.GetChildren():
if item.GetLabel() == 'SomeProcessing':
btn = item
break
event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
mf.GetEventHandler().ProcessEvent(event)
"""
PROBLEM: here I'd like to simulate user input
sel1 in Dlg_GetUserInput
(i.e. mf.dlg.sel1.SetValue())
and check that
data_after_processing == 'boo'
"""
mf.Destroy()
test()