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
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:
4. Assertions
Assertions validate whether your web application behaves as expected.
|
Keyword |
|
|
|
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 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
- Switch Window accepts parameters like title, URL, or index.
- Use Switch Window index=0 to return to the main window.
b) Alerts
- Alert Should Be Present verifies the alert.
- Confirm Action accepts the alert.
- Use Dismiss Alert to cancel it.
c) Frames
- Switch into the frame using Select Frame.
- Always return using Unselect Frame.
7. Example Full Web Test (QA Feast Demo Site)
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.