{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# MAGIC\n", "\n", "`JupyterLibrary` provides a few lightweight [IPython](https://ipython.readthedocs.io/en/stable/interactive/magics.html) [magics](https://jakevdp.github.io/PythonDataScienceHandbook/01.03-magic-commands.html) for its own testing purposes. \n", "\n", "If you like writing and executing Robot Framework in a Jupyter kernel, you might like a more full-featured experience:\n", "\n", "- [robotkernel](https://github.com/robots-from-jupyter/robotkernel)\n", "- [xeus-robot](https://github.com/jupyter-xeus/xeus-robot) " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%reload_ext JupyterLibrary" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `%%robot` magic runs a cell of code as you would write in a `.robot` file. No funny stuff (by default)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%robot -o _static\n", "*** Tasks ***\n", "Log Something\n", " Log Something" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The interactive help is pretty good." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%robot?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of note: you can specify extra arguments to `robot.run` with `-a`, the name of a local variable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "args = dict(include=[\"mytag:a\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%robot -a args -o _static\n", "*** Tasks ***\n", "Do thing A\n", " [Tags] mytag:a\n", " Log A\n", "\n", "Do thing B\n", " [Tags] mytag:b\n", " Log B\n", "\n", "Do thing AB\n", " [Tags] mytag:a mytag:b\n", " Log AB" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Running `JupyterLibrary`\n", "\n", "The line below is a _Markdown Cell_... change it to a _Code Cell_ to run it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " %%robot\n", " *** Settings ***\n", " Documentation A nice task suite\n", " Library JupyterLibrary\n", " Suite Setup Wait For New Jupyter Server To Be Ready\n", " Test Teardown Reset JupyterLab And Close\n", " Suite Teardown Run Keyword And Ignore Error Terminate All Jupyter Servers\n", "\n", "\n", " *** Tasks ***\n", " A Notebook in JupyterLab\n", " Open JupyterLab\n", " Launch A New JupyterLab Document\n", " Add And Run JupyterLab Code Cell print(\"hello\" + \" world\")\n", " Wait Until page Contains hello world\n", " Capture Page Screenshot ran-code.png" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### With Widgets\n", "\n", "> There is some more stuff coming with `%%robot`, but for now, [`ipywidgets.interact`](https://ipywidgets.readthedocs.io/en/latest/examples/Using%20Interact.html) can be used to quickly build UI around robot-generated artifacts" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "from IPython.display import display, Image\n", "\n", "ipywidgets = None\n", "try:\n", " import ipywidgets\n", "except:\n", " pass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if ipywidgets:\n", " @ipywidgets.interact\n", " def show_image(i=(0, 100)):\n", " all_images = sorted(Path(\"_robot_magic_\").rglob(\"*.png\"), key=lambda p: p.stat().st_mtime)\n", " if not all_images:\n", " return\n", " start = all_images[0].stat().st_mtime\n", " i = min(len(all_images) - 1, i)\n", " img = all_images[i]\n", " delta = img.stat().st_mtime - start\n", " display(f\"[{round(delta)}s][{i} of {len(all_images)}] {img.name}\", Image(img))" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.3" } }, "nbformat": 4, "nbformat_minor": 4 }