(二) robot framework - variable file

一,文件中创建variable的两种方式
1) 直接创建:
例子:
VARIABLE = "An example string" = ${VARIABLE}
INTEGER = 42 = ${INTEGER}
STRINGS = ["one", "two", "kolme", "four"] = ${STRINGS}
全是scalar类型的variable, 要创建List类型的variable就需要以LIST__ (注意双下划线)开头来命名variable:
LIST__STRINGS = ["list", "of", "strings"] = @{STRINGS}

还支持创建对象类型的variable,如:
MAPPING = {'one': 1, 'two': 2} -- 创建dictionary variable = ${MAPPING}
class MyObject: --创建一个类
def __init__(self, name):
self.name = name
OBJ1 = MyObject('John')
OBJ2 = MyObject('Jane') -创建2个类的对象


此外,还可以动态创建variable:
RANDOM_INT = random.randint(0, 10);
if time.localtime()[3] > 12:
AFTERNOON = True
else:
AFTERNOON = False

注1: 通常全局的大写,非全局的小写;
注2: 以下划线_开头的属性不被识别为variable;
注3: 在variable文件中提到的所有不以下划线开头的属性都被认为是variable,可以使用添加下划线的方式来过滤它们, 也可以使用__all__ (双下滑)来将要定义为variable的加进去,如:__all__ = ['AREA1', 'AREA2'],那么只有area1和area2被认为是variable;

2) 从方法或函数中获得返回值的方式 get_variables(),存储结构是python的dictionary 或 java的map;如下例子和1)中的第一个例子效果一样:
def get_variables():
variables = { "VARIABLE": "An example string",
"INTEGER": 42,
"STRINGS": ["one", "two", "kolme", "four"],
"LIST__STRINGS": ["list", "of", "strings"]
}
return variables

java实现版本:
public Map getVariables() {
HashMap variables = new HashMap();
variables.put("VARIABLE", "An example string");
variables.put("INTEGER", 42);
variables.put("STRINGS", ["one", "two", "kolme", "four"]);
variables.put("LIST__STRINGS",["list", "of", "strings"]);
return variables;
}

此外,get_variables()也可以接受参数,通常可以用来进行选择导入哪个variable file或者variable value,如下:
variables1 = { 'a': 'Scalar variable',
'LIST__a': ['List','variable'] }
variables2 = { 'a' : 'Some other value',
'LIST__a': ['Some','other','value'],
'extra': 'variables1 does not have this at all' }

def get_variables(arg):
if arg == 'one':
return variables1
else:
return variables2


二,一些规则
- 当找不到导入的variable文件时,会去python PATH下面找;
- 如果多个引入的variable文件中含有相同的变量名,那么最早的那个将被采用;
- cmd下设置的变量将覆盖文件中的同名variable;
- cmd上导入variable文件:
(--variablefile myvariables.py
--variablefile path/variables.py
--variablefile /absolute/path/common.py
--variablefile taking_arguments.py:arg1:arg2)
此时的variable scope是全局的;

三, 采用java class和python class 来实现 variable files:
一些rules:
1) Python class的名字必须和module的名字相同;
2) java class必须处于默认包中;
3) java class的路径必须以.java或.class结尾,并且两种文件都要存在;

例子-1:创建类属性 ${VARIABLE},@{LIST} 和对象属性 ${ANOTHER VARIABLE}
class StaticPythonExample(object): --python版本
variable = 'value'
LIST__list = [1, 2, 3]
_not_variable = 'starts with an underscore'

def __init__(self):
self.another_variable = 'another value'

public class StaticJavaExample { -------java版本
public static String variable = "value";
public static String[] LIST__list = {1, 2, 3};
private String notVariable = "is private";
public String anotherVariable;

public StaticJavaExample() {
anotherVariable = "another value";
}
}

例子-2:
class DynamicPythonExample(object): --python

def get_variables(self, *args):
return {'dynamic variable': ' '.join(args)}

import java.util.Map;
import java.util.HashMap;

public class DynamicJavaExample { -java

public Map<String, String> getVariables(String arg1, String arg2) {
HashMap<String, String> variables = new HashMap<String, String>();
variables.put("dynamic variable", arg1 + " " + arg2);
return variables;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值