有没有办法在GitHub动作上模拟基本硬件(鼠标,显示)的存在?

发布于 2025-02-04 17:55:12 字数 1234 浏览 3 评论 0原文

我目前正在从事一个python项目,该项目依赖于 pyautogui 控制鼠标。在将任何新更改推向GitHub之前,我设置了一系列测试,以便在我的计算机上运行,​​但是我也想在GitHub操作上设置一个工作流程,以按下构建和测试我的应用程序。

但是,正如您可以想象的那样,我的问题是在GitHub操作上进行测试的环境没有屏幕和鼠标。我的脚本只需要访问屏幕分辨率(width,height = pyautogui.size())并执行简单的操作( eg pyautogui.click('左')pyautogui.scroll(...))。我的测试实际上并不需要任何窗口弹出窗口才能在它们上执行操作,只是为了能够运行这些简单的功能。

目前,问题似乎是缺乏显示:

[...]
    import pyautogui
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'

因为display环境变量不存在(或者也许等于:0由于缺乏显示) 。我不确定鼠标是否是一个问题,因为它不会超出显示步骤,但我希望它也会有问题。

有人知道一种模拟屏幕和鼠标在GitHub Actions Runner上的存在的方法吗?还是有解决方法?

I'm currently working on a Python project that relies on PyAutoGUI to take control of the mouse. I set up an ensemble of tests to run on my machine before pushing any new change to GitHub, but I would also like to set up a workflow on GitHub Actions to build and test my application on push.

But, as you can imagine, my problem is that the environment in which the tests are run on GitHub Actions has no screen nor mouse. My scripts only need to access the screen resolution (width, height = pyautogui.size()) and to perform simple actions (e.g. pyautogui.click('left'), pyautogui.scroll(...)). My tests don't actually require any window to pop-up to perform actions on them, just to be able to run these simple functions.

Currently, it seems that the issue is the lack of display:

[...]
    import pyautogui
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/site-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/opt/hostedtoolcache/Python/3.7.13/x64/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'DISPLAY'

as the DISPLAY environment variable does not exist (or perhaps is equal to :0 due to the lack of display). I am not sure that the mouse is an issue yet, since it does not go beyond the display step, but I expect it to be problematic as well.

Does anyone know a way to simulate the presence of a screen and mouse on a GitHub Actions runner? Or any workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪意 2025-02-11 17:55:12

经过多次尝试,我终于设法使它起作用。我对X服务器一无所知,所以这几乎像是一个奇迹。

对于有兴趣的人来说,它只是归结为使用 python安装和运行xvfb 。这是我工作流程的关键部分:

[...]
    - name: Install
      run: |
        sudo apt-get install xvfb
        pip install -r requirements.txt
        pip install .
        
    - name: Run tests
      run: |
        xvfb-run -a -s "-screen 0 640x480x8" python -m unittest discover -s tests

Python只需要在与xvfb-run的同一命令上运行,仅此而已。这将设置display适当的变量,并求解依赖于此的Pyautogui导入。然后,来自Pyautogui的鼠标相关操作执行,没有任何问题,鼠标是否没有。

感谢您让我走上正确的曲目。

干杯。

After many attempts, I finally managed to get it working. I don't know anything about X servers, so it pretty much feels like a miracle.

To anyone interested, it simply comes down to installing and running xvfb with Python. Here's the critical part of my workflow:

[...]
    - name: Install
      run: |
        sudo apt-get install xvfb
        pip install -r requirements.txt
        pip install .
        
    - name: Run tests
      run: |
        xvfb-run -a -s "-screen 0 640x480x8" python -m unittest discover -s tests

Python simply needs to be run on the same command as xvfb-run, and that's it. This sets the DISPLAY variable appropriately and solves the PyAutoGUI import that relies on it. Then, the mouse-related actions from PyAutoGUI execute without any problem, mouse or not.

Thanks for putting me on the right tracks.

Cheers.

深者入戏 2025-02-11 17:55:12

谢谢救生@Johoward !!!为我提供了一些添加的功能:

  • 还安装'Python3-tk'和'Python3-Dev'带有'xfvb',正如我的github Action
  • 运行 pytest 中的错误日志所提示。而不是Unitest

我的完整的github动作.yml:

name: Push
on: push

jobs:
  pytest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository # must checkout!
        uses: actions/checkout@v3

      - name: Setup python
        uses: actions/setup-python@v4

      - name: Build environment
        run: pip install --no-cache-dir -r requirements.txt

      - name: Pytest with pyautogui
        run: |
          sudo apt-get install xvfb python3-tk python3-dev
          xvfb-run -a -s "-screen 0 640x480x8" pytest -v

thanks life-saver @JoHoward!!! worked for me with some additions:

  • also install 'python3-tk' and 'python3-dev' with 'xfvb', as prompted by error logs in my github action
  • run pytest instead of unittest

my full github action .yml:

name: Push
on: push

jobs:
  pytest:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository # must checkout!
        uses: actions/checkout@v3

      - name: Setup python
        uses: actions/setup-python@v4

      - name: Build environment
        run: pip install --no-cache-dir -r requirements.txt

      - name: Pytest with pyautogui
        run: |
          sudo apt-get install xvfb python3-tk python3-dev
          xvfb-run -a -s "-screen 0 640x480x8" pytest -v
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文