Example: The latest XKCD in Robot
Example: The latest XKCD in Robot¶
Also this notebook is associated with Robot Framework kernel, and may therefore contain Robot Framework suite with tests or tasks. In this notebook we fetch and display the latest XKCD strip using just Robot Framework keywords.
As a prerequisite, we have been given the XKCD HTTP API URL https://xkcd.com/info.0.json
for fetching information of the latest strip in JSON.
Let’s save that URL into a variable.
*** Variables ***
${url} https://xkcd.com/info.0.json
Then we continue with RESTinstance keyword library for easily fetching JSON information from the Internet.
*** Settings ***
Library REST
Now we are ready to fetch the data.
*** Tasks ***
Get XKCD JSON
REST.Get https://xkcd.com/info.0.json
{ "seconds": 0.05719, "status": 200, "body": { "month": "1", "num": 2250, "link": "", "year": "2020", "news": "", "safe_title": "OK/okay/ok", "transcript": "", "alt": "After changing it back and forth several times and consulting with internet linguist Gretchen McCulloch, I settled on \"ok\" in my book How To, but I'm still on the fence. Maybe I should just switch to \"oK.\"", "img": "https://imgs.xkcd.com/comics/ok_okay_ok.png", "title": "OK/okay/ok", "day": "3" }, "headers": { "Connection": "keep-alive", "Content-Length": "297", "Server": "nginx", "Content-Type": "application/json", "Last-Modified": "Fri, 03 Jan 2020 13:07:52 GMT", "ETag": "W/\"5e0f3ca8-1a6\"", "Expires": "Sat, 04 Jan 2020 15:28:12 GMT", "Cache-Control": "max-age=300", "Content-Encoding": "gzip", "Accept-Ranges": "bytes", "Date": "Sun, 05 Jan 2020 14:22:47 GMT", "Via": "1.1 varnish", "Age": "271", "X-Served-By": "cache-hel6823-HEL", "X-Cache": "HIT", "X-Cache-Hits": "1", "X-Timer": "S1578234168.702370,VS0,VE0", "Vary": "Accept-Encoding" } }
With the Output keyword of RESTinstance library, we can select the URL from the response.
Now, you might have expected to see just the URL of the image been rendered, not the actual image. Let’s explain, what actually happened.
The keyword call Output response body img
returned string containing the URL of the latest XKCD strip image. For convenience, the underlying Robot Framework IPython kernel did feed that URL into IPython display Image helper class, which fetched the image and returned it in a form that JupyterLab could display directly.
Because this was too easy. Let’s do this one more time with screen scraping the strip directly from https://xkcd.com/. For this we use SeleniumLibrary for browser automation and SeleniumScreenshots for cropping the strip from the page.
*** Settings ***
Library SeleniumLibrary
Library SeleniumScreenshots
With SeleniumLibrary we need to first open a browser to interact with.
(The number we see as the result of the executed cell is the index of the opened browser, as documented for the Open browser keyword.)
Now we can use Capture and crop page screenshot keyword from SeleniumScreenshots to finish the task.
*** Tasks ***
Screenshot the latest XKCD strip
Capture and crop page screenshot
... xkcd.png id:comic
All is good, except that we have now an open SeleniumLibrary-controlled test browser open and running.
The test browser can be closed simply by closing down the notebook, but that works only as long as this Robot Framework task suite is only run with Jupyter.
To make the suite properly close the test browser also when being run outside Jupyter, we should configure a test suite teardown setting for that.
*** Settings ***
Suite teardown Close all browsers
The XKCD strip from https://xkcd.com/ is licensed under a Creative Commons Attribution-NonCommercial 2.5 License.