Search code examples
pythonpytesttravis-cipyperclipxclip

Accessing clipboard on Travis-CI


I am trying to run an (integration?) test on my application, to verify that it actually copies the expected string to the clipboard with pyperclip.

This part is working on my development machine (Windows 10); but fails on travis-ci, where I get the following in my travis job log.

self = <pyperclip.init_no_clipboard.<locals>.ClipboardUnavailable object at 0x7ff0cd743588>
args = ('7 809823 102890 string 291',), kwargs = {}
    def __call__(self, *args, **kwargs):
>       raise PyperclipException(EXCEPT_MSG)
E       pyperclip.PyperclipException: 
E           Pyperclip could not find a copy/paste mechanism for your system.
E           For more information, please visit https://pyperclip.readthedocs.io/en/latest/introduction.html#not-implemented-error
../../../virtualenv/python3.7.1/lib/python3.7/site-packages/pyperclip/__init__.py:301: PyperclipException

According the to the pyperclip documentation, this occurs on Linux when there is no copy/paste mechanism. The solution is to install one of the following (quoting pyperclip docs):

  • sudo apt-get install xsel to install the xsel utility.
  • sudo apt-get install xclip to install the xclip utility.
  • pip install gtk to install the gtk Python module.
  • pip install PyQt4 to install the PyQt4 Python module.

So in my .travis.yml file, I have

before_install:
  - sudo apt-get install xclip

I've also tried xsel, with the same results.

Since the system on travis is Ubuntu 16.04.6, I tried adding sudo apt-get install python3-pyperclip to the before_install key, with the same result.

I was not able to install either gtk or PyQt4 by adding them to the install key in .travis.yml.

install:
  - pip install -r requirements_dev.txt
  - pip install PyQt4
  # or
  - pip install gtk

As these both result in the following error:

 Could not find a version that satisfies the requirement gtk (from versions: )
No matching distribution found for gtk
The command "pip install gtk" failed and exited with 1 during .

By this point, my before_install looks like this:

  - sudo apt-get install xclip
  - sudo apt-get install xsel
  - sudo apt-get install python3-pyperclip
  - sudo apt-get install gtk2.0

This seems like overkill (and still does not work); but I am currently out of ideas on how to make that test pass. Any pointers would be greatly appreciated.

Thanks


Solution

  • xclip requires an X server to be running, while Travis machines run in headless mode. You need to run the command in a virtual framebuffer; install xvfb in addition to xclip and use xvfb-run pytest instead of pytest. Full config example:

    language: python
    addons:
      apt:
        packages:
        - xclip
        - xvfb
    python:
      - "3.7"
    
    # setup installation
    install:
      - pip install -r requirements_dev.txt
    
    script: xvfb-run pytest
    

    Here's an example build on Travis. Notice that I used addons to declare dependencies that should be installed with APT; your solution with installing them explicitly in a before_install section is perfectly valid too, just a matter of taste.