
fitnesse - Variables and Symbols
2017-09-30
目录
1 Variables(静态变量)
1.1 定义及使用
1.2 Variable作用域
1.2.1 Variable在层次结构中的作用域
1.2.2 include对Variable作用域的影响
2 表中的Symbol(动态变量)
2.1 定义及使用
2.2 Symbol的作用域
3 Variable和Symbol的区别
1 Variables(静态变量)
1.1 定义及使用
Variables初始化有三种方式:
- !define VariableName {VariableValue} - 把大括号内的文本赋值给变量
- !define VariableName ${OtherVariableName} - 把另一个变量赋值给变量
- !define VariableName {${= 10 / 2 =}} - 通过表达式赋值给变量
Variables使用:
${VariableName}
示例1
页面脚本如下:
!define markedUp {This is '''bold'''}
${markedUp} is expressed as: This is bold !define y {y-value}
!define x {The value of y is ${y}}
${x} is expressed as: The value of y is y-value !define n {10}
!define q {2}
!define d {${=${n}/${q}=}}
${d} is : 5 !define q {5}
${d} is : 2
页面显示如下:
图1 示例1展示结果
1.2 Variable作用域
1.2.1 Variable在层次结构中的作用域
当Variable在页面中被调用,但当前页面没有,fitnesse会去依次去父页面寻找,若祖先页面也没找到,fitnesse会去System.properties寻找
示例:
http://ip:port/ HelloWorld
http://ip:port/ HelloWorld.HelloWorld1
http://ip:port/ HelloWorld.HelloWorld2
- 假设HelloWorld定义了静态变量!define name {tom}, HelloWorld1和HelloWorld2不用再次定义name变量,直接调用${name},就变量name的值为tom
- 假设HelloWorld定义了静态变量!define name {tom}, HelloWorld1又重新定义了变量!define name {jack},当HelloWorld1调用${name}后,变量name的值为jack,但不会影响HelloWorld2(HelloWorld2中name的值仍然是tom)
1.2.2 include对Variable作用域的影响
当变量在included page中时:
- 如果你把子页面include到主页面,那么你既可以使用主页面定义的变量,也可以用子页面定义的变量。
- 如果修改子页面的变量,那么主页面使用的变量也会修改。
示例2:
层次结构下:
Contents:
其中:
- grandfa页面定义变量: !define grandfaMoney {100}
- relative页面定义变量:!define relativeMoney {150}
- father页面定义变量:!define relativeMoney {150}
- mother页面定义变量:!define motherMoney {50}
- son页面定义变量:!define sonMoney {10}
把其他页面include到father页面,来测试在不同关系中各个变量的使用范围,页面脚本如下:
!define fatherMoney {80} grandfaMoney is: ${grandfaMoney}
relativeMoney is: ${relativeMoney}
fatherMoney is: ${fatherMoney}
motherMoney is: ${motherMoney}
sonMoney is: ${sonMoney} !include .Demo.variableIncludeTest.relative
!include .Demo.variableIncludeTest.grandfa.mother
!include .Demo.variableIncludeTest.grandfa.father.son grandfaMoney is: ${grandfaMoney}
relativeMoney is: ${relativeMoney}
fatherMoney is: ${fatherMoney}
motherMoney is: ${motherMoney}
sonMoney is: ${sonMoney}
页面保存后展示如下图所示:
图2 include变量作用域展示结果
在图2中我们可以得知:
- 未include前,我们可以使用主页面定义的变量和父节点或祖先(直系)页面定义的变量
- include的页面定义的变量,include后都能使用
2 表中的Symbol(动态变量)
2.1 定义及使用
Symbol是在表中定义(赋值)和使用的
示例3:
页面脚本如下:
!define TEST_SYSTEM {slim} !path D:\fitnesseUtil\bin |import |
|fitnesse.slim.test|
|util| |script|Add|1|2.2|
|$result=|calc|
|check|calc|$result| |script|Add|$result|2.2|
|$secondResult=|calc|
|check|calc|$secondResult|
|check|calc|$result|
其中:
- 第10行:$result= 表示声明并将返回的值赋值给声明的Symbol,从下图3可以看到 $result<-[3.2]
- 第11行:$result 表示使用变量result,从下图3可以看到 $result->[3.2]
执行结果:
图3 symbol(动态变量)的定义和使用
2.2 Symbol的作用域
Symbol作用域:
- Symbol定义的当前页面。从图3我们可以看到result定义后,可以跨表使用。
- 不是当前页面定义,在父节点页面定义,未在当前页面定义,不能使用,如下示例4。
- include symbol定义页面后可以使用,如下示例4。
示例4:
Contents:
grandfa页面脚本如示例3。father页面脚本如下:
!define TEST_SYSTEM {slim} !path D:\fitnesseUtil\bin |import |
|fitnesse.slim.test|
|util| |script|Add|1|2.2|
|check|calc|$result| !include .Demo.variableIncludeTest.grandfa |script|Add|1|2.2|
|check|calc|$result|
其中:
- 第10行:$result使用result,但由于在当前页面没有定义,所以没有被slim替换,还是$result,如下图4所示。
- 第15行:$result使用result,在这段脚本前,已经include father页面,在father页面定义了result,所以没有被slim替换,还是$result,如下图4所示。
执行结果如下:
图4 symbol作用域
3 Variable和Symbol的区别
- Variable的值在页面提交(页面保存)的时候,测试执行前就已经计算了。所以当父节点或祖先(直系)页面定义后,子节点页面可以使用。
- Symbol只存在在执行时
示例5:
页面脚本如下:
!define TEST_SYSTEM {slim} !path D:\fitnesseUtil\bin |import |
|fitnesse.slim.test|
|util| |script|Add|1|2.2|
|$result=|calc| !define vResult {$result}
vResult is: ${vResult} |script|Add|1|2.2|
|check|calc|$result|
|check|calc|${vResult}|
其中:
- 第12行: !define vResult {$result} 把Symbol result 赋值给 Variable vResult
- 第13行:调用${vResult},见图5,执行前执行后,它的值都是$result
- 第17行:调用${vResult},见图5,vResult的值$result,$result执行时被slim识别是symbol,执行时被替换
执行结果如下:
图5 把Symbol赋值给Variable