Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 36 ยท Web and API Testing

Browser Library (Playwright)

Meet Robot Framework Browser Library, built on Playwright: auto-waiting, contexts and pages, text selectors, and how it compares with SeleniumLibrary.

Browser Library is the modern alternative to SeleniumLibrary. It is built on Playwright and a Node.js back end. Its biggest selling point is auto-waiting: before it clicks or types, it waits until the element exists, is visible and is ready. That removes most of the timing code you saw in Locators and waiting.

Neither Playwright nor browsers can run here, so the real Robot code is read-only and the runnable examples are Python stand-ins for the underlying ideas (browser, context, page and auto-waiting).

Install

Terminal
pip install robotframework-browser
rfbrowser init        # downloads Node dependencies and browsers

rfbrowser init is a one-time step that fetches the browser binaries. After it, you import the library:

TEXT
*** Settings ***
Library    Browser

A first test

TEXT
*** Test Cases ***
Search Shows Results
    New Browser    chromium    headless=True
    New Page       https://shop.example.test
    Fill Text      css:input[name=q]    laptop
    Click          text=Search
    Get Text       css:.count    ==    3 results
    [Teardown]     Close Browser

Compare with SeleniumLibrary. Fill Text and Click need no preceding wait. And Get Text takes an assertion operator (==) plus the expected value, so one keyword both reads and checks. That "assertion engine" is a nice feature: Get Text css:.count contains results or Get Element Count css:.row > 0.

Browser, context, page

Browser One browser process.
Context Isolated profile, like incognito.
Page One tab inside a context.

Each test normally gets a fresh context, so cookies never leak between tests.

1 / 2

Browser, context and page

Browser Library has three levels, and understanding them saves headaches:

  • Browser: the program (Chromium, Firefox or WebKit).
  • Context: an isolated session with its own cookies and storage. Two contexts can be logged in as two different users at once.
  • Page: one tab.
TEXT
New Browser    chromium
New Context    viewport={'width': 390, 'height': 844}
New Page       https://shop.example.test

Starting a context with a phone-sized viewport is how you test responsive layouts. New Context can also set locale, timezone, a saved login state (storageState), or record a video and a trace.

Selectors

Playwright selectors are powerful and Browser Library accepts them directly:

Selector Meaning
css=.btn or just .btn CSS selector
id=login Element id
text=Sign in Element containing the text
"Sign in" (quoted) Exact text match
xpath=//h1 XPath
css=article >> text=Buy Chain: Buy inside an article
role=button[name="Save"] By accessibility role and name

Selecting by role and name is a good habit: it tests what users and screen readers see, and it survives markup changes.

Selenium or Browser?

SeleniumLibrary Browser Library
Engine WebDriver Playwright
Waiting Manual (explicit waits) Automatic
Speed Good Usually faster
Needs Browser and driver Node.js and rfbrowser init
Assertions Separate Should keywords Built into Get ... keywords
Old browsers Wide support Chromium, Firefox, WebKit
Isolation New browser per test New context per test (cheap)

For a new project with modern browsers, Browser Library is the usual recommendation. SeleniumLibrary remains the safe choice for large existing suites and for grids that already run WebDriver.

Auto-waiting stand-in

The runnable stand-in below mimics what Playwright does before a click: it polls a list of checks and only then acts, with a clear error naming the check that never passed. It uses a fake element whose state changes on each poll, so the output is deterministic.

Python Stand-in: auto-waiting before a click

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.

The first run polls three times and clicks. The second runs out of polls and reports exactly what was missing. Real Playwright works the same way, with a default timeout of 10 seconds in Browser Library.

Contexts keep tests independent

Here is the isolation idea in code: each "context" has its own cookie jar, so a login in one never shows up in another.

Python Stand-in: isolated contexts

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.

Log in once, save the state with Save Storage State, and start later contexts from it. A test suite that logs in through the UI in every test spends most of its time on the login page.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is Browser Library built on?

    Browser Library uses Playwright with a Node.js back end.
  2. 2. What does auto-waiting do?

    Before clicking or typing, Playwright waits for the element to be visible, stable, enabled and uncovered.
  3. 3. What is a browser context?

    Contexts work like separate incognito profiles, which keeps tests independent.
  4. 4. What does Get Text css:.count == 3 results do?

    The assertion operator lets one keyword read and check in a single step.

Summary

  • Browser Library is Robot's Playwright-based web library; install it with pip and rfbrowser init.
  • It auto-waits, so you write fewer explicit waits.
  • Browser, context and page are the three levels; use a new context per test.
  • Get ... keywords take assertion operators.
  • Prefer role and text selectors; choose SeleniumLibrary for existing WebDriver suites.

Next: API testing with RequestsLibrary