Transcripted Summary

Let's talk a little bit about the settings section.

I'm going to go into my tour-of directory and open up tour-of.robot because it's got an example of a nice big setting section.

# Example Code - tour-of.robot


*** Settings ***
Library  SeleniumLibrary
Library  OperatingSystem
Library  Collections

Resource    resources.robot
Resource    invoice-details-page.robot
Resource    navigation.robot
Resource    system.robot
Resource    data.robot

Suite Setup  Run Keywords   Initialize Test Data    Configure Selenium   Navigate To Homepage
Suite Teardown  Exit Selenium

*** Test Cases ***
Create An Invoice
    ${invoice}=     Get Dummy Invoice   demo
    Navigate To Add Invoice
    Fill Out Invoice Details    ${invoice}
    Submit Invoice Form
    ${invoice_id}=   Get Invoice Id     ${invoice}
    Page Should Contain     ${invoice_id}
    Open Invoice    ${invoice_id}

# Settings - Library

We've already talked about the Library keyword and how we can use preexisting libraries full of keywords in our test cases. The Library keyword can pull in preexisting ones.

Your developers can make libraries for Robot Framework. You could even pull in a Python package and use it here.

Any publicly exposed method or function within Python can be exposed as a keyword by pulling it in with the Library keyword here.


# Settings - Resource

Resources, these are the files that you build. You may have files that have Keywords in them. You may have some that have Variables in them.

There are lots of different ways to use the Resource keyword but it helps you keep your keywords in certain groups.

For instance, in this case, we've grouped together all the navigation keywords into a file called navigation.robot.


*** Keywords ***
Navigate To Add Invoice
    Click Element   css:[href="#/addInvoice"]

Open Invoice
    [Arguments]    ${id}
    Click Link    css:#invoiceNo_${id} > a

We've got basically a Page Object in our invoice-details-page.robot.


*** Keywords ***
Enter Invoice Number
    [Arguments]    ${invoice_id}
    Input Text  css:[ng-model="invoice.invoiceNo"]    ${invoice_id}

Enter Company Name
    [Arguments]    ${company_name}
    Input Text  css:[ng-model="invoice.companyName"]    ${company_name}

Enter Type of Work
    [Arguments]    ${type_of_work}
    Input Text  css:[ng-model="invoice.typeOfWork"]    ${type_of_work}

Enter Amount
    [Arguments]    ${amount}
    Input Text  css:[ng-model="invoice.price"]    ${amount}

Select Status
    [Arguments]     ${status}
    Select From List By Value   css:[ng-model="invoice.status"]   ${status}

Enter Due Date
    [Arguments]     ${due_date}
    Input Text   css:[ng-model="invoice.dueDate"]   ${due_date}

Enter Description
    [Arguments]     ${description}
    Input Text   css:[ng-model="invoice.comment"]   ${description}

Submit Invoice Form
    Click Button    createButton

Fill Out Invoice Details
    [Arguments]    ${invoice}
    Enter Invoice Number    ${invoice["id"]}
    Enter Company Name  ${invoice["company_name"]}
    Enter Type of Work  ${invoice["type_of_work"]}
    Select Status  ${invoice["status"]}
    Enter Amount  ${invoice["amount"]}
    Enter Due Date  ${invoice["due_date"]}
    Enter Description   ${invoice["description"]}

We've got system keywords in system.robot.


*** Keywords ***
Configure Selenium
     Set Selenium Speed    .25 Seconds

Navigate To Homepage
    Open Browser    ${SiteUrl}    ${BROWSER}
    Maximize Browser Window

Exit Selenium
    Capture Page Screenshot
    Close Browser

Generate Random Number
    ${random_number}    Evaluate    random.randint(1000000, 9999999)   random
    [return]    ${random_number}

And we've got our data that we use in data.robot.


*** Keywords ***
Read Invoice Fixtures
    ${json_data}=     Get File    ${EXECDIR}/invoice-fixtures.json
    ${json}=    evaluate    json.loads('''${json_data}''')    json
    [return]  ${json}

Initialize Test Data
    ${json}=    Read Invoice Fixtures
    set global variable  ${Invoices}    ${json}

Get Dummy Invoice
    [Arguments]    ${invoice_name}
    ${invoice}=  Get From Dictionary     ${Invoices}     ${invoice_name}
    Randomize Invoice Id    ${invoice}
    [return]    ${invoice}

Randomize Invoice Id
    [Arguments]    ${invoice}
    ${random_number}=  Generate Random Number
    Update Invoice Id   ${invoice}    ${random_number}

Update Invoice Id
    [Arguments]    ${invoice}   ${id}
    set to dictionary  ${invoice}   id=${id}

Get Invoice Id
    [Arguments]    ${invoice}
    ${invoice_id}=   Convert To String   ${invoice["id"]}
    [return]  ${invoice_id}

There are a lot of other cool things you can do in the Setting section.

Make sure to look at robotframework.org and the documentation there to learn more about different keywords you can use in the Settings section.



Some of my favorites are Suite Setup, Suite Teardown, Test Setup and Test Teardown.

The Suite Setup allows you to run specific keywords prior to this entire suite running.


Suite Setup  Run Keywords   Initialize Test Data    Configure Selenium   Navigate To Homepage

Here, I've got a keyword called Run Keywords allowing me to run more than one keyword at a time.

Then Initialize Test Data, which is one of my custom keywords in one of the resource files. Configure Selenium is another one of these that I've created, and Navigate To Homepage.


We run those in order from left to right prior to this suite or this file running.

So, before any test cases, we do those 3 steps. After all the test cases are done, we do Exit Selenium using this Suite Teardown.


This is a little bit different in terms of how control flow works within Robot Framework.

We previously said that control flow works by executing the test case with the Variable section first, the Settings section second, the Test Cases third. This breaks that rhythm. It breaks that paradigm.

  • Suite Setup gets run prior to the test cases.
  • Suite Teardown runs after all test cases.

But both of those keywords are in the Settings section.

I know that's a little confusing. Take a look at the documentation for different explanations that might work better for you.


The Settings section is basically something you can think of as your configuration, your steps before the test case, your preconditions.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR