Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Web and API Testing

Web testing with SeleniumLibrary

Drive a browser from Robot Framework with SeleniumLibrary: open a page, type, click, read text and assert results, with a runnable stand-in for the browser.

Most people meet Robot Framework through browser tests: open a page, fill in a form, click a button, check what appears. The classic library for this is SeleniumLibrary. It wraps Selenium WebDriver and gives you readable keywords such as Open Browser, Input Text and Click Button.

This lesson shows the shape of a web test and the keywords you will use every day. The real library needs a browser and a driver, and neither exists on this site. So the real Robot code is shown read-only, and the runnable examples are stand-ins written in Python: a tiny fake page and a few functions that behave like the keywords. They let you practice the ideas (find an element, act on it, assert) without any browser.

Install and import

SeleniumLibrary is installed with pip, next to Robot Framework itself. Recent Selenium versions download a matching browser driver for you (Selenium Manager), so a normal setup is two commands:

Terminal
pip install robotframework robotframework-seleniumlibrary
robot tests/

In a suite you import it in the *** Settings *** section, as you saw in Libraries:

TEXT
*** Settings ***
Library    SeleniumLibrary

*** Variables ***
${URL}        https://shop.example.test/login
${BROWSER}    headlesschrome

The address above is a placeholder. Use your own application under test.

A first web test

TEXT
*** Test Cases ***
Valid User Can Log In
    Open Browser    ${URL}    ${BROWSER}
    Input Text      id:username    demo
    Input Password  id:password    s3cret
    Click Button    id:login
    Page Should Contain    Welcome, demo
    [Teardown]    Close Browser

Read it as a story. Each line is one keyword: a verb followed by its arguments, separated by two or more spaces. id:username is a locator: it says "the element whose id is username". Page Should Contain is an assertion: if the text is missing the test fails and Robot records the message. The teardown runs even when the test fails, so the browser never stays open.

The four moves of a web test

Open Start a browser at a URL.
Act Type, click, select.
Check Assert text or elements.
Close Always clean up.
1 / 2

Common keywords

Keyword What it does
Open Browser Starts a browser and loads a URL
Go To Loads another URL in the same browser
Input Text Types into a text field (clears first)
Click Element Clicks anything matching a locator
Get Text Returns the visible text of an element
Element Should Be Visible Fails unless the element is shown
Title Should Be Checks the page title
Capture Page Screenshot Saves a picture and links it in the log
Close All Browsers Closes every browser the test opened

Notice the naming rule: keywords that check something contain Should, keywords that return something start with Get. Combine them and you rarely need Python at all.

A browser stand-in you can run

To practice the logic, this stand-in models a page as a dictionary of elements and implements three keywords. It is not SeleniumLibrary: it has no real DOM. Its job is to show how a keyword finds an element by locator and how an assertion keyword turns a false check into a failure.

Python Stand-in: locators and assertions

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.

Two failures show the two main reasons web tests fail: the application behaved differently (wrong password), or the test could not find something (a bad locator). Telling them apart quickly is a big part of debugging. The log of a real run tells you which keyword failed and why. See reports and logs.

Keep the keywords readable

Raw locators in test cases become hard to read and hard to fix when the page changes. Put them in variables or, better, wrap the steps in your own keyword:

TEXT
*** Keywords ***
Log In As
    [Arguments]    ${user}    ${password}
    Input Text        id:username    ${user}
    Input Password    id:password    ${password}
    Click Button      id:login

Now the test says Log In As demo s3cret, and when the id changes you edit one line. This idea grows into the page object pattern.

Use Input Password instead of Input Text for secrets. Robot hides the value in the log, so passwords do not end up in your report.

A keyword-wrapping stand-in

This second example shows a user keyword as a plain Python function that calls lower-level ones, and how arguments flow through. It repeats the idea of the real Log In As above.

Python Stand-in: your own keyword

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.

Do not run tests against a production site with real accounts. Use a test environment and test users, so a mistaken click cannot delete real data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which keyword fails the test when a text is missing from the page?

    Keywords with Should in the name are assertions: they fail when the check is false.
  2. 2. What does the locator id:username mean?

    The part before the colon is the strategy (id), the part after is the value to look for.
  3. 3. Why put Close Browser in a teardown?

    A teardown always runs, so the browser is closed whether the test passed or failed.
  4. 4. Why prefer Input Password over Input Text for a password?

    Input Password keeps the secret out of the log and report files.

Summary

  • SeleniumLibrary gives Robot Framework keywords to drive a real browser.
  • A web test opens, acts, checks and cleans up.
  • Locators like id:login say how to find an element.
  • Keywords with Should assert; keywords starting with Get return values.
  • Wrap repeated steps in your own keywords to keep tests readable.
  • The runnable code here is a stand-in; the real library needs a browser.

Next: Locators and waiting