1

如何处理在测试 wxPython 应用程序时打开的对话框?

因为有人已经有类似的问题

问题是,一旦应用程序启动模态对话框,控制不会返回,直到模态对话框退出,此时测试脚本将数据输入其中为时已晚

一般来说,我想为以下工作流程编写一个测试用例:

  1. 用户按下按钮“SomeProcessing”
  2. 在打开的对话框中,用户选择“Selection 1”并按 OK
  3. 根据选择处理数据并与已知结果进行比较 ( 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()  
4

2 回答 2

3

您可能想要查看这些应用程序之一进行 GUI 测试:

于 2012-08-10T17:30:38.783 回答
0

发布解决方案以防万一有人遇到同样的问题。

def test():
    app = wx.PySimpleApp()
    mf = MyFrame(None, 'testgui')
    for item in mf.GetChildren():
        if item.GetLabel() == 'SomeProcessing':
            btn = item
            break

    def clickOK():
        dlg = wx.GetActiveWindow()
        dlg.sel1.SetValue(True)
        clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, wx.ID_OK)
        dlg.ProcessEvent(clickEvent)

    event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())   
    wx.CallAfter(clickOK)
    mf.GetEventHandler().ProcessEvent(event)

    print 'data_after_processing:', mf.data_after_processing
    mf.Destroy()
于 2012-08-14T12:19:43.957 回答