Home > Net >  Is there a way to simulate the presence of basic hardware (mouse, display) on GitHub Actions?
Is there a way to simulate the presence of basic hardware (mouse, display) on GitHub Actions?

Time:06-08

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?

CodePudding user response:

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.

  • Related