Automated testing with Robot Framework for testers

I. Introduction

1. Overview of Robot Framework

Robot Framework is a testing framework. It provides everything needed to build and develop a test script, including input / end conditions, reporting results, etc. The appeal of Robot Framework with testers is that we don't need to worry about it. consider the most basic programming algorithms. Everything we need to do is write a test script based on keywords (keyword) only.

More specifically, the Robot Framework is:

  • Framework for testing, provides testing platform for tester based on Python programming language. The approach of this testing platform is keyword driven and data driven for testing for end-to-end acceptance testing.
  • To approach this testing platform, the tester only needs to write a test script with the keyword driven and data driven data.
  • Tester can create new high-level keywords from existing ones using the same syntax used to create test cases.

The outstanding features of Robot Framework:

  • Robot Framework helps us to perform automated testing with script in table form easily. The Robot Framework delivers the results of executing test scripts and logs in html format, helping us to read and analyze the results more quickly and easily.
  • The Robot Framework supports the functionality of marking test scripts, allowing us to choose quick and convenient test scripts.
  • The biggest strength of the Robot Framework is its ability to run on many different operating systems without modifying test scripts or downstairs keywords.

2. Supported libraries in Robot Framework

The Robot Framework has many libraries that support automated testing, can refer to the library for Robot Framwwork at http://robotframework.org/#test-libraries .

7c09fc7530966020cdd4ebce92806688bee90a11

However, in this article, we will focus on introducing the two most popular libraries: Selenium2Library and Calculator Library.

2.1 Selenium2Library

  • Selenium2Library is used for Web-based testing, and is forked from SeleniumLibrary and added to use Selenium 2 and WebDriver.
  • Selenium2Library works in most popular browsers like IE, Firefox, Safari, Chrome, etc. and can be used with both Python and Jython.

To run the testcase using Selenium2Library, you first need to:

  1. Install Selenium2Library,
  2. Import Selenium2Library into testsuite Robots.
  3. Use the Open Browser keyword to enable the browser you want to use for testing.

Why should use Selenium2Library library?

Selenium2Library is a language that is very close to the user's actual language, what kind of action do you want, just type the corresponding keyword.

For example:

  • input text: enter a string of characters
  • click button: click
  • double click element: double click the element
  • get alert message: get the value of the notification
  • open context menu: open submenus
  • etc …

Learn more at:

2.2 CalculatorLibrary

Next we will learn about a library that is available in the Robot Framework, a simple computing library that contains only business logic and not the UI part.

Let's go through an example of CalculatorLibrary and run a simple test case to better understand the Robot Framework.

You can download the demo here: https://bitbucket.org/robotframework/robotdemo/downloads .

For example:

b94e442c87eff8f32e9e0c55418f3c5f7c165569

The above example includes 5 Testcases: Of which there are 4 TCs for example PASSED, and 1 TC example of FAILED:

  • Push button PASSED
  • Push multiple buttons FAILED
  • Simple calculation PASSED
  • Longer calculation PASSED
  • Clear PASSED

The keyword is quite simple and easy to understand so I do not explain anything more. Now we will run these 5 TCs with Terminal on Linux as follows:

  1. Download the library and available at: https://bitbucket.org/robotframework/robotdemo/downloads .
  2. Open the terminal and point to the folder containing the downloaded file.
  3. Run test case "keyword_driven.robot" with the following command: $ robot keyword_driven.robot .

a5fabe04a9d9d31297524951b7ba35c005bd9856

The result is 4 TCs Pass and 1 TC Failed.

Conclusion: For Robot Framework we don't need to know programming to write testcase and script like other tools. For simple and quick requirements, the Robot Framework is a suitable choice.

II. Setting

This section explains how to install RF together with Selenium on Linux and Windows.

1. Install Robot Framework

Step 1. Install Python.

First of all, because the Robot Framework is a testing platform based on Python, it is necessary to first install Python (Python 2.5 or later should be installed – recommended to install Python 2.7).

Linux: Python often comes with Ubuntu / Linux installation. To check if Python is installed, as well as its version, use the following command on Terminal (you can use the keyboard shortcut Ctrl + Alt + T to enable Terminal: $ python -version . If Python is already installed, You will see its version, such as Python 2.7.6.

Windows: Similar to Linux, turn on the cmd and check if python is installed with: python -version command. If not, go to https://www.python.org/ and download the corresponding Python version and install it.

Join the Machine Learning event - Technology of the Future!
Join the Machine Learning event – Technology of the Future!

Step 2. Install PIP (Python Package Manager).

Linux: PIP is a package manager for setting up Python packages. To install PIP, use the following command:

$ wget https://bootstrap.pypa.io/get-pip.py

$ sudo python get-pip.py

Windows: PIP has been installed if you are using Python 2> = 2.7.9 or Python 3> = 3.4 downloaded from https://www.python.org/ , however you will need to upgrade the PIP with the command after:

python -m pip install -U pip

Step 3. Install the Robot Framework package using PIP.

Linux: Use the following command to install Robot Framework:

$ sudo pip install robotframework

The latest version of Robot Framework will be installed automatically. If you want to install a specific version, just add it, for example:

$ sudo pip install robotframework == 2.8.4

After the installation is complete, use the following command to see if the installation was successful: $ pybot -version . You will see information about the Robot Framework version if successfully set up.

Windows: In the Command promt window, go to the Python installation directory and use the following command to install RF:

pip install robotframework

After successful installation, use pybot -version to check:

85a54dbbe7e47d670d9649022a42a348851f4d99

2. Install Selenium2Library

To work with Webdriver (Selenium2) and Robot Framework, you need to install Selenium2Library using PIP:

Linux: Use the following command: $ sudo pip install robotframework-selenium2library

This command will automatically install its dependencies, including decorators, and Selenium packages. As soon as the installation is complete, use the $ python command to navigate to the python window:

086ea3973c728c0a8dc436a8df6705c44611876a

In this window, type the following command to import Selenium2 Library: >> import Selenium2Library .

If no error is detected, the Selenium2 library has been successfully installed. If you want to exit the Python window, use the exit () command .

Windows: In the Command promt window, use the command: pip install robotframework-selenium2library . After successful installation will have the following message:

e0077af87537efcafad673aec2cde3c38638274d

3. RIDE Installation (Standalone RobotFramework Test Data Editor)

RIDE is an IDE for building tests using the Robot Framework. In addition to RIDE, you can replace it with SublimeText, IntelliJ or Eclipse, … Because RIDE was developed using wxPython, you need to install the wxPython 2.8 toolkit that supports unicode to run RIDE. As follows:

Linux: Use the following command to install wxPython:

$ sudo apt-get install python-wxgtk2.8

$ sudo apt-get install python-wxversion

Next, use the following command to install RIDE:

$ sudo pip install robotframework-ride

To verify whether the installation was OK yet, run the following command: $ ride.py. The RIDE application will pop up as follows:

072281cc7ba1c61ffa0c24a1f95ea0756e379105

Windows:

c92c89abad3eba6ebfaac1b799ae6387919069e0

3a74e904945698ffa13d88086b6bd922e79fc89d

  • In the Command promt window, use the command: pip install robotframework-ride to install RIDE.
  • After successful installation will have the following message

eb3219bf61baabb75d7a4a35b0a5111ebea0097c

  • Use the command: ride.py to start RIDE.

III. How to catch the interface element

1. XPath overview

  • XPath is a way to analyze HTML code to identify elements of a web driver.
  • A language that supports searching information in XML documents using XPath expressions to orient the search for data on XML instead of performing a recursive search to browse the XML tree.
  • XPath defines 7 types of nodes according to the model shown below from root, element, attribute, text, namespace, processing-instruction and comment.
  • In addition, XPath defines some special nodes to show the relationship between the nodes in the model in the process of processing as follows:
  1. Parent Node: the direct upper node of the current node.
  2. Child Node: the direct node of the lower-level current node.
  3. Sibling: peer node or the same parent as the current node.
  4. Ancestors: all child nodes above the current node are in the same branch.
  5. Descendants: all sub-nodes below the current node are in the same branch.
Join the Machine Learning event - Technology of the Future!
Join the Machine Learning event – Technology of the Future!

XPath syntax:

  • For an absolute path query to go from the root of the XML document to the components to access, the XPath specified with the syntax starts with a /
  • For a query with a relative path to be able to access any part of the condition, the XPath specifies the syntax to use with the //
  • To query for any element without knowing its name, the XPath specifies the character to be used *.
  • To access the properties of a node, the XPath specifies that the query attribute must have a syntax starting with @.
  • Condition when query is placed in []
  • Query select nodes

Expression

Define

Name Node

Select all child nodes of the node name.

/

Select all nodes from root.

//

Select all nodes counted from the current node.

.

Select the current node.

..

Select the parent node of the current node.

Math operations used in XPath:

  • Algebra: +, -, * (multiply), div (decimal division), mod (divide by balance)
  • Compare or relate: =,! = (Other), <, <=,>,> =
  • Reason: true, false, and, or, not
  • Combination: | (Assembly)

2. How to catch XPath with Firebug and FirePath

Firebug and FirePath are two add-ons that support fast and easier XPath capture in Firefox browsers.

Install Firebug and FirePath:

  1. In Firefox browser, select the icon [Open Menu] -> Add-ons.
  2. Find and install Firebug, FirePath.

After successful installation, Firebug and FirePath appear in Extensions section and their icons will appear in Firefox's toolbar.

2b510b64cc5c74744898385a5d3c4895ab138f7f

Use Firebug and FirePath:

  • Click the bug icon -> select the FirePath card.
  • Click the arrow symbol next to the bug -> click on the element to get the xpath. The xpath of that element will show:

d1fbc9c3854ef35c3353076e1ba16bd7c621a5da

Note:

  • XPath obtained from FirePath is for reference only and helps users identify elements more easily.
  • XPath obtained from FirePath is the simplest way, but does not guarantee stability and only when the version of the web page changes. Therefore, users can use several ways to support the following query to capture XPath to increase stability and availability when they want to use elements that relate to each other more accurately:

Axis

Define

ancestor

Select all the nodes on the current node.

ancestor-or-self

Select all nodes on the current node and itself.

attribute

Select all properties of the current node.

child

Select the child node of the current node.

descendant

Select all the nodes below of the current node.

descendant-or-self

Select all the nodes under the current node and itself.

theo sau

Select all nodes after the closing tag of the current node.

following-sibling

Select all horizontal nodes after the closing tag of the current node.

namespace

Select all namespaces of the current node.

parent

Select all parent nodes of the current node.

preceding

Select all components before starting the current node's open tag.

preceding-sibling

Select all peer nodes before starting the open tag of the current node.

self

Select the current node.

For example:

VD1: ancestor

  1. Show all parent tags that contain div tags id = <"identifier-shown">, not including the div tag id = <"identifer-shown"> checked from the Login Gmail page.

fb1b340719938f5b70b42e784bf1d152f32404ab

  1. If only want to display the specified parent tag.

347ef0cedd9f6ff09d2cfba5d6a3db99e50b6898

Vd2: ancestor-or-self

Displays the parent tags and the div tags id = <"identifier-shown">, including the div id = "<identifier-shown>" tags checked from the Login Gmail page.

278d8b8f2a7b923f6b3c28327e8823d4741457e2

Vd3: attribute

Select all the current attributes of the Next button on the Login Gmail page.

b5ad5ea2eab13aed28dea354e9437a6a3032919b

Vd4: child

Select all child tags of div class = "input-wrapper focused" tags.

6152b5513962f8991e981c2b0f2cbe8575b96257

Vd5: descendant

Select all child and grandchildren tags of id = "input-wrapper focused" div, not including the id = "input-wrapper focused div" tags.

96aa0fe73cb7df41d982193eb3b52dc4bbc60eeb

VD6: descendant-or-self

Select all child and grandchildren tags of id = "input-wrapper focused" div, including id = "input-wrapper focused" div tags.

374b38776e68e15f57e36fd056c93f70f6f47757

Vd7: following

  1. Show all tags after the closing tag of class = "identifier-wrapper div".

f82c138b5de9639827b4a1a9230339c221f390ba

  1. Display 1 card after the closing tag of class class = "identifier-wrapper div".

9566b8874419e55d44286a49173ddf7cd713a883

Vd8: following-sibling

Display sibling tags after the closing tag of div class = "input-wrapper focused" tags.

ca6db17bf580a6fc4d1afbb2ffc7759414d04c8c

Vd9: parent

Select all parent tags of div class = "input-wrapper focused" tags.

16d6dcb79d278326f232eee009cfbe09f898f5d1

Vd10: preceding

Join the Machine Learning event - Technology of the Future!
Join the Machine Learning event – Technology of the Future!

Select all tags before input id = "next", except for ancestors and attributes.

b328b438090e7a67f184b8973b6b954530b9d395

Vd11: contains ()

Select all elements containing text as "Create".

db1ce283d67472980df94cf6a90833ebe736b22a

IV. Demo

Context: Log in to http://www.chatwork.com/ successfully.

Implementation steps:

Step 1. Create a new Project.

  • i. Click File -> New Profile .
  • ii. Enter Name and select Type – Directory, Format – HTML.

211ec6ddfbb1457c6d91357e968da51ff864779c

Step 2. Create Interface.

  • i. Right-click and select Project folder and select Test Suite.

a75a1823bf1388333107559ca6b994df0fa5eda6

ii. Name "Interface".

0530a02b087c4672f7252509de509e2a8c87ddf5

iii. Right-click and choose Interface -> New Resource folder.

562c945bba4f5e3da63be958a413d6f2940b929a

bcfa6c3d571a0987751ec8afd4936b0e3f1020bb

iv. Right click on Interface resource -> New Scalar .

d988bc7f6a51bed3d96e8202bfb69cf5bba18661

337f06616c72772a4fcdf662e1e8e9a625f9b61f

Step 3. Create New Action.

  • i. Right-click and choose Project Name -> New Suite .
  • ii. Create a new "Action" suite.

273b7ee418507ff200fa1c86a1ba1862e256cf38

iii. Similar to Interface, create New Source for Action by right clicking on "Action".

e2718e1f236521b4a4b718515505604f222ce433

1d1be3475039d40ae42c3f63572545128b1331ae

iv. Add a New User Keyword.

c8d46e495b8f5950bae913cf471aea9828d1b6a5

b0d36811a6a32695ea6475acf74008c3b6b67089

v. Add Selenium2Library for each action.

a7693ebb2f54ea76a63d8d8a0c28150b2a47c07b

vi. Import Interface of the corresponding page by clicking Resource and pointing to the folder containing Interface.

2382ecdba71ea77a1292701f1e9fe93a7256247a

vii. Create test steps:

dccb5b180a148c84e80df86b0af263a09d9fd5c8

Step 4. Create Testsuite.

  • i. Right-click and choose the project folder, select "New Suite". Create Test Suite with txt File.

a10719a6e9e839600ff05d04ef1ea03215334f40

e22a1270422cc84de7dc71f6394a32097e641ce8

ii. Add Library and Resource.

8b4e0e02719a6413c7e314dbcdeb60310da347ee

Step 5. Create a Testcase.

  • i. Right-click on the newly created testsuite, select "New Test Case". Then fill in the name Test case.

e6ef5e416f1ca33cef7966fd0b5427c22a330394

284310247cf64ebe4c844d291836fc06eb897a5f

  • ii. Write scripts for testcase.

0bdb909df2a546a4ac483f0de1554d7125f914db

Step 6. Execute testsuite.

d57e08d43866db4c2b1f0e55a629f8a0d4363ff4

Join the Machine Learning event - Technology of the Future!
Join the Machine Learning event – Technology of the Future!

Step 7. Check the results.

e7f0ef58678c18a5b97a60c8acd846d5db588259

f8da3fd9b4bf31c6e5d335ef0a9da96f099c8370

9dbf74f16b6f43d9f5f288b6a647d3bb3ec9b686

V. References

ITZone via Viblo

 

Share the news now