Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Libraries and Resources

Standard Libraries Overview

Tour the Robot Framework standard libraries: BuiltIn, String, Collections, OperatingSystem, DateTime, Process, XML and more, with import syntax and name conflict rules.

Keywords are the vocabulary of Robot, and libraries are where most of them come from. Robot ships with a set of standard libraries, and thousands of external ones exist for browsers, HTTP, databases and more. In this lesson you learn how to import a library, what the standard libraries offer, and what happens when two libraries have a keyword with the same name.

Importing a library

Libraries are imported in the Settings section with Library:

ROBOTFRAMEWORK
*** Settings ***
Library    String
Library    Collections
Library    OperatingSystem
Library    DateTime
Library    Process
Library    ../libs/MyHelpers.py
Library    SeleniumLibrary    timeout=10s    AS    Browser1
  • A standard library is imported by name. BuiltIn is imported automatically and needs no line.
  • A custom library is a Python file or module; use its path or its importable module name.
  • Arguments after the name go to the library's constructor.
  • AS name gives the library an alias (Robot 6.0 and later; the older form was WITH NAME, now deprecated). An alias is handy when you import the same library twice with different settings.
  • Inside a test you can import dynamically with the keyword Import Library, though a Settings import is clearer.

The standard libraries

Library Purpose Typical keywords
BuiltIn Always available: logging, comparison, conversion, control Log, Should Be Equal, Set Variable, Evaluate, Sleep, Run Keyword If
String Text handling Convert To Upper Case, Replace String, Split String, Get Substring, Strip String, Generate Random String
Collections Lists and dictionaries Append To List, Get From List, Sort List, Get From Dictionary, Set To Dictionary, Lists Should Be Equal
OperatingSystem Files, directories, environment Create File, File Should Exist, Remove File, Create Directory, Copy File, Get Environment Variable
DateTime Dates and times Get Current Date, Convert Date, Add Time To Date, Subtract Date From Date
Process Run and control programs Run Process, Start Process, Wait For Process, Terminate Process
XML Read and check XML Parse XML, Get Element Text, Element Should Exist
Dialogs Pop-up windows asking a human Pause Execution, Get Value From User
Screenshot Take screenshots Take Screenshot
Telnet Telnet connections Open Connection, Write, Read Until
Remote Talk to a library running elsewhere Keywords of the remote library

Dialogs need a graphical desktop, so they do not work on build servers.

A taste of each

ROBOTFRAMEWORK
*** Settings ***
Library    String
Library    Collections
Library    OperatingSystem
Library    DateTime
Library    Process

*** Test Cases ***
Strings
    ${clean}=    Strip String    ${SPACE}${SPACE}robot${SPACE}
    ${parts}=    Split String    a,b,c    separator=,
    ${text}=     Replace String    I like cats    cats    robots
    Should Be Equal    ${text}    I like robots

Collections
    ${list}=    Create List    3    1    2
    Sort List    ${list}
    Should Be Equal    ${list}[0]    1
    ${dict}=    Create Dictionary    a=1    b=2
    Set To Dictionary    ${dict}    c=3
    Dictionary Should Contain Key    ${dict}    c

Files
    ${path}=    Set Variable    ${TEMPDIR}${/}note.txt
    Create File    ${path}    hello
    File Should Exist    ${path}
    ${content}=    Get File    ${path}
    Should Be Equal    ${content}    hello
    [Teardown]    Remove File    ${path}

Dates
    ${now}=    Get Current Date
    ${later}=    Add Time To Date    ${now}    1 day
    ${diff}=    Subtract Date From Date    ${later}    ${now}
    Should Be Equal As Numbers    ${diff}    86400

Programs
    ${result}=    Run Process    python    -c    print('hi')
    Should Be Equal As Integers    ${result.rc}    0
    Should Be Equal    ${result.stdout}    hi

When two libraries have the same keyword

If two imported libraries both provide Open Connection (for example SSHLibrary and Telnet), a bare call is ambiguous and Robot reports "Multiple keywords with name ... found". Fix it by prefixing the library name (or its alias):

ROBOTFRAMEWORK
SSHLibrary.Open Connection    host1
Telnet.Open Connection         host2

Resource files work the same way (common.Log In). Name conflict rules in short:

  1. A keyword with an explicit prefix is looked up only in that library or resource.
  2. Keywords defined in the same file come first, then keywords from resource files, then keywords from libraries (external libraries before the standard ones).
  3. If several matches remain at the same level, the call fails as ambiguous.
  4. Robot may log a warning when a keyword from another library hides a BuiltIn keyword of the same name; prefix the call to be explicit.

The tidy habit is to choose distinct names and to prefix when you must.

Finding keywords: libdoc

libdoc generates documentation for any library or resource file:

Terminal
libdoc String                      # prints help in the terminal
libdoc String String.html          # writes an HTML page listing every keyword
libdoc resources/shop.resource shop.html

The HTML page has a search box and shows each keyword's arguments and documentation. IDEs use the same information for autocompletion.

Stand-in 1: what the string and collection keywords compute

The blocks below are not Robot Framework. The first one shows the plain Python operations that several standard keywords wrap, to demystify them: a library keyword is just a function with a readable name.

Python Stand-in: String and Collections in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in 2: keyword name conflicts

A keyword search follows the rules above. The stand-in resolves a call among several providers, using prefixes and the "same file wins" rule.

Python Stand-in: resolving duplicate keyword names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Choosing libraries wisely

  • Start with BuiltIn, String, Collections and OperatingSystem; most data handling needs nothing more.
  • For web, HTTP, databases and SSH, pick one maintained library per area (see the Web and API chapter).
  • Do not import a library "just in case". Each import adds keyword names that can conflict.
  • When a keyword does not exist, writing a small Python library is often easier than a long Robot keyword: see Python Keyword Libraries.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which library is always available without an import?

    BuiltIn is imported automatically. The others need a Library line.
  2. 2. How do you call a keyword when two imported libraries both define it?

    A library or resource prefix selects the provider explicitly.
  3. 3. What does the AS word do in Library SeleniumLibrary AS Browser1?

    AS (Robot 6.0 and later, replacing WITH NAME) names the import so you can use it as a prefix or import twice.
  4. 4. Which tool produces an HTML list of a library's keywords?

    libdoc writes keyword documentation for libraries and resource files.

Summary

  • Import libraries with Library; BuiltIn is automatic; use AS for aliases.
  • The standard libraries cover text, collections, files, dates, processes, XML, dialogs, screenshots, telnet and remote calls.
  • Keyword name conflicts are resolved by file order and prefixes such as LibraryName.Keyword.
  • libdoc documents libraries and resources; prefer a small, focused set of libraries.

Next: Resource Files and Imports