Robot Framework Python Remote Server

repository·master·Indexed 41 days ago

https://github.com/robotframework/pythonremoteserver

A generic Python implementation of the Robot Framework remote library interface that allows test libraries to be hosted on different processes or machines than the Robot Framework runner. It provides the RobotRemoteServer class to host libraries over XML-RPC, supporting custom address and port configuration, background threading, and remote server management via CLI or API.

Tokens
4K
Snippets
18
Records
21
Agent score
99%

What's inside Robot Framework Python Remote Server

  1. Retrieve the active server port

    master

    If you use port=0 to let the server select a free port, you can find the active port using these methods:

    1. Console Output: The address and port are printed to the console on startup.
    2. Port File: Provide a port_file path during initialization. The server writes the port to this file and removes it when stopped.
    3. activate() method: Calling server.activate() returns the bound port and sets internal attributes.
    4. Attributes: Access server.server_address (a tuple of address and port) or server.server_port (the port as an integer).
  2. List keywords and view documentation from a remote server

    master

    You can use Robot Framework's Libdoc tool to inspect the keywords available on a running remote server.

    # List available keywords
    python -m robot.libdoc Remote::http://127.0.0.1:8270 list
    
    # Show documentation in the console
    python -m robot.libdoc Remote::http://127.0.0.1:8270 show
    
    # Generate HTML documentation
    python -m robot.libdoc Remote::http://127.0.0.1:8270 MyLibrary.html
  3. Run a remote library example

    master

    To use a remote library, you must first ensure the robotframework-pythonremoteserver is installed or available in your PYTHONPATH.

    1. Start the remote library: Execute the library file using your preferred Python interpreter (Python 2/3, Jython, IronPython, or PyPy).
    2. Run the tests: Execute the Robot Framework tests using the robot command or by running the robot module via an interpreter.

    Note: This specific example is designed to work when the tests and the remote library are running on the same machine.

    # 1. Start the library
    python examplelibrary.py
    
    # 2. Run the tests
    robot tests.robot
  4. Install robotremoteserver via pip

    master

    You can install the Python Remote Server using pip. To install the latest available release, use the --pre and --upgrade flags. To install a specific version, such as version 1.1, specify the version number directly.

    # Install the latest available release
    pip install --pre --upgrade robotremoteserver
    
    # Install exactly version 1.1
    pip install robotremoteserver==1.1
  5. Stop the remote server

    master

    The remote server can be stopped using several methods:

    • Console: Press Ctrl-C (not supported automatically if running in a background thread).
    • Signals: Send SIGINT, SIGTERM, or SIGHUP (does not work on Windows or in background threads).
    • Robot Framework Keyword: Use the Stop Remote Server keyword.
    • XML-RPC: Use the stop_remote_server method.
    • CLI: Run python -m robotremoteserver stop [uri].
    • Programmatically: Call the stop() method on the server instance (recommended for background threads).

    Note: Remote stopping via keywords, XML-RPC, or CLI can be disabled by setting allow_remote_stop=False during initialization.

  6. Start the remote server

    master

    You can start the server in two ways: automatically upon initialization or manually by calling the serve() method. To use the manual method, set serve=False during initialization.

    from robotremoteserver import RobotRemoteServer
    from mylibrary import MyLibrary
    
    # Option 1: Start automatically (default)
    RobotRemoteServer(MyLibrary())
    
    # Option 2: Start manually using serve()
    server = RobotRemoteServer(MyLibrary(), serve=False)
    server.serve()
  7. Test if a remote server is running

    master

    You can check the availability of a remote server via the CLI or programmatically.

    # Using CLI
    python -m robotremoteserver test
    python -m robotremoteserver test http://10.0.0.42:57347
    # Using Python API
    from robotremoteserver import test_remote_server
    
    if test_remote_server('http://localhost:8270'):
        print('Remote server running!')
  8. Configure the remote library address and port

    master

    By default, the remote library listens on localhost at port 8270. You can override these settings using command-line arguments when starting the library, or by passing variables to the Robot Framework execution.

    Using command-line arguments (Library side): Pass the address and port as positional arguments to the library script: python examplelibrary.py <ADDRESS> <PORT>

    Using Robot Framework variables (Test side): Pass the ADDRESS and PORT variables to the robot command: robot --variable ADDRESS:<ADDRESS> --variable PORT:<PORT> tests.robot

    # Start library on a specific IP and port
    python examplelibrary.py 192.168.1.15 7777
    
    # Run tests pointing to that specific IP and port
    robot --variable ADDRESS:192.168.1.15 --variable PORT:7777 tests.robot
  9. Create and upload Python distributions

    master

    Once the version is set and tagged, follow these steps to create and publish the package to PyPI:

    1. Clean the environment:
      invoke clean
    2. Build distributions: Create a source distribution and a universal wheel (compatible with Python 2 and 3).
      python setup.py sdist bdist_wheel --universal
    3. Upload to PyPI: Use twine to upload the files in the dist/ directory.
      twine upload dist/*
    4. Verify installation:
      pip install --upgrade robotremoteserver
    python setup.py sdist bdist_wheel --universal
    twine upload dist/*
  10. Install build dependencies for Python Remote Server

    master

    Before creating releases, you must install the necessary build dependencies using pip and the provided requirements-build.txt file. This ensures that tools like Invoke and rellu are available.

    Requirements:

    • Operating System: Linux (tested), OSX/Unix (expected to work). Windows is not recommended.
    • Python: 3.6 or newer.
    pip install -r requirements-build.txt
  11. Start the remote server in a background thread

    master

    To run the server in a background thread, initialize it with serve=False and use the threading module. Note that when running in a thread, automatic graceful stopping via Ctrl-C or signals is not supported; you must register signal handlers manually to call server.stop().

    import signal
    import threading
    from examplelibrary import ExampleLibrary
    from robotremoteserver import RobotRemoteServer
    
    server = RobotRemoteServer(ExampleLibrary(), port=0, serve=False)
    # Register signal handler to stop the server gracefully
    signal.signal(signal.SIGINT, lambda signum, frame: server.stop())
    
    server_thread = threading.Thread(target=server.serve)
    server_thread.start()
    
    while server_thread.is_alive():
        server_thread.join(0.1)