写在最前面:
FOR LOOP有2中格式,在3.1版本之前仅支持旧格式,3.1及之后能支持2中格式
RF的文档可以从此处获取,各个版本都有:
http://robotframework.org/robotframework/
旧格式:
:FOR ${item} IN @{bugs}
\ ${all}= Set Variable <a href="${item['url']}">${item['key']}</a> ${all}
新格式:
FOR ${item} IN @{bugs}
${all}= Set Variable <a href="${item['url']}">${item['key']}</a> ${all}
END
# 也可以不用缩进
FOR ${item} IN @{bugs}
${all}= Set Variable <a href="${item['url']}">${item['key']}</a> ${all}
END
3.1文档中的原文摘抄如下:
For loop syntax was enhanced in various ways in Robot Framework 3.1. The most noticeable change was that loops nowadays end with the explicit END
marker and keywords inside the loop do not need to be indented. In the space separated plain text format indentation required escaping with a backslash which resulted in quite ugly syntax:
*** Test Cases ***
Example
:FOR ${animal} IN cat dog
\ Log ${animal}
\ Log 2nd keyword
Log Outside loop
Another change, also visible in the example above, was that the for loop marker used to be :FOR
when nowadays just FOR
is enough. Related to that, the :FOR
marker and also the IN
separator were case-insensitive but nowadays both FOR
and IN
are case-sensitive.
Old for loop syntax still works in Robot Framework 3.1 and only using IN
case-insensitively causes a deprecation warning. Not closing loops with END
, escaping keywords inside loops with \, and using :FOR
instead of FOR
are all going to be deprecated in Robot Framework 3.2. Users are advised to switch to the new syntax as soon as possible.
简单翻译下:
在RF3.1中对FOR LOOP进行了加强。其中最显著的变化是,新版的FOR LOOD是以一个关键字 END 来表示结束的,且循环体内的关键字不用在用空格进行缩进了(不要空格缩进,也就不需要使用\来进行转义了,这个转义语法太恶心了)
另外一个变化是,旧版的 :FOR被FOR所代替,不再需要哪个前置的冒号了。 同时,在旧版中 :FOR 和 IN 都是不区分大小写的,但是新版中的 FOR 和 IN 都是区分大小写的,这个要注意。
在RF3.1中,旧版的FOR LOOP还能使用,但是如果使用旧版时没有用大写,会有一个warning(Using 'in' as a FOR loop separator is deprecated. Use 'IN' instead.)。 旧格式在将来的3.2版本中,将不再建议使用,希望大家尽快使用新的格式。