简体   繁体   English

如何使用python检测应用程序是否正在运行以及MAC OS下是否显示消息框

[英]How to use python to detect if an application is running and if a message box is displayed under MAC OS

I am trying to write unit test for an application on Mac OS using python. 我正在尝试使用python在Mac OS上为应用程序编写单元测试。 There is one problem I encounter and have no idea how to do it. 我遇到一个问题,不知道该怎么做。 I want the testing program to check if the application is running by checking process and I also want to check if a message box is displayed. 我希望测试程序通过检查进程检查应用程序是否正在运行,我还想检查是否显示了消息框。 In addition, I hope testing program can automatically click button on message box. 另外,我希望测试程序可以自动点击消息框上的按钮。 Could anyone give me some suggestions? 有人能给我一些建议吗?

Here's one way to do it with AppleScript: 以下是使用AppleScript执行此操作的一种方法:

import subprocess

def is_runnning(app):
    count = int(subprocess.check_output(["osascript",
                "-e", "tell application \"System Events\"",
                "-e", "count (every process whose name is \"" + app + "\")",
                "-e", "end tell"]).strip())
    return count > 0

print is_runnning("iTunes")

See also this for some variations. 有关一些变化,请参阅此内容

Check out psutil for inspecting various parts of the host system. 检查psutil以检查主机系统的各个部分。 With this python library you can inspect which processes are running. 使用此python库,您可以检查正在运行的进程。

As far as interacting with OS X itself, this thread has some nice information about controlling the mouse and keyboard using PyObjC 至于与OS X本身的交互, 这个线程有一些关于使用PyObjC控制鼠标和键盘的很好的信息

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

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