Getting to the intranet

Ok, let's teach our robot something useful!

Defining our task

Our robot will not be spending its precious time setting a value to a variable; it has a job to do. Let's start by being ambitious and describe the full job it will need to do in one phrase. That will become our task description.

We settle on: Insert the sales data for the week and export it as a PDF. Let's update the high-level documentation of our robot. When someone reads the high-level documentation, they should get a basic understanding of what this robot is about. We can do this by adding docstrings to our @task function. To do this, you have to write your text between """. In the tasks.py file, add bellow the definition of "minimal_task()":

from robocorp.tasks import task @task def minimal_task(): """Insert the sales data for the week and export it as a PDF"""

You can click the clipboard icon on the top-right corner of the code blocks on these pages to copy the whole block of code. Then you can paste the code into your editor. Typing is also ok!

The first step: opening the intranet

Just like Maria does when she starts working with the sales data, the robot will need to open a browser and navigate to the intranet website.

Great! Let's add it to our task as the first step. We are going to give a suggestive name to our function to clearly define what it does. We choose to name our function open_the_intranet_website(). Actually, we should also rename our task to something more suggestive, like robot_spare_bin_python. Delete the Hello World! lines, and add our function call there, just like this:

@task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" open_the_intranet_website()

The meaning of open_the_intranet_website is clear enough for us humans, but the robot does not yet know what that means or what it is supposed to do. How do we explain that to it? We implement a new function, where we tell the robot what it does in practice.

Adding a new function

Add the new function definition after our @task function.

def open_the_intranet_website():

The full robot file should look similar to this:

from robocorp.tasks import task @task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" open_the_intranet_website() def open_the_intranet_website():

You can place these functions anywhere, but we choose to keep the @task one at the top so that we can see at a glance what our robot will do

Great! Now, open_the_intranet_website() is an empty function. It does not do anything yet, it's just a name. How can we teach our robot to open a browser and work with it? 🤔

Adding our first library

The answer is by adding a new library. Libraries teach our robots new skills by making new keywords available to them. In our case, we want to work with the browser, so we will add browser module of the robocorp library. Libraries are added at the top of robot scripts. Just like we have the task import at the top of the page, we will now import the browser module as well.

from robocorp.tasks import task from robocorp import browser

Using functions from our imports

To open a browser and navigate to the intranet, we can now use the goto() function provided by the browser module, and give it the address (URL) of the intranet as an argument. We learned from Maria that the intranet is located at https://robotsparebinindustries.com/, so we can provide this URL as an argument.

def open_the_intranet_website(): browser.goto("https://robotsparebinindustries.com/")

Docstrings to explain our functions

The function we just wrote, is a simple one, it does only one thing. However, in the future, we might add more complex logic to it, therefore, it is useful to add docstrings, just like above.

def open_the_intranet_website(): """Navigates to the given URL""" browser.goto("https://robotsparebinindustries.com/")

The robot so far

Now our robot looks like this:

from robocorp.tasks import task from robocorp import browser @task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" open_the_intranet_website() def open_the_intranet_website(): """Navigates to the given URL""" browser.goto("https://robotsparebinindustries.com/")

Let's run our robot and enjoy the robot starting to work for us:

  • Open the Command Palette by pressing Shift-Command-P (macOS) or Ctrl+Shift+P (Windows).
  • (Optional): Type run robot. This will find the command you need.
  • Press Enter to run the Robocorp: Run Robot command.

You should see the robot opening a web browser and navigating to the given URL. The output in the terminal should look similar to this:

Collecting tasks from: tasks.py ======================= Running: robot_spare_bin_python ======================== robot_spare_bin_python status: PASS ================================================================================ Robocorp Log (html): /Users/moni/Projects/Examples/example-python-robotsparebin/output/log.html OK.

Slowing down our robot

Right now, it moves really fast. We didn't even get to see it properly. Let us slow it down a bit. We can do this by configuring the browser properties. The configure() function of the browser, can take a slowmo argument. Let us set it to 100 to see better see what happens when our robot runs. Add this right after our task definition.

from robocorp.tasks import task from robocorp import browser @task def robot_spare_bin_python(): """Insert the sales data for the week and export it as a PDF""" browser.configure( slowmo=100, ) open_the_intranet_website() def open_the_intranet_website(): """Navigates to the given URL""" browser.goto("https://robotsparebinindustries.com/")

Let's run our robot again. Now we can actually see something!

By setting a higher value for slowmo we can slow down the robot execution even further. This number represent the number of miliseconds the robot waits for between 2 actions.

Closing the browser after task completion

You may have noticed that the robot opened the web browser, navigated to the given URL, and then immediately closed the browser.

This automatic closing of the browser is the default behavior of the robocorp.browser module. The reason is to keep the robot from leaving unnecessary browser processes running after it's done. This is important in production use!

What we learned

  • Try to define the task of your robot in one concise phrase.
  • You can define your own functions and call them in your task.
  • Your own functions can call other functions.
  • You can get new function for your robot by importing libraries.
  • Libraries add new capabilities to your robot.
  • Libraries imports are defined at the top of the file to keep it structured and oragnized.
  • Testing your robots often, even after each edit, gives you continuous feedback on how your robot functions.
  • You can open a browser with the goto() function by giving it the URL you want to open as an argument.
  • Indentation is important.
  • You can configure your robot to run slower with the slowmo argument