Selenium

Selenium Library Example

1. Introduction

Selenium is used to automate browsers. Selenium WebDriver has found its place in test automation for web applications. It is used to create robust, browser-based regression automation suites and tests. It can be used for end to end testing of an application. This ensures that the application works as designed. Selenium supports various programming languages such as C#, Haskell, Java, Javascript, Python and Ruby through Selenium drivers. Selenium supports various browsers such as chrome, firefox, safari, opera and internet explorer.

 
 
 

2. Robot Framework and Library

Selenium is generally used for end to end testing of an application. This ensures that application works as designed. But acceptance test ensures that the right things are built. Automated acceptance testing is one of the principles of Extreme programming. The Robot Framework is a test automation framework facilitating acceptance testing and in turn, acceptance test-driven development. It is a keyword driven framework(will cover below) that uses tabular test data syntax and has a modular architecture that can be extended with bundled and self-made test libraries. Selenium Library is a web testing library for Robot Framework.

2.1 Python and Library Installation

The first step is to install python in the system. Python can be obtained from the official site which has detailed instructions for each operating system. Successful installation of python can be verified by running the command

Python Version Command

python --version

The command above shows the python version installed in the machine. Once python and pip have been installed, Selenium Library and robot framework need to be installed to execute our test cases. To install, execute the below command

Python Version Command

pip install --upgrade robotframework-seleniumlibrary

This installs the latest version of robot framework along with Selenium Library needed to execute the test cases.

2.2 Keyword Driven Framework

Keyword-driven testing, also known as table-driven testing or action word based testing, is a software testing methodology suitable for both manual and automated testing. This method focuses on separating the definition of test cases and execution of text cases. As a result, it separates the test creation process into two distinct stages: a design and development stage, and an execution stage.

This specifies that a test file contains few keywords which are meaningful and are used to execute the tests. The actual definition of these keywords are handled by the framework(Selenium Library). This immediately gives a clear benefit of abstractions at all levels. It provides a clear picture of the test cases executed while abstracting the low level details. This can be achieved only by a robust library of keywords supporting major functionality of Selenium. Selenium Library also allows our own keywords to be defined which is the focus of the next section.

2.3 Google Search Testing DSL

In this section, we will use the existing keywords to define a custom keyword framework for our testing. Our test scope is limited to performing a google search on Selenium and the corresponding image search. To facilitate this, the following resource file is defined

google_resource.robot

*** Settings ***
Documentation     A resource file with reusable keywords and variables.
...
...               The system specific keywords created here form our own
...               domain specific language. They utilize keywords provided
...               by the imported SeleniumLibrary.
Library           SeleniumLibrary

*** Variables ***
${SERVER}         www.google.com
${BROWSER}        Firefox
${DELAY}          0
${SEARCH URL}      http://${SERVER}/

*** Keywords ***
Open Browser To Search Page
    Open Browser    ${SEARCH URL}    ${BROWSER}
    Maximize Browser Window
    Set Selenium Speed    ${DELAY}
    Search Page Should Be Open

Search Page Should Be Open
    Title Should Be    Google 

Input Search Term
    [Arguments]    ${search_term}
    Input Text    q    ${search_term}
    Press Keys  None    ENTER

Perform Image Search
    Click Element    	css:a[href*='tbm=isch']
  • The Settings keyword defines this is a resource file to be used by the robot framework.
  • Documentationsimply indicates meta-information about this file.
  • Library indicates the type of library to be used withRobot framework Here we are using Selenium Library to perform the testing.
  • The variables required for testing are defined under the variables section. The defined variables can be overridden by providing values for them as command line parameters.
  • The custom keyword Open Browser to Search Page is defined.
    • The first step is opening the browser specified in the browser variable using Open Browser keyword. It also takes the url to be opened as parameter along with the browser to be used.
    • The opened browser is maximized using the Maximize Browser Window keyword.
    • Set Selenium Speed is used to set the overall execution speed. It is used to introduce delays to wait for elements to appear on the page. Generally, it is not advisable to introduce delays and in our test case also, we have specified delay to be 0.
    • This utilizes another keyword Search page should be Open which is defined next.
  • Search Page Should be Open just defines an assertion to be used for our testing. It verifies wheather the browser title is Google.
  • Input Search Term is used to input the entered search text into a text box.
    • Arguments are defined for this keyword which will be provided by the calling test case.
    • Press keys is used to search for a locator and onto that element, the specified keys are passed. In our case, the locator is specified as None and the keys are passed to the browser window itself.
  • Perform Image Search keyword is defined with the following specifications
    • We use the predefined keywordClick Element. We provide a CSS locator to identify the images element and perform a click on it.

The entire keyword driven framework is custom developed and helps in executing our test cases. It is saved as robot file by convention. Important thing to note is that python has no special characters for termination and separation. Indentation and spacing is critical as these act as termination characters in robot file.

2.4 Google Search Testcases

This section is concerned with the actual tests to be executed by the team. We will discuss the first test case of just opening the google search page.

google_search.robot

*** Settings ***
Documentation     A test suite with tests for google search.
...
...               This test has a workflow that is created using keywords in
...               the imported resource file.
Resource          google_resource.robot

*** Test Cases ***
Open Search
    Open Browser To Search Page
    Search Page Should Be Open
    [Teardown]    Close Browser
  • The first few lines are similar to the previous resource file except the Resource keyword which imports the previous resource file google_resource.robot
  • TestCases indicate the next few are test cases to be executed by the robot framework
  • This uses the keywords defined in the resource file to open the google home page and verify the title.
  • As part of the test case closure, TearDown step is executed with Close Browser to close the file.

Before executing the test case, the web drivers for the required browsers should be placed in the execution path. It is advisable to download the chromedriver and gecko driver and place in the /usr/local/bin(*nix) directory so that the code can access it during execution. In Windows, the path to the drivers can be added via the environment variables from control panel.

To execute the above test case the following command can be used.robot google_search.robot. This executes the test case in firefox browser and generates reports which will be discussed in the next section. Now to switch the execution to chrome, it is as simple as executing the same command by passing parameter robot --variable browser:chrome google_search.robot. As soon as the command is run, results are also available in the terminal in which the commands have been executed. We will cover the other two test cases of performing a text search and image search at google.

google_search.robot

Search Keyword
    Open Browser To Search Page
    Input Search Term  Selenium
    [Teardown]    Close Browser

Search Image
    Open Browser To Search Page
    Input Search Term  Selenium
    Perform Image Search
    [Teardown]    Close Browser
  • Search Keyword is the next test case. We provide the search term as input – Selenium
  • The only difference in the subsequent test case is that Perform Image Search keyword is used to carry out the image search.

2.5 Errors And Reports

The execution of test cases provides detailed reports for later inference out of the box. The first report generated is log.html. It generates logs of each test case similar to the screenshot below.

Selenium Library - Log.html
Log.html

Clicking on the report in log.html navigates to a summary like page of the results similar to the screenshot below.

Selenium Library - Report Screenshot
Report Screenshot

Robot framework also generates an output.xml file which has entire details of the test executed. We also get additional details in case of error. For example if we prove a invalid selector for image search i.e as below ,We would get a screenshot of the webpage when the error happened.

google_search.robot

Perform Image Search
    Click Element    	css:a[href*='at']

The above changes lead to the error screenshot as displayed below.

Selenium Library - Error Screenshot
Error Screenshot

3. Download the Source code

Download
You can download the full source code of this example here: Selenium Library Example

Rajagopal ParthaSarathi

Rajagopal works in software industry solving enterprise-scale problems for customers across geographies specializing in distributed platforms. He holds a masters in computer science with focus on cloud computing from Illinois Institute of Technology. His current interests include data science and distributed computing.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button