Collecting the results

All the sales data has been entered. Nice! After entering all the sales data, Maria typically takes a screenshot of the sales summary:

Sales summary in the intranet page

Our robot can handle this case too!

Let's add a new step to our task. We'll call it Collect The Results:

*** Tasks ***
Insert the sales data for the week and export it as a PDF
    Open The Intranet Website
    Log In
    Download The Excel File
    Fill The Form Using The Data From The Excel File
    Collect The Results

Above the *** Tasks *** cell, we add a new keyword:

*** Keywords ***
Collect The Results

Taking a screenshot

The RPA.Browser library provides the Screenshot keyword to help us with this step. We will need to give it a locator of the element we want to take a screenshot of, and a path for the screenshot image file.

Locating the element

Let's take a look at the HTML markup of the page to see if there is a locator we could use:

...
<div class="alert alert-dark sales-summary" role="alert">
  <div><span>Active sales people:</span>...</div>
</div>
...

How did we find that HTML markup? See the How to find user interface elements using locators in web applications article if you want to learn how to view the HTML markup of web pages using browser developer tools.

We have a div element. It has class and role attributes. Since there are no id or name attributes to work with, we decide to use the CSS classes to target it. The sales-summary class seems like a good candidate because then it will be clear in our script what we are taking a screenshot of. Because we are using CSS to locate the element, our locator will start with css:. There are multiple types of locators: id:, name:, xpath, depending on the strategy used.

So our locator will be css:div.sales-summary, which means:

Using the CSS strategy, find me a div element that has the sales-summary CSS class. With extra cheese. Grazie! Danke schön!

Saving the file to the output directory

As a second argument, we will pass to the Screenshot keyword the path to use for the screenshot image file. We want the file to be called sales_summary.png and added to a new directory called output.

The output folder is where Robocorp Cloud will look for any files generated by our robot. Check the robot.yaml specification page to more information.

Our path for the file will be this: ${CURDIR}${/}output${/}sales_summary.png.

But what are those variables?

  • ${CURDIR} is a special built-in Robot Framework variable that represents the current directory, or the directory where our tasks.robot file is.

  • ${/} is another built-in variable that represents the directory path separator. We use this variable to make sure that the path will work in any operating system.

So our keyword implementation will be:

*** Keywords ***
Collect The Results
    Screenshot    css:div.sales-summary    ${CURDIR}${/}output${/}sales_summary.png

And our robot will now look like this:

*** Settings ***
Documentation     Robot to enter weekly sales data into the RobotSpareBin Industries Intranet.
Library           RPA.Browser
Library           RPA.Excel.Files
Library           RPA.HTTP

*** Keywords ***
Open The Intranet Website
    Open Available Browser    https://robotsparebinindustries.com/

*** Keywords ***
Log In
    Input Text    id:username    maria
    Input Password    password    thoushallnotpass
    Submit Form
    Wait Until Page Contains Element    id:sales-form

*** Keywords ***
Fill And Submit The Form For One Person
    [Arguments]    ${sales_rep}
    Input Text    firstname    ${sales_rep}[First Name]
    Input Text    lastname    ${sales_rep}[Last Name]
    Input Text    salesresult    ${sales_rep}[Sales]
    ${target_as_string}=    Convert To String    ${sales_rep}[Sales Target]
    Select From List By Value    salestarget    ${target_as_string}
    Click Button    Submit

*** Keywords ***
Download The Excel file
    Download    https://robotsparebinindustries.com/SalesData.xlsx    overwrite=True

*** Keywords ***
Fill The Form Using The Data From The Excel File
    Open Workbook    SalesData.xlsx
    ${sales_reps}=    Read Worksheet As Table    header=True
    Close Workbook
    FOR    ${sales_rep}    IN    @{sales_reps}
        Fill And Submit The Form For One Person    ${sales_rep}
    END

*** Keywords ***
Collect The Results
    Screenshot    css:div.sales-summary    ${CURDIR}${/}output${/}sales_summary.png

*** Tasks ***
Insert the sales data for the week and export it as a PDF
    Open The Intranet Website
    Log In
    Download The Excel File
    Fill The Form Using The Data From The Excel File
    Collect The Results

We run the robot. At the end of the run, a screenshot of the sales summary appears in the Notebook:

Sales summary

Done!

What we learned

  • The Screenshot keyword of the RPA.Browser library takes screenshots of elements.
  • Knowing how to use locators gets you a long way when automating web-based applications. Use the source, Luke!
  • There are many types of locators. Choose the simplest one for your use case!
  • The ${CURDIR} built in variable represents the current directory where our task file resides.
  • The ${/} built in variable represents the path separator for the current operating system.
  • The output folder is where we should put all files that our robot generates.