Logging out and closing the browser

Taking out the trash

The process is now automated! Well done, you!

Let's still do some housekeeping and clean up after ourselves. Maria told us that after completing her task, she logs out and closes her browser. This might not come as a huge surprise for you anymore, but: our robot can do this thing, too!

Let's add the next (last!) step, Log Out And Close The Browser, to our task:

*** 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
    Export The Table As A PDF
    Log Out And Close The Browser

Then (you guessed it, right? 🙂) we add a new cell above the tasks one to implement our new keyword:

*** Keywords ***
Log Out And Close The Browser

So, two things. Log out. Close the browser. You already know about the Click Button keyword, so let's go with that for the logout:

*** Keywords ***
Log Out And Close The Browser
    Click Button    Log out

How do we close the browser? There's a keyword for that, too (thanks again, RPA.Browser!)!:

*** Keywords ***
Log Out And Close The Browser
    Click Button    Log out
    Close Browser

Done!?

Tearing things down

Almost. One more detail before the class is dismissed. We want to ensure that the logout and closing of the browser happens even if some part of our process fails. To do it, we use a special keyword: [Teardown] (the brackets are important!).

Let's modify our *** Tasks *** cell like this:

*** 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
    Export The Table As A PDF
    [Teardown]    Log Out And Close The Browser

What if we decided to leave out the [Teardown]? In that case, the logout and closing of the browser would not happen if one of our keywords happened to fail during execution. Using the [Teardown] keyword and giving our keyword as the argument, we ensure that no matter what, at least the cleanup step always happens. Neat!

It is done!

Our finished robot now looks 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
Library           RPA.PDF

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

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

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

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

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

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

*** Keywords ***
Export The Table As A PDF
    Wait Until Element Is Visible    id:sales-results
    ${sales_results_html}=    Get Element Attribute    id:sales-results    outerHTML
    Html To Pdf    ${sales_results_html}    ${CURDIR}${/}output${/}sales_results.pdf

*** Keywords ***
Log Out And Close The Browser
    Click Button    Log out
    Close Browser

*** 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
    Export The Table As A PDF
    [Teardown]    Log Out And Close The Browser

If we run it, we will see that it logs out and closes the browser at the end. Such a polite robot!

What we learned

  • The [Teardown] keyword is useful for ensuring that your process does not leave a mess behind. Such as an open browser.