Web Automation in Robot Framework with Selenium Library

Web automation is one of the most common uses of Robot Framework, primarily achieved through SeleniumLibrary.

This section covers setup, locators, browser interactions, assertions, waits, and handling complex web elements such as windows, alerts, and frames.

1. Setting Up SeleniumLibrary

Before writing web automation tests, you need to set up Robot Framework and SeleniumLibrary.

Step 1: Install Robot Framework: pip install robotframework

Step 2: Install SeleniumLibrary: pip install robotframework-seleniumlibrary

Step 3: Download Browser Drivers

Selenium 4 can handle browsers by itself. 

     - ChromeDriver (for Chrome): https://chromedriver.chromium.org/
     - EdgeDriver (for Edge): https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Step 4: Import the SeleniumLibrary in .robot file

*** Settings ***
Library    SeleniumLibrary


2. Creating a Robot Framework Project in VS Code

     - Open VS Code.
     - Create a new folder for your project (e.g., robot-web-tests).
     - Inside the folder, create a tests directory.
     - Create a new .robot file inside tests (e.g., qa_feast_test.robot).
     - Install the Robot Framework Language Server extension in VS Code for syntax highlighting, autocompletion, and test execution.

3. Locators and Browser Interactions

Web elements are identified using locators. Robot Framework supports:

Locator

Example

Usage

id

id=username

Input Text id=username Yogesh

name

name=password

Input Text name=password 1234

xpath

xpath=//button[text()='Login']

Click Button xpath=//button[text()='Login']

css

css=.btn-primary

Click Button css=.btn-primary

class

class=loginBtn

Click Button class=loginBtn


Browser actions examples:

Open Browser    https://example.com    chrome
Input Text      id=username    demoUser
Input Text      id=password    1234
Click Button    id=loginBtn
Close Browser


4. Assertions 

Assertions validate whether your web application behaves as expected.

Keyword

Description

Example

Title Should Be

Verifies the page title

Title Should Be Dashboard

Page Should Contain

Checks if specific text appears on the page

Page Should Contain Welcome

Element Should Be Visible

Verifies element visibility

Element Should Be Visible id=logoutBtn

Element Should Contain

Verifies text inside an element

Element Should Contain id=message Success

Location Should Be

Validates the current page URL

Location Should Be https://example.com/home


Waits

Web pages may take time to load. Use explicit waits instead of hardcoded delays.

Wait Until Element Is Visible    id=logoutBtn    10s


     - Wait Until Element Is Visible waits up to 10 seconds for the element to appear.
     - Avoid Sleep whenever possible; waits are smarter and dynamic.

5. Handling Multiple Windows, Alerts, and Frames

a) Multiple Windows

Click Link    id=newWindow
Switch Window    title=New Window Title
Page Should Contain    Some Text
Close Window


     - Switch Window accepts parameters like title, URL, or index.
     - Use Switch Window index=0 to return to the main window.

b) Alerts

Click Button    id=deleteBtn
Alert Should Be Present
Confirm Action


     - Alert Should Be Present verifies the alert.
     - Confirm Action accepts the alert.
     - Use Dismiss Alert to cancel it.

c) Frames

Select Frame    id=iframe1
Input Text      id=searchBox    Robot Framework
Unselect Frame


     - Switch into the frame using Select Frame.
     - Always return using Unselect Frame.

7. Example Full Web Test (QA Feast Demo Site)

*** Settings ***
Library    SeleniumLibrary
*** Variables ***
${URL}       https://www.qafeast.com/
${BROWSER}   chrome
*** Test Cases ***
Automate QAFeast Demo Site
    Open Browser    ${URL}    ${BROWSER}
    Maximize Browser Window
    
    Wait Until Element Is Visible    id=gdpr-cookie-accept    10s
    Click Element    id=gdpr-cookie-accept
    Click Element    css:li[class='tut_ fr_tls']
    Click Link       Demo Site
    
    Wait Until Element Is Visible    xpath://h2[text()='Textbox']    10s
    Input Text       id=editabletext    Hello World
    
    Close Browser


Explanation:

     - Opens the browser and navigates to QA Feast.
     - Accepts cookies.
     - Navigates to Demo Site.
     - Waits for the Textbox section to load.
     - Types "Hello World" into the editable text box.
     - Closes the browser.
 

 

Related Tutorials