Introduction and application of the RobotFramework automated testing framework compiled by Byte's ten-year engineer

Table of contents

1. Concept

Two, characteristics

3. RF environment installation

Fourth, the use of RF

Five, RF common class library

Six, the use of common keywords in RF

7. Prepare the environment for UI automation testing

8. Keyword for browser operation

9. Element positioning

10. The three-tier structure of the project

11. RF non-GUI mode (command line) running and Jenkins integration

1. Concept

What is Robot Framework?

Robot Framework is a functional automation testing framework written in python. It has good scalability, supports keyword-driven, can test multiple types of clients or interfaces at the same time, and can perform distributed test execution. Mainly used for acceptance testing and acceptance test driven development (ATDD) with many rounds.
 

Two, characteristics

1. Test cases are saved in text files (TXT or TSV files), and tabs are used to separate data. You can easily use any text editor, or EXCEL to edit test cases. Use cases can also be created in HTML format.

2. The use of variables is supported in test cases, and IF statements and FOR loop statements can be used.

3. You can use the "label" function to classify and selectively execute test cases.

4. Support keyword-driven, data-driven and behavior-driven.

5. Using existing keywords, testers can create keywords they need to form higher-level behaviors.

6. Test execution reports and logs are in HTML format, easy to read.

7. Robot Framework is not an automated testing tool like QTP. It is an automated testing framework, or an automated testing platform.

8. Provides a listening interface for test execution events, and can customize the scripts in the interface. For example, before a use case is executed, the script in the "start_test" interface will be executed; after the use case is executed, the script in the "end_test" interface will be executed. Testers can customize the scripts of the two interfaces "start_test" and "end_test".

9. Provides a command line interface and output in XML format. It can be combined with version management tools for continuous integration.

10. Robot Framework identifies and manipulates the tested object through the test library. There are many built-in or third-party open source test libraries. For example, use the "selenium2Library" library to test the web client. In addition, you can also test java client, Win32 client, character terminal of SSH protocol, etc.

11. Testers can use Python and java to create their own test libraries.

12. Provides a remote test execution interface for distributed test execution.

The above content comes from Baidu Encyclopedia
 

3. RF environment installation

The installation of the environment requires version matching. The version configured on my computer is currently valid for personal testing.

1. Install python3.7.6 environment and configure environment variables;

2. Open the dos window as an administrator:

Install robotframework: pip install robotframework==4.1

Uninstall as pip uninstall robotframework

3. Install the RIDE tool in the dos window. RIDE is a development tool for robotframework:

Install RIDE: pip install robotframework-ride==1.7.4.1

Uninstall: pip uninstall robotframework-ride

The following dialog box will pop up during installation, do you want to create a desktop shortcut? , click "Yes".

 4. Install wxPython

Wxpython is a very famous GUI library for python, because RIDE is developed based on this library, so this must be installed.

pip install wxPython==4.0.4

5. Selenium2Library installation

RF-seleniumlibrary can be regarded as the RF version of the selenium library, and selenium (webdriver) can be regarded as a set of web-based specifications (APIs). Therefore, test tools such as RF and appium can locate and operate pages based on this set of APIs.

pip install robotframework-selenium2library

6. Double-click the icon to open it, or enter ride.py in the dos window to open it, as shown in the figure below after opening.

 

Problems encountered during installation

Encountered flashback or startup error as follows:

 Change self._initial_locale = wx.Locale(wx.LANGUAGE_ARABIC) in the application.py file in the ..python37\Lib\site-packages\robotide\application directory to self._initial_locale = wx.Locale(wx.LANGUAGE_ENGLISH)

 In case of other problems, please help enthusiastic netizens, most of the problems can be solved.

Fourth, the use of RF

1. Create a new project

Click [File]→[New Project], fill in the project name, path, and type (file or folder) in the pop-up box, and create a new folder here.

 

2. Create a test suite

Right-click on the folder, select [New Suite], fill in the name of the test suite in the pop-up box, and select File as the type.

 

 

3. Create test cases

Right-click on the test suite, select [New Test Case], and fill in the name of the test case in the pop-up box.

 

4. Create resource files

Right-click on the folder, select [New Resource], fill in the name in the pop-up box, and select the format as TXT. Generally, it saves business keyword resources and is the carrier of custom keywords. User-defined keywords can be created under the resource file.

 

 

 

5. Page Operation Introduction

[1] The Edit tab page of the test suite, as shown in the figure below

  1. Settings settings

2. Import: import external files

Library: Import an external class library, if it is black, it means success, and if it is red, it means failure.

Resource: Import resource files, for example, you can import business keywords.

3. Define internal variables

Adding variables, adding a list collection, and adding a dictionary is relatively seldom used, and the more commonly used functions are the Library and Resource functions of importing external files.

4. Metadata

【2】Test case page

 

 

Five, RF common class library

1. Standard library : No need to install, use it directly, RF comes with it.

Buitini (test library)

Collections

DateTime (time library)

ScreenShot (screenshot library)

The location of the standard library: D:\software\python37\Lib\site-packages\robot\libraries

2. Extended library : need to install the library through pip

Web automated testing: SeleniumLibrary, Selenium2Library, Selenium2Library for java, etc.

API interface automation: RequestsLibrary

APP automated testing: AppiumLibrary

Installation method:

pip install robotframework-seleniumlibrary

pip install robotframework-requests

pip install robotframework-appiumlibrary

The location of the extension library: D:\software\python37\Lib\site-packages\

Note: When importing the package, it must be consistent with the name of the folder, including capitalization.
 

Six, the use of common keywords in RF

hot key:

1. Search keywords: F5

2. Automatically complete keywords: ctrl+shift+space

When we encounter unfamiliar keywords, we can find out how to use them in the pages shown below.

 Small scale chopper

 

 

Comment	1.打印									
Log	这是一条打印语句									
Comment	2.设置变量									
${a}	Set Variable	100								
Log	${a}									
Comment	3.获取系统时间									
${times}	Get Time									
Log	${times}									
Comment	4.睡眠时间,强制等待									
sleep	3									
Comment	5.字符串的拼接									
${str}	Catenate	oracle	mysql	sqlserver						
Log	${str}									
Comment	6.创建列表									
${list1}	Create List	功能测试	自动化测试	性能测试						
Log	${list1}									
@{list2}	Create List	功能测试	自动化测试	性能测试						
Log Many	@{list2}									
Comment	7.创建字典									
${dic}	Create Dictionary	name=张三	age=18							
Log	${dic}									
Comment	7.1 获得字典的键									
${keys}	Get Dictionary Keys	${dic}								
Log	${keys}									
Comment	7.2 获得字典的值									
${values}	Get Dictionary Values	${dic}								
Log	${values}									
Comment	7.3 通过键取值									
${key_value}	Get From Dictionary	${dic}	name							
Log	${key_value}									
Comment	8.执行python里面的方法									
${random_number}	Evaluate	random.randint(1,101)	modules=random							
Log	${random_number}									
${times}	Evaluate	time.time()	modules=time							
Log	${times}									
Comment	9.执行python自定义的方法									
Import Library	E:/pythonProject/test.py									
${a}	Evaluate	int(10)								
${b}	Evaluate	int(20)								
${return_result}	sum	${a}	${b}							
Log	${return_result}									
Comment	10. 流程控制IF									
${age}	Set Variable	22								
Run Keyword If	${age}>30	Log	年龄太大,不合适	ELSE IF	18<=${age}<=30	Log	年龄正合适	ELSE	Log	未成年
Comment	11. 流程控制FOR									
FOR	${a}	IN	oracle	mysql	sqlserver					
	Log	${a}								
END										
Comment	流程控制FOR的另一种形态									
@{list3}	Create List	oracle	mysql	sqlserver						
FOR	${a}	IN	@{list3}							
	Log	${a}								
END										
Comment	流程控制FOR循环范围内的数据									
FOR	${a}	IN RANGE	1	11						
	Run Keyword If	${a}==5	Exit For Loop							
	Log	${a}								
END										
										
复制代码

The result of running the above code:

 

Starting test: TestDemo.RF测试1.TestSuit1.TestCase1
20210831 08:28:34.827 :  INFO : 这是一条打印语句
20210831 08:28:34.828 :  INFO : ${a} = 100
20210831 08:28:34.828 :  INFO : 100
20210831 08:28:34.829 :  INFO : ${times} = 2021-08-31 08:28:34
20210831 08:28:34.830 :  INFO : 2021-08-31 08:28:34
20210831 08:28:37.837 :  INFO : Slept 3 seconds
20210831 08:28:37.839 :  INFO : ${str} = oracle mysql sqlserver
20210831 08:28:37.839 :  INFO : oracle mysql sqlserver
20210831 08:28:37.840 :  INFO : ${list1} = ['功能测试', '自动化测试', '性能测试']
20210831 08:28:37.840 :  INFO : ['功能测试', '自动化测试', '性能测试']
20210831 08:28:37.841 :  INFO : @{list2} = [ 功能测试 | 自动化测试 | 性能测试 ]
20210831 08:28:37.841 :  INFO : 功能测试
20210831 08:28:37.841 :  INFO : 自动化测试
20210831 08:28:37.841 :  INFO : 性能测试
20210831 08:28:37.842 :  INFO : ${dic} = {'name': '张三', 'age': '18'}
20210831 08:28:37.843 :  INFO : {'name': '张三', 'age': '18'}
20210831 08:28:37.844 :  INFO : ${keys} = ['age', 'name']
20210831 08:28:37.844 :  INFO : ['age', 'name']
20210831 08:28:37.845 :  INFO : ${values} = ['18', '张三']
20210831 08:28:37.846 :  INFO : ['18', '张三']
20210831 08:28:37.847 :  INFO : ${key_value} = 张三
20210831 08:28:37.847 :  INFO : 张三
20210831 08:28:37.848 :  INFO : ${random_number} = 101
20210831 08:28:37.848 :  INFO : 101
20210831 08:28:37.849 :  INFO : ${times} = 1630369717.848543
20210831 08:28:37.849 :  INFO : 1630369717.848543
20210831 08:28:37.859 :  INFO : ${a} = 10
20210831 08:28:37.860 :  INFO : ${b} = 20
20210831 08:28:37.860 :  INFO : ${return_result} = 30
20210831 08:28:37.861 :  INFO : 30
20210831 08:28:37.861 :  INFO : ${age} = 22
20210831 08:28:37.862 :  INFO : 年龄正合适
20210831 08:28:37.863 :  INFO : oracle
20210831 08:28:37.864 :  INFO : mysql
20210831 08:28:37.865 :  INFO : sqlserver
20210831 08:28:37.866 :  INFO : @{list3} = [ oracle | mysql | sqlserver ]
20210831 08:28:37.867 :  INFO : oracle
20210831 08:28:37.868 :  INFO : mysql
20210831 08:28:37.869 :  INFO : sqlserver
20210831 08:28:37.871 :  INFO : 1
20210831 08:28:37.872 :  INFO : 2
20210831 08:28:37.873 :  INFO : 3
20210831 08:28:37.875 :  INFO : 4
20210831 08:28:37.876 :  INFO : Exiting for loop altogether.
Ending test:   TestDemo.RF测试1.TestSuit1.TestCase1
复制代码

7. Prepare the environment for UI automation testing

1. Install the extension library through pip: pip install robotframework-seleniumlibrary;

2. Download Google Chrome;

3. Download the driver of Google Chrome (note: the driver of Google Chrome must be compatible with Google Chrome), and then put chromedriver.exe in the python directory;

4. Import SeleniumLibrary in the RF test suite;
 

8. Keyword for browser operation

 

 

Comment	打开浏览器		
Open Browser	https://www.baidu.com	chrome	
Comment	隐式等待		
Set Browser Implicit Wait	5		
sleep	2		
Comment	浏览器放大		
Maximize Browser Window			
sleep	2		
Comment	设置浏览器的尺寸		
Set Window Size	1024	768	
sleep	2		
${width}	${height}	Get Window Size	
sleep	2		
Comment	返回上一步		
Go Back			
sleep	2		
Comment	直接跳转页面		
Go To	https://www.baidu.com		
Comment	刷新页面		
Reload Page			
sleep	2		
Comment	获取title		
${title}	Get Title		
Log	${title}		
sleep	2		
Comment	获取路径		
${loc}	Get Location		
Log	${loc}		
sleep	2		
Comment	关闭浏览器		
Close Browser			
复制代码

9. Element positioning

Eight ways to locate elements: id, name, link_text, partial_link_text, xpath, css, class_name, tag_name

Prerequisite: Elements must be unique

The following are several simple ways to locate by id, name, link

 In addition to the above positioning methods, there are two more powerful positioning methods: xpath and css

We are now going to position the textbox as follows:

 

xpath: (the elements positioned below are part of the Baidu homepage)

1. By absolute path positioning, this method is almost useless.

2. Locating by relative path://form/span/input

 3. Locating by element attribute: //input[@autocomplete="off"]or//input[@autocomplete="off" and @class="s_ipt"]

4. Locate through some attributes: //input[starts-with(@autocomplete,"of")] or //input[contains(@autocomplete,"of")]

5. Position by text//a[text()="news"]

css: (the elements positioned below are part of the Baidu homepage)

1. By absolute path positioning, this method is almost useless.

2. Position by ID or Class: #ID or .class

3. Positioning through element attributes:

An attribute positioning: input[autocomplete="off"]

Two attributes are positioned at the same time: input[autocomplete="off"][class="s_ipt"]

4. Locating through some attributes:

What to start with: input[autocomplete^="of")]

What to end with: input[autocomplete$="ff")]

Contains: input[autocomplete*="of")]

5. Locate div#s-top-left a:nth-child(3) through child elements, and the id is the third a tag under s-top-left.

 

How to deal with the frame frame?

In actual projects, we will find that there are frame frames in many cases, and the elements in the frame frame cannot be positioned directly, and need to jump into the current frame before positioning.

 If you want to locate elements that are not in the frame, you need to jump out of the current frame before proceeding: Unselect Frame, as shown in the figure below:

  The positioning method of the drop-down box list:

  Take the following Select From List By Valueas an example:

  Target one of a group of identical elements:

 

Handling alert boxes

There are generally three types of bullet boxes: alert, confirm, prompt

 

To process the alert box in the figure above, the key word is Handle Alert, if you click OK, you don’t need to pass any parameters, if you click the cancel button, pass the value: DISMISS.

10. The three-tier structure of the project

1. What is the three-tier architecture?

(1) Page element layer

(2) Business logic layer

(3) Test case layer

The business logic layer calls the page element layer, and the test case layer calls the business logic layer.

2. Why layering and what is the significance?

Realize centralized management of page elements, public methods, public data, business logic, and test cases.

Increase script reusability.

Increased script maintainability.
 

11. RF non-GUI mode (command line) running and Jenkins integration

Command: pybot -d path to test report path to test case

Basic operations of Jenkins

1. Install Jenkins environment

(1) Install jdk environment;

(2) Enter the Jenkins official website, download the installation file, double-click to install, and follow the steps;

 2. Jenkins configuration

(1) Search for Robot Framework in the plug-in management and install it.

 (2) Create a free style project

  (3) Set up scheduled execution tasks

  (4) Create a Windows batch command

 (5) Added Execute Groovy script System.setProperty("hudson.model.DirectoryBrowserSupport.CSP","")

 (6) Add post-build operation steps

 

Guess you like

Origin blog.csdn.net/lzz718719/article/details/131603325