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 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.Selenium!)!:

*** 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 and the robot fails to reach the end of the task. To do it, we use a special keyword: [Teardown] (the brackets are essential!).

Let's modify our *** Tasks *** section 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     Insert the sales data for the week and export it as a PDF.
Library           RPA.Browser.Selenium    auto_close=${FALSE}
Library           RPA.Excel.Files
Library           RPA.HTTP
Library           RPA.PDF

*** 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

*** Keywords ***
Open the intranet website
    Open Available Browser    https://robotsparebinindustries.com/

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

Download the Excel file
    Download    https://robotsparebinindustries.com/SalesData.xlsx    overwrite=True

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]
    Select From List By Value    salestarget    ${sales_rep}[Sales Target]
    Click Button    Submit

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

Collect the results
    Screenshot    css:div.sales-summary    ${OUTPUT_DIR}${/}sales_summary.png

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}    ${OUTPUT_DIR}${/}sales_results.pdf

Log out and close the browser
    Click Button    Log out
    Close 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 ensures your process does not leave a mess behind. Such as an open browser.

Learn more about the libraries mentioned on this page: