Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythontkintergui自动化_Python_Tkinter_Ui Automation_Inspector_Winappdriver - Fatal编程技术网

pythontkintergui自动化

pythontkintergui自动化,python,tkinter,ui-automation,inspector,winappdriver,Python,Tkinter,Ui Automation,Inspector,Winappdriver,我想进入GUI自动化,以便在我自己的程序上运行测试。我想测试的程序是用Python编写的,并使用Tkinter作为GUI。虽然测试代码不一定必须是python,但CPP也可以。我做了一些研究,我已经面临一个问题 从我的研究中,我发现“Windows应用程序驱动程序”是一种测试GUI的免费方式。还有一个“WinAppDriver UI Recorder”,它似乎很方便使用。此外,(在我的例子中)“C:\program Files(x86)\Windows Kits\10\bin\10.0.1904

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

从我的研究中,我发现“Windows应用程序驱动程序”是一种测试GUI的免费方式。还有一个“WinAppDriver UI Recorder”,它似乎很方便使用。此外,(在我的例子中)“C:\program Files(x86)\Windows Kits\10\bin\10.0.19041.0\x86”中的“Inspect.exe”程序对于获取有关GUI元素的信息非常有用

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

从Tkinter导入*
导入ttk
类测试():
定义初始化(自):
self.root=Tk()
自根几何(“250x100”)
self.text=StringVar()
self.text.set(“原始文本”)
self.buttonA=Button(self.root,textvariable=self.text)
self.buttonA.configure(text=“test”)
self.buttonB=按钮(self.root,
text=“单击以更改文本”,
command=self.changeText
)
自钮扣包装(侧=左)
自锁钮扣包装(侧面=右侧)
self.root.mainloop()
def changeText(自我):
self.text.set(“更新的文本”)
app=Test()
运行代码并使用
Inspect.exe检查
按钮nb
时,我得到的结果名称为“”(空)。有什么方法可以将该名称更改为信息性和有用的名称,如计算器示例中的“7”按钮的名称为“7”。然后在测试仪中使用,如下所示:

self.driver.按名称(“七”)查找元素。单击()

应该是这样的:

self.driver.通过名称(“buttonB”)查找元素。单击()


以我为例

您可以将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"

我尝试了在互联网上找到的几乎所有与tkinter小部件命名有关的东西,并在WinAppDriver中帮助识别它们。