【发布时间】:2018-07-11 05:30:38
【问题描述】:
我需要测试使用 pyautogui 的函数。例如,如果按键盘键“a”实际上按“a”。我怎样才能重现这个?有没有办法捕捉它被按下的键?
如果它适用于 Windows 和 Linux 则非常理想
【问题讨论】:
标签: python-3.x python-unittest pyautogui
我需要测试使用 pyautogui 的函数。例如,如果按键盘键“a”实际上按“a”。我怎样才能重现这个?有没有办法捕捉它被按下的键?
如果它适用于 Windows 和 Linux 则非常理想
【问题讨论】:
标签: python-3.x python-unittest pyautogui
PyAutoGUI 单元测试有这样的东西。 See the TypewriteThread code for details. 设置线程等待一秒钟并调用pyautogui.typewrite(),然后调用input() 接受文本。确保您的窗口处于焦点位置,并且 typewrite() 线程按 Enter(\n 字符)结束。
import time
import threading
import pyautogui
class TypewriteThread(threading.Thread):
def __init__(self, msg, interval=0.0):
super(TypewriteThread, self).__init__()
self.msg = msg
self.interval = interval
def run(self):
time.sleep(0.25)
pyautogui.typewrite(self.msg, self.interval)
t = TypewriteThread('Hello world!\n')
t.start()
response = input()
print(response == 'Hello world!')
【讨论】: