繁体   English   中英

Python Tkinter GUI自动化

[英]Python Tkinter GUI Automation

我想进入 GUI 自动化,以便在我自己的程序上运行测试。 我要测试的程序是用 Python 编写的,GUI 使用 Tkinter。 测试代码虽然不一定要在 python 中,但 CPP 也可以。 我做了一些研究,我已经面临一个问题。

根据我的研究,我发现“Windows 应用程序驱动程序”是一种测试 GUI 的免费方式。 还有“WinAppDriver UI Recorder”,使用起来很方便。 此外,(在我的情况下)“C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x86”中的“Inspect.exe”程序对于获取有关 GUI 元素的信息很有用。

假设我有一个像这样的小 python 代码(仅用于测试):

from Tkinter import *
import ttk

class Test():
    def __init__(self):
        self.root = Tk()
        self.root.geometry("250x100")
        self.text = StringVar()
        self.text.set("Original Text")
        self.buttonA = Button(self.root, textvariable=self.text)
        self.buttonA.configure(text="test")

        self.buttonB = Button(self.root,
                                text="Click to change text",
                                command=self.changeText
                              )
        self.buttonA.pack(side=LEFT)
        self.buttonB.pack(side=RIGHT)
        self.root.mainloop()

    def changeText(self):
        self.text.set("Updated Text")

app=Test()

当运行代码并使用Inspect.exe检查buttonB时,我得到的名称是“”(空)。 有什么方法可以将该名称更改为信息性和有用的名称,例如计算器示例中的“7”按钮的名称是“七”。 然后在测试器中使用如下:

self.driver.find_element_by_name("Seven").click()

应该是这样的:

self.driver.find_element_by_name("buttonB").click()

例如在我的情况下。

您可以命名 tkinter 小部件,例如:

self.buttonA = Button(self.root, textvariable=self.text,name = 'buttonA')

如果WinAppDriver无法找到以这种方式命名的 tkinter 小部件。 您可以修改代码以模仿 UI 自动化框架的方式调用按钮(并“保持”其他 UI 小部件):

我修改了您的示例以显示如何做到这一点

from Tkinter import *
import ttk

def _widgets_by_name(parent,name,widgets):
    if not parent.winfo_children():
        if name == parent.winfo_name() :
            widgets.append(parent)
    else:
        for child in parent.winfo_children():
            _widgets_by_name(child,name,widgets)
            
def find_widget_by_name(parent,name):
    ''' ui automation function that can find a widget in an application/hierarchy of widgets by its name '''
    widgets = []
    _widgets_by_name(parent,name,widgets)
    if len(widgets) == 0:
        raise Exception(f'no widget named {name} found')
    elif len(widgets) >1:
        raise Exception(f'multiple widget named {name} found')
    return (widgets[0])


class Test():
    def __init__(self):
        self.root = Tk()
        self.root.geometry("250x100")
        self.text = StringVar()
        self.text.set("Original Text")
        self.buttonA = Button(self.root, textvariable=self.text,name = 'button-a')
        self.buttonA.configure(text="test")

        self.buttonB = Button(self.root,
                                text="Click to change text",
                                command=self.changeText,
                                name = 'button-b'
                              )
        self.buttonA.pack(side=LEFT)
        self.buttonB.pack(side=RIGHT)
        # self.root.mainloop() do not start the main loop for testing purpose
        # can still be started outside of  __init__ for normal operation 

    def changeText(self):
        self.text.set("Updated Text")
    
app=Test()

# test the app step by step
# find one of the buttons and invoke it
find_widget_by_name(app.root,'button-b').invoke()
app.root.update() # replace the app mainloop: run the UI refresh once.

assert app.text.get() == "Updated Text"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM