This page looks best with JavaScript enabled

Robot Framework Basics

 ·  ☕ 2 min read

This post mainly introduces some basic concepts of Robot Framework and how to write a test case.

1. Basic Elements

1.1 Keywords

Robot Framework keywords are similar to functions. They are divided into system keywords and user-defined keywords.

  • System keywords, imported by loading a Library
  • User keywords, imported by loading a Resource

1.2 Library

Libraries encapsulate and reuse keywords. Robot Framework libraries include system libraries and user-defined libraries.

  • System libraries, such as XML, String, etc.
  • User-defined libraries. Some third-party libraries installed separately, such as seleniumlibrary, etc. They can also be Python files you write yourself, such as send_mail.py, etc.

1.3 Test Case

A collection of keywords and test data combined in order, used to test a certain scenario. Several test cases make up a test suite.

1.4 Resource

The overall structure of a resource file is the same as a test case file, except that it cannot contain test cases.

1.5 Test Suite

A test suite is a txt file containing multiple test cases, including the content of four parts.

  • Setting
  • Variable
  • Test Case
  • Keyword

1.6 Variable

Variables come in three types:

  • Scalars, referenced as: ${SCALAR}
  • Lists, referenced as, @{LIST}
  • Dictionaries, referenced as: &{DICT}

In addition, environment variables can be referenced directly with %{ENV_VAR}

1.7 Data Types

  • String: ${variable},${test}
  • Numeric: ${80},${1.2}
  • Boolean: ${true},${false}
  • Null/None: ${None},${null}
  • Space, Empty: ${SPACE},${EMPTY}

2. Directory Structure

You can first create the directory structure below locally, then open the project file with RIDE:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ProjectName
|---cases
|------ 测试套件
|--------- 测试用例
|---keywords (存放关键字)
|------common.txt (通用关键字)
|---res(附件资源文件夹,放图片、表格、文件等)
|---global.txt(存放一些必须的全局变量)
|---locators (存放页面上一些固定的元素,进行统一管理)
|---lib (自己写的一些库 Python 库文件)

If there are few variables and test cases to maintain, just use a single text file.

If you need to maintain a large number of variables and users, then split the files into folders and create files for each module inside the folders for maintenance.

3. Writing Your First Test

3.1 The Structure of a Test Case

  • Some initialization operations
  • Some test operations on the page to be tested
  • Verify whether the page’s behavior matches expectations

3.2 A Simple Test Case

global.txt

1
2
3
4
*** Variables ***
${site_url}       http://xxxxx.com/    # 首页地址
${username}       myname    # 用户名
${password}       password    # 密码

For ease of maintenance, define global variables in a separate file.

case/login.txt

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
*** Settings ***
Resource          ../global.txt

*** Test Cases ***
登录
    Open Browser    ${site_url}    gc
    Maximize Browser Window
    Wait Until Page Contains Element    id=user
    Wait Until Element Is Visible    id=user
    Input Text    id=user    ${username}
    Sleep    1
    Wait Until Page Contains Element    id=password
    Wait Until Element Is Visible    id=password
    Input Text    id=password    ${password}
    Sleep    1
    Wait Until Page Contains Element    jquery=.login-btn
    Wait Until Element Is Visible    jquery=.login-btn
    Click Element    jquery=.login-btn
    Sleep    1

This is a simple test case: it opens a login link, enters the username and account password, and finally logs in and redirects.

In fact, Robot Framework supports several description formats such as HTML, TSV, TXT, and reST. Robot Framework uses a different parser for each format.

Take TXT as an example.

The first column is the test case name, meaning this test case is named 登陆.

The second column is either a keyword or a variable. As mentioned above, keywords are first-class citizens in Robot Framework and can be understood as functions. Functions are divided into those with a return value and those without. If a keyword with a return value is used, then several columns starting from the second may be used to receive return values, until a keyword is encountered. If the keyword has no return value, then the second column is the keyword to use. For example, Open Browser and Maximize Browser Window here are both keywords.

The columns following a keyword are passed to the keyword as arguments. For example, id=password, ${password}, and so on here.

3.3 How to Use Keywords

After reading the example above, you have probably realized how important keywords are in Robot Framework. A large number of operations require keywords, and you also need to encapsulate your own keywords.

In RIDE, press F5 to see the built-in and library keywords and their help documentation.

In web automated testing, the most commonly used are the keywords from the selenium2library library.

4. References


微信公众号
WRITTEN BY
微信公众号