Getting Started Selenium Automation Testing with Robot Framework
Here is the demonstration of selenium automation test case writing with Robot Framework. The test scenario is “Searching keyword in Google”.prior to the test case writing, you should set up your environment as the first post.
Step 1 — Install robot framework selenium libraries
SeleniumLibrary is a web testing library for Robot Framework.SeleniumLibrary uses the Selenium WebDriver modules internally to control a web browser. There are several keywords inside this library.
The recommended installation method is using pip:
pip install --upgrade robotframework-seleniumlibrary
Step 2 -Create the test project in Eclipse
for creating the test project we have to navigate to the below path.
File → New → Robot project → Give relevant project name → Finish
Step 3 -Create the Test Suite and Test case
As a best practice before creating the test suite better create the suite folder first.
- Create the test suite folder inside the project
Follow the below path
Right-click on Project → New →Test Suite Folder →Provide the relevant name for folder →Finish
2. Create the test suite and test cases
Right-click on folder →New →Robot test suite →provide the name →Finish
Step4 -Writing an Automation Test Case
This example will attempt to write a test case to open a browser, navigate to Google, and search for a topic.
Before we explore the various sections of the test suite, let’s create a file called “my_firsttestCase.robot”. The various sections described below such as settings, keyword definitions, etc would be added to this file.
Step 4.1 Settings
The very first step is to configure the settings at the beginning of the file. For our test case, we need to use the SeleniumLibrary. So, importing that is the only setting we need to configure.
The settings section of the test case:
***Settings ***
Library SeleniumLibrary
Note -Each section in a Robot Framework test case starts with “***” followed by the name of the block and then ends with “***”. The first line indicates that the code block that follows is the setting block. The second line uses the “Library” keyword to import the SeleniumLibrary into context.
Step 4.2 Defining Variables
The variables block helps define some constants that may be used throughout the use case. For this test case, two variables will be defined: the “HOMEPAGE” variable, which will store the URL of the website to be opened, and the “BROWSER” variable, which stores information on which browser is to be used. Given below is the code for this:
*** Variables ***
${HOMEPAGE} http://www.google.com
${BROWSER} Chrome
Set the HOMEPAGE to be google.com, use the Google Chrome browser to run the test case.
Step 4.3 Keyword definitions
Keywords in the Robot Framework work differently when compared to other programming languages. Robot wanted to make the framework easy to understand, so the keywords are human-readable descriptions. For example, SeleniumLibrary comes with a large number of predefined keywords like “Open Browser”. This keyword is responsible for opening a browser and loading a webpage.
Similarly, in a test case, one has to define custom keywords to build operations that are relevant to the use case. Here’s how to define keywords in this framework:
*** Keywords ***
open the browser
Open Browser ${HOMEPAGE} ${BROWSER}
search topic
[Arguments] ${topic}
Input Text name=q ${topic}
Press Key name=q ENTER
The code above uses the Keywords block and defines two keywords. The first keyword is called “open the browser”. It is configured to open a new browser window defined by the “BROWSER” variable and load the URL initialized in the “HOMEPAGE” variable. Here, “Open Browser” is an in-built keyword of SeleniumLibrary used to open a browser instance using Selenium Webdriver.
The second keyword defined here is “search topic”. This keyword is capable of executing the following tasks in sequence:
It accepts an argument called “topic.”
It uses the “Input Text” in-built keyword to find an element on the web page and input text.
Here, it uses a locator to find the input element where text can be entered. On the Google homepage, the text box to enter search queries has an attribute called “name” whose value is set to “q”. Here, the Input Text keyword searches for a text box with the name attribute set to the value “q”. It then types the text provided in the argument in step 1.
Finally, it uses the “Press Key” in-built keyword to mimic the user action of pressing the “Enter” key denoted by “ENTER”.
Step 4.4 Writing Test Cases
Now, use all the blocks that have been defined earlier to write test cases. Let’s break up the task into two test cases. The first test case opens the homepage on Chrome and the second test case searches for a topic on Google. Here’s the code:
*** Test Cases ***
Open Browser
open the browser
Search on Google
search topic Automation
Each test case has a generic pattern. The first line is the name of the test case, and the second line invokes the keywords defined in Step 4.3. The test passes the arguments wherever applicable.
The first automation test case is “Open browser”. It is passed if the browser opens the homepage on the mentioned browser. Otherwise, it fails. The second test case is “Search on Google”. Here, it searches for the word “BrowserStack”. If the search is complete, this test case will be marked as successful, else it is marked as a failure.
Step 4.5 Running the Test Suite
Here’s the entire test suite:
That’s about it for now.
If you are still reading this, thank you, and I hope you found this post to be of a useful start on robot framework and that I motivated you to get more hands-on experience. be practicing.
I might be expanding on this post with advanced stuff. Stay tuned and thanks for reading!