我想要进入GUI自动化,以便在我自己的程序上运行测试。我要测试的程序是用Python编写的,并使用Tkinter作为GUI。虽然测试代码不一定要用python编写,CPP也可以。我做了一些研究,我已经面临一个问题了。
通过研究,我发现"Windows应用程序驱动程序“是一种免费的测试GUI的方法。还有"WinAppDriver UI记录器“,看起来使用起来很方便。另外,(在我的例子中) "C:\Program Files (X86)\Windowskits10bin\10.0.19041.0x86“中的`Inspect.exe程序对于获取有关图形用户界面元素的信息很有用。
假设我有一小段这样的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‘按钮的名称是"Seven“。然后在测试器中使用,如下所示:
self.driver.find_element_by_name("Seven").click()
它应该看起来像这样:
self.driver.find_element_by_name("buttonB").click()
例如在我的例子中。
发布于 2020-08-25 17:56:29
您可以像这样命名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"https://stackoverflow.com/questions/62328953
复制相似问题