【问题标题】:test pyautogui functions with unittest in python3.6在 python3.6 中使用 unittest 测试 pyautogui 函数
【发布时间】:2018-07-11 05:30:38
【问题描述】:

我需要测试使用 pyautogui 的函数。例如,如果按键盘键“a”实际上按“a”。我怎样才能重现这个?有没有办法捕捉它被按下的键?

如果它适用于 Windows 和 Linux 则非常理想

【问题讨论】:

    标签: python-3.x python-unittest pyautogui


    【解决方案1】:

    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!')
    

    【讨论】:

    • 我已将其更改为 print(),它应该打印出 True。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多