当前位置:网站首页>Robot_ Framework: Variables
Robot_ Framework: Variables
2022-06-09 07:30:00 【Cat fearless mouse a】
Variable declarations
1、 because RF The bottom is Python, So its syntax is similar , Variables do not require specific declarations , It can be used as long as there is an initialization assignment
2、 If you insist that there is a statement , Then you can put TestSuite The following manually added variables are understood as declarations . For example, you can TestSuite Right click or click on Edit Zone point Add Scalar or Add List or Add Dict To add variables
3、 stay TestSuite Defining variables gives me the feeling that : Some global variables are defined , Applicable to current TestSuite All test cases under
⑴ If it is defined in the resource file suite Next , By calling the resource file , Any test suite You can use . This is different from the variables defined in the test case
Variable type
1、RF There are three main types of variables :Scalar、List、Dict
2、RF Each variable in the needs to use " Variable identifier " To indicate the type of the variable : Each variable can use " Variable identifier { Variable name }" To express
⑴Scalar Variable :${ Variable name }. Represents a data . When passed as a parameter , Represents a parameter
⑵List Variable :@{ Variable name }. Represents a set of data . When passed as a parameter , A few data are just a few parameters
⑶Dict Variable :&{ Variable name }. Represents a set of key value pairs . When passed as a parameter , A few key value pairs are just a few parameters
3、 By default RF All variables with numeric values are string type
notes :
1、Scalar Variable : In translation, it is called scalar in Chinese ( Single valued variable ), It corresponds to List This multivalued variable
⑴ Scalar variables are variables that hold only one value , It can be a number 、 character string 、 list 、 Dictionary, etc
2、 Variable types can be converted
⑴Dict Variables can be converted to Scalar Variable
⑵List Variables can be converted to Scalar Variable
⑶ stay RF in , I feel the type of a variable (Scalar、List、Dict) It mainly depends on the identifier before the variable name
3、 Pay attention Scalar Variables and List Variable (Dict Variable ) The difference between : There are some differences between the meaning of variables and their invocation
⑴Scalar Variable : When its value is list 、 Dictionary time , Is to put the whole list 、 The dictionary as a whole ( Parameters ). Always a value
⑵List Variable : Is to change its value ( list ) Take it alone , A few elements in the list are just a few parameters ( Corresponding Python Variable parameters inside :*args)
⑶Dict Variable : A few key value pairs are just a few parameters ( Corresponding Python Variable parameters inside :**Kwargs)
Variable assignment
1、( establish ) There are also several ways to assign variables , It can be used according to the actual situation
⑴ You can use keyword methods :Set and Create assignment
⑵ It can also be assigned in operation ( That is, assign the return value of the keyword to a variable in the test case )
2、Scalar Variable :Set variable Keyword assignment
3、List Variable :Set variable or Create list Keyword value
4、Dic Variable :Create Dictionary Keyword assignment
notes :
1、Create Dictionary Key words and Create list Keywords can also be used to create use cases Scalar Variable
⑴ It just means that when defining variable names , Inconsistent variable identifiers used
Scope of variable
1、 In general , Each variable is a local variable by default . But the scope of a specific variable should be determined according to its defined position : The variables defined in the test cases mentioned earlier ,suite Variables defined in
⑴ One Case The variables inside : The scope is in this Case Inside
⑵ One UserKeyword The variables inside : The scope is in this UserKeyword Inside . Just reference the directory of the resource file or suit You can use these variables
⑶ A file type Suite The variables inside : The scope is in this Suite Inside , All below it Case You can use ( One is defined in the directory type suite Under the , One is defined in the resource file , Variables defined under the resource file can be used by calling the resource file )
⑷ A directory type Suite The variables inside : The scope is in this directory ( This definition is meaningless : The file type below it Suite You cannot use these variables )
2、 The scope of a variable can be changed . Some keywords can be used to change the scope of variables
⑴Set Global Variable: Set to global variable . After setting, this variable can be used in all test cases and test suites
⑵Set Suite Variable: Set to File Suite Level variable . After setting this variable in the current file suite Can be used inside
⑶Set Test Variable: Set to Case Level variable . After setting, this variable is in the current Test Case You can use
notes : However, it is not recommended to use these keywords to change the scope of variables
Create variables
1、 First RF There are three types of variables :Scalar、List、Dict
2、RF There are three ways to create variables :TestCase、TestSuite、UserKeyword
3、 There are two ways to create variables : Use keywords to create 、 Create... During use case running 、 stay Edit Zone point Add XXX To create
establish Scalar Variable
1、Scalar Variable : Scalar variables are variables that hold only one value , It can be a number 、 character string 、 list 、 Dictionary, etc
⑴ When its value is list 、 Dictionary time , Is to put the whole list 、 The dictionary as a whole ( Parameters ). Always a value
2、Scalar Variable : Use "${ Variable name }" To define Scalar Variable , Use "${ Variable name }" To take a value ( call )
Use keywords to create
1、 Can be used to create Scalar The key words of variables mainly include the following :
⑴Set Variable: This keyword is used to set the variable , The scope is smaller , Usually used for a single CASE in , You need to declare ( Commonly used )
⑵Set Suite Variable: The scope of this keyword is Suite Available in , The premise is to state , Later need suite In all of the case Just can use ( Not commonly used )
⑶Set Global Variable: Scope of action : The scope of this keyword is Proeject, The premise is to state , After the statement case、suite Only available ( Not commonly used )
example 1:
⑴ Defining variables

⑵ Output

notes :
1、 By default ,RF The input variable values are all string type
2、 Creating dictionary type Scalar Variable or Dict variable , Its value can be replaced by a variable

stay Edit Zone creation
1、 stay Edit The zone creates variables through Add Scalar To create
2、 stay Edit The variables created by the zone are Suit Level variables , In this whole Suit This variable can be used in all test cases under
3、Edit The zone can be a resource file or a test directory . The two places create variables in the same way , Only the variable scopes created are inconsistent
⑴ Variables under the resource file : As long as the resource file is called , You can use the following variables
example 2:
⑴ Create variables 
⑵ Call variables 
Create variables at run time
1、 Creating variables during test case running refers to : Put keywords (Python function ) The return value of is assigned to a variable
example 3:
⑴ Create variables 
establish List Variable
1、List Variable :@{ Variable name }. Represents a set of data , When passed as a parameter , A few data are just a few parameters ( Corresponding Python Variable parameters inside :*args)
2、 So in RF If you need to define variable position parameters (*args) Words , You can define the variable as List type
Use keywords to create
1、 and Scalar similar ,List Variables can also be used Set Variable To assign a value , But its most formal way of assignment is to use Create List keyword
example 4:
⑴ Create variables

⑵ Output

notes :
1、 In this example, the keywords are used respectively Set Variable and Create List To set a variable , And assign values to a Scalar The type and List Type variable
⑴ You can see that no matter what you use Set Variable Or use Create List To set a variable , They all return the same result ( It's all lists ), But the meaning of data representation is inconsistent
⑵Scalar type : No matter how many elements there are in the value , Is a variable value
⑶List Type variable : There are as many variable values as there are elements in the value ( Feeling heel Python The variable parameters in are somewhat similar to )
stay Edit Zone creation
1、 stay Edit The zone creates variables through Add List To create
2、 stay Edit The variables created by the zone are Suit Level variables , In this whole Suit This variable can be used in all test cases under
3、Edit The zone can be a resource file or a test directory . The two places create variables in the same way , Only the variable scopes created are inconsistent
⑴ Variables under the resource file : As long as the resource file is called , You can use the following variables
example 5:
⑴ Create variables

Create variables at run time
1、 Feel created in the run List Type variables are rare : Even a keyword (Python function ) It returns a list , It also considers the list as a whole ( At this time to use $ identifier ), Not that you need to separate the elements in the list
2、List(@) Variable , Just to define variable parameters (*args)
take List Type to Scalar type
1、 Will a List The type list is changed to Scalar Type list , Just modify the identifier in front of the variable name ( take @ It is amended as follows $)
example 6:


notes :
1、 The reason why it is necessary to List The type list is changed to Scalar The type list is because log Keyword can only accept one required parameter
⑴ If you pass in List If it is a type parameter, it will have multiple parameters :List The number of elements in the value of a type variable represents the number of parameters
⑵ So we can only List The type list is changed to Scalar Type list : Pass in as a whole
⑶ If the keyword originally accepts a variable parameter , Then at this time, you can't List Type variable is changed to Scalar Type passed in
2、 Although this time "${ Variable name }" Displayed in purple ( Purple means that the variable is undefined ), But in this case, the variable can be used normally
3、 So is it necessary to List The type list is changed to Scalar The type list still depends on the actual situation : What type of parameter the keyword accepts
@{lists} and ${lists} The difference between
㈠ The same thing :
1、@{} List type and ${} List types can be used to create list variables , You can go through set variable and create list establish
㈡ Difference
1、 The two types of variables have different meanings :
⑴Scalar type : No matter how many elements there are in the value , Is a variable value
⑵List Type variable : There are as many variable values as there are elements in the value
⑶${lists} It can be seen as a whole list , A list is a parameter ; @{lists} Is to break the list into individual elements , Each element is a parameter
⑷${lists} stay Python There's one in there list( all in one ), then @{lists} Is to expand its list Every element in , Passed in as multiple parameters , and *args The same effect , stay Python It is called variable parameter
⑸ Simply put : In defining ( Pass in as a parameter ) variable , See whether we need to pass in the whole list or the elements in the separate list
① Pass in the entire list : Think of the list as a whole : Is defined as Scalar type
② Separate the elements in the list : Is defined as List Type variable ( Corresponding Python Variable parameters inside :*args)
2、 The display format and reference format of two types of variables
⑴Scalar Type variables and List Type variables can represent lists , So you can use list indexes
⑵List Type variable : stay List Add the upper bracket outside the bracket of the variable of , Inside is the index value ,@{ Variable name }[ Index value ]
⑶Scalar Type variable : Put parentheses after the variable name , Inside is the index value ,${ Variable name [ Index value ]}
establish Dict Variable
1、Dict Variable :&{ Variable name }. Represents a set of key value pairs . When passed as a parameter , A few key value pairs are just a few parameters ( Corresponding Python Variable parameters inside :**Kwargs)
2、 So in RF If you need to define variable keyword parameters in (**Kwargs) Words , You can define the variable as Dict type
Use keywords to create
1、 have access to Create Dictionary Keyword to create dictionary variables
example 7:
⑴ Create variables

⑵ Output

stay Edit Zone creation
1、 stay Edit The zone creates variables through Add Dict To create
2、 stay Edit The variables created by the zone are Suit Level variables , In this whole Suit This variable can be used in all test cases under
3、Edit The zone can be a resource file or a test directory . The two places create variables in the same way , Only the variable scopes created are inconsistent
⑴ Variables under the resource file : As long as the resource file is called , You can use the following variables
example 8:
⑴ Create variables 
Create variables at run time
1、 Feel created in the run Dict Type variables are rare : Even a keyword (Python function ) Back is a dictionary , It also takes the dictionary as a whole ( At this time to use $ identifier )
take Dict Type to Scalar type
1、 Will a Dict Type dictionary to Scalar Type Dictionary , Just modify the identifier in front of the variable name ( take & It is amended as follows $)
example 8_1:


notes :
1、 The reason why it is necessary to Dict Type dictionary to Scalar Type dictionary is because log Keyword can only accept one required parameter
⑴ If you pass in Dict If it is a type parameter, it will have multiple parameters :Dict The number of elements in the value of a type variable represents the number of parameters
⑵ So we can only Dict Type to Scalar type : Pass in as a whole
⑶ If the keyword originally accepts a variable parameter , Then at this time, you can't Dict Type variable is changed to Scalar Type passed in
2、 Although this time "${ Variable name }" Displayed in purple ( Purple means that the variable is undefined ), But in this case, the variable can be used normally
3、 So is it necessary to Dict Type dictionary to Scalar Type dictionary still needs to see the actual situation : What type of parameter the keyword accepts
&{dict} and ${dict} The difference between
1、 Dictionary variables are also a kind of scalar , It's just that values are dictionaries , So it can be regarded as a dictionary variable
2、${dict} It can be regarded as a complete dictionary object ,&{dict} It can be seen as a whole and disassembled into individual key value pairs
3、${dict} stay Python There's one in there dict, then &{dict} Is to expand its dict Each key value pair in , Passed in as multiple parameters , and **kwargs The same effect , stay Python It is called keyword parameter
Variable operating
1、RF It's using Python Written , So its variables can be used in some ways Python The grammar of
⑴ Such as the index 、 Slicing, etc.
Scalar Variable
1、Scalar Variable : Use "${ Variable name }" To define Scalar Variable , Use "${}" To take a value ( call )
2、 Index value :${var[index]}
3、 Slice value :${var[start_index:end_index:step]}
example 9:
list Variable
1、list Variable : Use "@{ Variable name }" To define ; Use "${ Variable name }" To take a value ( call )
2、 Create a list of :Create List or Set Variable
3、 Index value :${var[index]}
4、 Slice value :${var[start_index:end_index:step]}
example 10: Create variables
@{list_1} Set Variable 1 2 3 4
log ${list_1}
@{list_2} Create List 1 2 3 4
log ${list_2}
# Output
20210111 17:06:39.012 : INFO : @{list_1} = [ 1 | 2 | 3 | 4 ]
20210111 17:06:39.013 : INFO : [u'1', u'2', u'3', u'4']
20210111 17:06:39.014 : INFO : @{list_2} = [ 1 | 2 | 3 | 4 ]
20210111 17:06:39.015 : INFO : [u'1', u'2', u'3', u'4']example 14: Indexes 、 section
@{list_1} Set Variable 1 2 3 4
log ${list_1[1]}
log ${list_1[1:3:1]}
# Output
20210111 17:07:50.601 : INFO : @{list_1} = [ 1 | 2 | 3 | 4 ]
20210111 17:07:50.602 : INFO : 2
20210111 17:07:50.603 : INFO : [u'2', u'3'] notes : As can be seen from the above example
⑴ Defined List(@) Variable , It is considered as a whole when calling , The whole list is just a variable . Use $ Identifier calls
⑵List(@) Variable , Just to define variable parameters (*args), It needs to be seen as a whole when it is used
Dict Variable
1、Dict Variable : Use "&{ Variable name }" To define Dict Variable ; Use "${ Variable name }" To take a value ( call )
2、 Create a dictionary :Create Dictionary
example 11: Create variables
&{dict} Create Dictionary name=zh age=11 country=china
log ${dict}
# Output
20210111 17:17:58.307 : INFO : &{dict} = { name=zh | age=11 | country=china }
20210111 17:17:58.308 : INFO : {u'name': u'zh', u'age': u'11', u'country': u'china'} notes : As can be seen from the above example
⑴ Defined Dict(&) Variable , It is considered as a whole when calling , The whole dictionary is a variable . Use $ Identifier calls
⑵Dict(&) Variable , Can be used to define variable parameters (**kwargs: In fact, it is also a dictionary ), It needs to be seen as a whole when it is used
Constant
1、 In addition to variables ,RF There are also some constants in the . Constants are mainly environment variables 、 Numerical constants 、 Special character constants 、 The system reserves variables
2、 environment variable : stay RF The same identifier can be used in "%" To express . For example, the computer environment variable defines a JAVA_HOME The variable of , It's in RF Can be used in %{JAVA_HOME} To call (RF Can only be used in , Can't assign a value )
3、 Numerical constants : Usually , stay RIDE All the characters in it will be treated as strings , Even if the value of a variable is a number , By default, it also exists in string form (Robotframework The default input in the input box is string ). If you want it to exist numerically , You can use numerical constants
⑴ Use numeric definitions ${2}: Such as defining a 2 Value type of , It can be written. ${${2}} ( The outer ${} Is to define a variable , The inner layer of the ${2} Represents the value , The feeling is to escape )
⑵ Of course you can PY Use in script int() Function conversion
4、 Special character constants and system reserved variables : In this business, you can hardly meet , such as ${/}、${:}、${False}、${None}、${null}、${True}
边栏推荐
猜你喜欢

InfoWorld article - applying data orchestration technology to AI model training

Simple analysis of performance inflection point

Question bank and answers of 2022 special operation certificate for installation and repair of refrigeration and air conditioning equipment

Squid代理服务器应用

Oracle: subquery, sorting

Opengauss database operation steps

【无标题】

2022年塔式起重机司机(建筑特殊工种)考试题模拟考试题库及模拟考试

推送相关的总结

2022制冷与空调设备安装修理特种作业证考试题库及答案
随机推荐
Confusing output from infinite recursion within try catch
How about opening an account for shares of tongdaxin? Is it safe to open an account?
性能测试流程,指标,实现方法以及总结
蓝桥杯电子类单片机第十一届决赛试题
Apache Web page and security optimization
[guide to stepping on the big pit] CSharp_ Process_ Prompt after running: no such file or pyinstaller prompt badzip, etc
Which software design documents are most easily ignored?
Jasperreport generate PDF report section
Flutter学习之Hello World
PostgreSQL database replication - background first-class citizen process walreceiver extracts information
Homebrew installing MySQL
政务大厅导航系统功能解读
100 Go Mistakes and How to Avoid Them 之代码结构和项目组织(四)
招聘 | 上班轰趴,下班狼人杀,天天招人,怕是要发!
Question bank and answers of 2022 special operation certificate for installation and repair of refrigeration and air conditioning equipment
C language file -- read and write by string
大厂面试算法系列-动态规划求解最长公共子串问题
朴素贝叶斯分类器
【工作向】conda常用命令汇总
【6月第一周学习记录】UU-Computer vision(1):3D reconstruction&Camera calibration