Robot Framework – Test Automation the Smart Way!

Robot Framework – Test Automation the Smart Way!

TABLE OF CONTENT

  • ROBOT FRAMEWORK
  • ROBOT FRAMEWORK LIBRARIES
  • SCRIPT READABILITY
  • CENTRALIZE DATA BY VARIABLES
  • SCRIPT RUNNING OPTIONS
  • REPORTING

ROBOT FRAMEWORK

Robot framework consists of a set of tools, techniques and abstract rules; its job (besides allowing to write automated test cases) is simplifying the test automation process. In practice, Robot is a modular test automation framework that has the capability to interact with 3rd party libraries and functions.

Robot framework comes with solid functionality and 3rd party libraries that others have already implemented; such as writing logs and reports, capturing screenshots, integration with continuous integration tools, support for KDT, simultaneous running and of course a connection to a variety of external. All these great features allow us to focus on our product testing, instead of investing a lot of time in reinventing the wheel.

Robot Framework is a generic test automation framework for acceptance testing and acceptance test-driven development (ATDD). Its core framework is written in Python, but also supports IronPython (.NET), Jython (JVM) and PyPy as well.

  • Keyword-driven approach to simplify tests and make them readable
  • Reusable higher-level keywords can be created from existing ones
  • Easy-to-use tabular test data syntax
  • Cross-platform
  • Rich ecosystem including a variety of APIs consisting of generic test libraries and tools that can be developed as separate projects
  • Highly extensible natively – using Python and Java libraries and by the means of different API’s
  • Can be extended natively using Python or Java
  • Other languages supported via a remote interface
  • Clear testing reports
  • Detailed logs
  • Easy integration

ROBOT FRAMEWORK VS SELENIUM WEB DRIVER

Robot framework or Selenium Web driver? A question frequently asked in professional forums, the answer is simple- “This question is irrelevant”, there is no sense in comparing the two.

Selenium is a library (some call it a web driver) and Robot is a test automation framework that uses libraries. With Robot, you can run a variety of automated tests; Both UI element-based and API tests, you can run a test with the help of the math library and a test that validates if the element exists on the page, with the assistance of Selenium Web driver.

With selenium, you can create UI based functional test cases, but in order to execute them, you’ll need a test automation runner or alternatively, an automation wrapper. There are several popular test runners available out there, for instance: MSTest, TestNG, NUnit, JUnit, etc.

No alt text provided for this image
No alt text provided for this image

ROBOT FRAMEWORK VS TESTNG

No alt text provided for this image

ARCHITECTURAL VISION

Robot Framework is a keyword-driven framework but has data-driven capabilities too. Robot Framework depends upon libraries. Robot Framework is in fact an abstraction or a wrapper layer on Selenium.

No alt text provided for this image

HOW DOES ROBOT WORKS

The following diagram describes the architecture of the environment, the orange rectangles represent the connection to the libraries (in this case to Selenium and the Database)

No alt text provided for this image

ROBOT FRAMEWORK LIBRARIES

libraries provide the actual testing capabilities to Robot Framework by providing keywords. There are several standard libraries that are bundled in with the framework, and galore of separately developed external libraries that can be installed based on your needs. Creating your own test libraries is a breeze.

No alt text provided for this image

BUILT-IN LIBRARY

It provides a set of often needed generic keywords. Always automatically available without imports. The provided keywords can be used, for example, for verifications (e.g. Should Be Equal, Should Contain), conversions (e.g. Convert to Integer) and for various other purposes (e.g. Log, Sleep, Run Keyword If, Set Global Variable).

  • HTML error messages
  • Evaluating expressions
  • Boolean arguments
  • Multiline string comparisons
  • Shortcuts
  • Keywords

DIALOGS LIBRARY

Provides means for pausing the test execution and getting input from users.

  • Get Selection from User
  • Execute Manual Steps
  • Pause Execution
  • Set Global Variables

STRING LIBRARY

The string is a Robot Framework’s standard library for generating, modifying and verifying strings and manipulating strings (e.g. Replace String Using Reg expression, Split to Lines) and verifying their contents (e.g. Should Be String). Following keywords from the built-in library can also be used with strings:

  • Catenate
  • Get Length
  • Length Should Be
  • Should (Not) Be Empty
  • Should (Not) Be Equal (As Strings/Integers/Numbers)
  • Should (Not) Match (Reg expression)
  • Should (Not) Contain
  • Should (Not) Start With
  • Should (Not) End With
  • Convert to String
  • Convert to Bytes

OPERATING SYSTEM LIBRARY

Operating System is Robot Framework’s standard library that enables various operating system related tasks to be performed in the system where Robot Framework is running. It can, among other things, execute commands (e.g. Run), create and remove files and directories (e.g. Create File, Remove Directory), check whether files or directories exist or contain something (e.g. File Should Exist, Directory Should Be Empty) and manipulate environment variables (e.g. Set Environment Variable).

  • Path separators
  • Pattern matching
  • Tilde expansion
  • Boolean arguments
  • Shortcuts
  • Keywords
  • List/Move/Copy/Create File
  • Create Directory
  • Join Directory Paths


DATABASE LIBRARY

Database Library contains utilities meant for Robot Framework's usage. This can allow you to query your database after an action has been made to verify the results. There are some new keywords available with this release of the Database Library. Those are listed in the following. Details can be found from the Library Documentation.

  • Store Query Result to File
  • Compare Query Result To file
  • Execute SQL From File
  • Execute SQL From File Ignoring Errors
  • Row Should Not Exist in Table

API LIBRARY

Robot Framework test library for HTTP JSON APIs.

SELENIUM LIBRARY

Web testing library that uses popular Selenium tools internally.

Libraries: https://robotframework.org/robotframework/

No alt text provided for this image

SCRIPT READABILITY

Readability

  • Chunky vs Chatty approach
  • Locators should be stored in the variables so that the result log looks intuitive

Flexible

  • Should be able to change the Browser & URLs easily

Reusable

  • Sensible Page Object keywords
  • Add (Documentation, Tags, Logs, Sleep) in Test Cases as a good practice

Maintainable

  • All the web locators should be encapsulated in the Page Object files
No alt text provided for this image

PROCEDURAL VS GHERKIN

Frameworks such as the Robot Framework allow for high-level, business-readable, BDD-style test designs or specifications. These BDD-specific features thus add to the frameworks the capability of facilitating specification and collaboration.

No alt text provided for this image
Gherkin Style

·        READABILITY

No alt text provided for this image

BREAKING SCRIPT INTO KEYWORDS

WHY

  • Keywords can be reused by multiple test cases
  • Improves our understanding of script intention
  • Improves our ability to decipher the test results

HOW

No alt text provided for this image

Add line breaks and comments to scripts to determine which steps go together

No alt text provided for this image

Create a Keyword for each comment

No alt text provided for this image

Move the lines under each comment to the respective keyword

No alt text provided for this image
No alt text provided for this image

MOVING KEYWORDS TO RESOURCE FILE

No alt text provided for this image

Create the Resources file for declaring the Keywords

No alt text provided for this image

Copy Keywords from script to Resource file

No alt text provided for this image

Remove Keyword section from the Script file

No alt text provided for this image

Remove SeleniumLibrary declaration from the Script file and add into Resource keyword file

No alt text provided for this image

Add reference of the Resources File into Script file

No alt text provided for this image

Note:  ../Resources/Resource_FileName annotation is used to refer any file within Resources directory

MOVING LOCATORS TO PAGE OBJECT FILE

WHY

  • One place for logics & locators
  • Reusable by many test cases & keywords
  • A fix in one place corrects many test cases
No alt text provided for this image

HOW

Create the Page Object file for declaring the Keywords

No alt text provided for this image

Copy Locators from script to Page Object file

No alt text provided for this image

Remove SeleniumLibrary declaration from the Resource file and add into Page Object file

No alt text provided for this image

Call page object methods from Keywords

No alt text provided for this image

Add reference of the Page Object File into Resource file

No alt text provided for this image

Note:  ./PO/Resource_FileName annotation is used to refer any file within PO directory

ADDING SETUP & TEARDOWN FUNCTIONS

No alt text provided for this image

HOW

Create the Resources file for declaring the Keywords

No alt text provided for this image

Copy Keywords from script to Resource file

No alt text provided for this image

Remove SeleniumLibrary declaration from the Script file and add into Resource keyword file

No alt text provided for this image

Add reference of the Resources File into Script file

No alt text provided for this image

Declare reference of the Setup & Teardown keywords under Settings

No alt text provided for this image

Note:  ../Resources/Resource_FileName annotation is used to refer any file within Resources directory

CENTRALIZE DATA BY VARIABLES

WHAT IS VARIABLE

A named ‘bucket’ to store information

No alt text provided for this image

WHY

  • Centralize input data
  • Reduce code repetition within the script
  • Enable different input values at run time

TYPES OF VARIABLE

  • SCALAR VARIABLE

Scalar variables hold single value e.g. first name, credit card number or browser.

Syntax for Setting   ${Variable_Name}

For example

  1. ${Variable_Name} = ABC
  2. ${Variable_Name} = Set Variable ABC

Syntax for Retrieving

  1. Log ${Variable_Name}
  • LIST VARIABLE

List variables hold multiple values e.g. username, password or series of coordinates.

Syntax for Setting   @{Variable_Name}

For example

  1. @{Variable_Name} = ABC    DEF    XYZ
  2. @{Variable_Name} = Set Variable ABC    DEF    XYZ
  3. @{Variable_Name} = Create List ABC    DEF    XYZ

Syntax for Retrieving

  1. Log @{Variable_Name} [0]
  2. Log @{Variable_Name} [1]
  3. Log @{Variable_Name} [2]

VARIABLE SCOPE/PRECEDENCE

No alt text provided for this image

Naming Convention

  • Global: ${UPPER_CASE}
  • Test Case: ${lower_case}
  • Keyword: ${lower_case}

Precedence

A variable has a cascading order of precedence that can be overridden in various locations

No alt text provided for this image

MODIFY PROJECT WITH VARIABLES

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

RUNTIME VARIABLE INPUT

No alt text provided for this image
  • Syntax

-v Variable Name: New Value

  • For Example

robot -d results tests/Script file -v SEARCH_TERM: Hand Bags

SCRIPT RUNNING OPTIONS

Running Test Scripts from PyCharm

  • How to Run all the Test Scripts within Tests Directory?

Example: robot -d results tests

  • How to change the Report file name?

Example: robot -d results -N “FileName” tests

Note: -N is used to Title/Name your test result Reports

  • How to Run Test Scripts within Smoke Suites only?

Example: robot -d results -N “FileName” “tests/Smoke Suite”

  • How to Run Test Scripts with specific Tags?

Example: robot -d results -N “FileName” -i Smoke tests

  • How to Run Single Test from a specific Suite?

Example: robot -d results -t “Test Case 1” “tests/Smoke Suite/Directory (Optional)/FileName.robot”

Note: -t is used to run specific test

Running Test Scripts from Command Prompt

  • Open Command Prompt
  • CD to Project Directory
  • Execute Robot script

Example: C:\developemnt\robot-scripts\amazon> robot -d results -v BROWSER : ie tests/amazon.robot

Note: You can also pass browser variable

Running Test Scripts from Batch File

  • Open Text File
  • Execute Robot Script
  1. @echo off
  2. Cd [Directory]
  3. Call Script

Example: C:\developemnt\robot-scripts\amazon> robot -d results -v BROWSER : ie tests/amazon.robot

Note: You can also pass browser variable

  • Change file exe .txt to .bat
  • Double click on the file to execute

Note:

  1. Create a folder named Batch within C drive
  2. The filename should not contain spaces

Running Test Scripts from Task Scheduler

  • Open Tash Schedular
  • Navigate to Task Schedular library
  • Create a New Folder [Robot Framework]
  • Robot Framework [Create New Task]
  • Name Task File
  • Actions Tab
  • Click on New
  • Provide an absolute path of Batch File
  • Run File

Note: Set script execution time from Trigger Tab

REPORTING

LOGGING & FAILURE ANALYSIS:         

If you are used to Java, then it is easy. However, you don't get the logs as good/cool as Robot. The screenshot is not captured by default unless you implement some logic. You need to use Log4j for detailed logging. Whereas neat and clean logs and reports come with a screenshot via Robot Framework.

I do need to mention that the reporting system for this environment is not the most visually impressive I’ve ever seen. But, it’s clear, user-friendly, provides all the required information (including screenshots) and doesn’t take any effort to implement. Here is the example of a test automation output (a screenshot of a log and a screenshot of a report):

A case of a test that passed

No alt text provided for this image
No alt text provided for this image

A case of a test that failed

No alt text provided for this image
No alt text provided for this image

To summarize, the reporting system, in my opinion, symbolizes Robots in terms of how easy it is to generate it. The example presented above demonstrates how opening a browser in the Robot requires only a single line of code (instead of 5 in Selenium), logs and reports that are created automatically when executing tests, without the need to write even a single line of code. Therefore, in my opinion; Robot Framework is probably one of the most worthwhile test automation frameworks available in the market nowadays.

Khazima Irfan

Sr. Quality Assurance Engineer

4 年

Great work mentor!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了