←back to Blog

Complete Guide to the Robot Framework Selenium Library: Everything You Need to Know

This Blog post summarises key themes and important facts from provided source excerpts related to Robot Framework’s Selenium library, focusing on browser and window management, variable scopes, implicit waits, conditional statements, handling alerts, and data-driven testing.

Browser and Window Management:

  • Keywords: Open Browser, Maximize Browser Window, Get Browser IDs, Get Browser Aliases, Switch Browser, Close All Browsers, Get Window Handles, Get Window Identifiers, Get Window Names, Get Window Titles, Set Window Position, Get Window Position, Set Window Size, Get Window Size, Switch Window, Close Window.
  • These keywords allow controlling browser instances, switching between them, and manipulating browser windows (e.g., maximizing, positioning, resizing).
  • Aliases help manage multiple browser instances effectively, especially when switching between them using the Switch Browser keyword.
  • Window management keywords offer fine-grained control over window positions, sizes, and switching between multiple windows spawned from the same browser.

Example:

*** Settings ***

Library SeleniumLibrary

*** Test Cases ***

Working With Browsers

Open Browser google.com Chrome alias=chromeRCV

Maximize Browser Window

Open Browser about:blank Firefox alias=rcvFirefox

${aliases}= Get Browser Aliases

Log ${aliases}[chromeRCV]

${browser_ids}= Get Browser IDs

Log ${browser_ids}[1]

Switch Browser index=1

Input Text id=lst-ib go to salesforce.com

Go To salesforce.com

Close All Browsers

Variable Scopes:

  • Robot Framework supports four variable scopes: Global, Test Suite, Test Case, and Local.
  • Global variables are defined in the *** Variables *** section and are accessible everywhere.
  • Test Suite variables are limited to a specific test suite and its contained test cases.
  • Test Case variables are only accessible within a specific test case.
  • Local variables are limited to the keyword where they are defined.
  • Understanding variable scopes is crucial for managing data accessibility and preventing unintended variable overrides.

Implicit Waits:

  • Keywords: Set Selenium Implicit Wait, Get Selenium Implicit Wait, Set Browser Implicit Wait.
  • Implicit wait instructs Selenium to wait for a specified duration before throwing an element not found exception.
  • Set Selenium Implicit Wait sets the global implicit wait, impacting all subsequent element searches.
  • Set Browser Implicit Wait applies the implicit wait only to the current browser instance.
  • Get Selenium Implicit Wait retrieves the currently set implicit wait value.

Example:

*** Settings ***

Library SeleniumLibrary

*** Variables ***

${default_implicit_wait}= Get Selenium Implicit Wait

*** Test Cases ***

Implicit Wait Demo

Log Default Implicit Wait: ${default_implicit_wait}

Set Selenium Implicit Wait 20s

${updated_implicit_wait}= Get Selenium Implicit Wait

Log Updated Implicit Wait: ${updated_implicit_wait}

Conditional Statements (if-else):

  • The Run Keyword If keyword implements conditional logic in Robot Framework.
  • Allows executing different keywords based on the evaluation of a condition.
  • Supports multiple Else If clauses for handling multiple conditions.
  • An Else clause can be used to define actions when all preceding conditions are false.

Example:

*** Keywords ***

Test Keyword One

Log Executed Keyword One: Found items as expected

Close Browser

Test Keyword Two

Log Executed Keyword Two: Found less than expected items

Test Keyword Three

Log Executed Keyword Three: Unexpected number of items

*** Test Cases ***

If-Else Demo

${items_on_page}= Get Element Count //div[@class=’inventory_list’]

Run Keyword If ${items_on_page} == 10 Test Keyword One

… Else If ${items_on_page} < 10 and ${items_on_page} > 6 Test Keyword Two

… Else Test Keyword Three

Handling Alerts:

  • Keywords: Handle Alert, Input Text Into Alert, Alert Should Be Present, Alert Should Not Be Present.
  • Handle Alert interacts with JavaScript alerts, accepting, dismissing, or leaving them open.
  • Input Text Into Alert types text into an alert box.
  • Alert Should Be Present verifies the presence of an alert and optionally checks its message.
  • Alert Should Not Be Present asserts that no alert is displayed.

Example:

*** Test Cases ***

Handle Alerts

Open Browser https://www.w3schools.com/js/tryit.asp?filename=tryjs_alert Chrome

Click Button xpath=//button[text()=’Try it’]

Sleep 2s

${message1}= Handle Alert

Log ${message1}

Go To https://www.w3schools.com/js/tryit.asp?filename=tryjs_confirm Chrome

Click Button xpath=//button[text()=’Try it’]

Sleep 2s

${message2}= Handle Alert action=dismiss

Log ${message2}

Data-Driven Testing:

  • Built-in Approach: Uses Test Template keyword to define a common test flow with variable inputs.
  • CSV Approach: Leverages the DataDriver library to read test data from external CSV files.
  • Both approaches improve test case maintainability and reduce code duplication.
  • The choice between the two depends on the complexity and volume of test data.

Example (Built-in Approach):

*** Settings ***

Suite Setup Common Functionality.Start Test Case

Suite Teardown Common Functionality.Finish Test Case

Test Template Invalid Login Scenarios

*** Keywords ***

Invalid Login Scenarios

[Arguments] ${username} ${password} ${error_message}

Input Text id=user-name ${username}

Input Text id=password ${password}

Click Button id=login-button

Sleep 2s

Element Text Should Be xpath=//h3[@data-test=’error’] ${error_message}

*** Test Cases ***

Blank Username and Password

${EMPTY} ${EMPTY} Epic sadface: Username is required

Logged Out User

locked_out_user secret_sauce Epic sadface: Sorry, this user has been locked out.

Wrong Password

standard_user wrong_password Epic sadface: Username and password do not match any user in this service

Example (CSV Approach):

*** Settings ***

Library DataDriver ../test_data/test_data.csv

Suite Setup Common Functionality.Start Test Case

Suite Teardown Common Functionality.Finish Test Case

Test Template Invalid Login Scenarios

*** Keywords ***

Invalid Login Scenarios

[Arguments] ${username} ${password} ${error_message}

*** Test Cases ***

Login Fails With Invalid Credentials

${username} ${password} ${error_message}

This briefing document provides an overview of essential aspects of the Robot Framework Selenium library. For a deeper understanding and specific usage instructions, refer to the official Robot Framework and SeleniumLibrary documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *