From: http://blog.csdn.net/tulituqi/article/details/8038923
I. Branch
In versions earlier than robotframework2.7.4, we can directly use run keyword if to write if easily, but it is not that convenient if we want to write else if and Else, in earlier versions, if you want to write and determine the branch, you must write another run keyword if statement, and then write different conditions.
Fortunately, in version 2.7.4, the extension of the branch structure is added, that is, the else if and else are integrated based on the run keyword if statement.
1. Simple Branch
The branch structure is as follows:
Run keyword if condition 1 do Action1
... Else if condition 2 do Action2
<... Else if condition X do action x>
... Else do action n
Write an example first. This is also an example in the updated document.
Among them, the three vertices before else if and else must be vertices, otherwise they cannot be identified.
Change the value of $ {A} in the first line to 0, and then run it.
You can modify the variable values of $ {A} and $ {B} by yourself to verify whether the branch is correct and I will not display the results.
The structure given above is just a standard structure. You can modify it as needed.
For example, the intermediate else if can have more than one, and writing more is a bit like case Branch.
You can write only else if, or only Else.
If you only have else, you can also write a row. For example, if you cannot cut it down, what you cannot see next is 2)
2. Complex Branch
The above are relatively simple structures. You can also write Branch judgment for complicated structures.
Because too many columns are displayed in the table, it is inconvenient to directly use the source code.
It should be noted that a run keyword if is added in the second else if, and a run keyword if with else is added in the last else (the source code breaks log 8, actually they are in a row)
After my experiment, you can only add run keyword if in else if, but not with else.
In else, you can add run keyword if with an else at the same time.
Well, I don't know if you understand it. It's really hard to read this complicated structure. It's not necessary to write it into a standard structure, the complex structure can certainly be organized into multiple else if conditions or integrated, which looks much clearer.
I tried to integrate the above branch and it looks much clearer.
Of course, I am just an example. I think the integrated code is slightly different from the original intent, mainly in $ {B} <= 7. Okay, blame me for writing too complicated.
If you really want to write such a complex statement, write it in a complicated way. Maybe you can clearly write your judgment logic in the document so that others can easily understand it.
3. Condition Conditions
Basically, the Branch is about to talk about it. I also want to add the condition method. You can also press F5 to view it. In the help of run keyword if, we mentioned:
Runs the given keyword with the given arguments, if 'condition' is true.
The given 'condition' is evaluated similarly as with 'could be true' keyword, and 'name' and '* ARGs 'have same semantics as with 'run keyword '.
His condition will be evaluated, similar to the keyword shocould be true, so let's continue to look at the help of this keyword:
If 'condition 'is a string (e.g. '$ {RC} <10'), it is evaluated as a python expression using the built-in 'eval' function and the keyword status is decided based on the result. if a non-string item is given, the status is got directly from its truth value as explain at http://docs.python.org/lib/truth.html.
In fact, condition uses the python syntax for judgment, which is equivalent to executing evaluate (as described in the next lecture ). The specific information can be found at http://docs.python.org/lib/truth.html.
Commonly used Stickers:
Operation |
Result |
Notes |
X or Y |
IfXIs false, thenY, ElseX |
(1) |
X and Y |
IfXIs false, thenX, ElseY |
(2) |
Not x |
IfXIs false, thenTrue, ElseFalse |
(3) |
Operation |
Meaning |
Notes |
< |
Strictly less |
|
<= |
Less than or equal |
|
> |
Strictly greater |
|
> = |
Greater than or equal |
|
= |
Equal |
|
! = |
Not equal |
|
Is |
Object Identity |
|
Is not |
Negated Object Identity |
|
! = It is also available <>, but it is recommended to use it! =
You can add mathematical operations to the conditions. I will not list them one by one.
Ii. Loop
This function has always been supported in the system, but the for syntax is a little special. You need to add a colon to the front. The keyword cannot be found directly in the F5 keyword search, but the exit for loop can be found in the keyword search. At that time, when we saw this keyword, we thought that there should be a for loop, otherwise, there will be no keyword to exit the loop.
The for loop structure is as follows:
: For Loop Variable in range end
: For Loop Variable in range start end step
The loop body is the indent code next to it
Or
: For loop variable in a <B, c,...>
The loop body is the indent code next to it
Or paste two examples first.
1. In range:
In range is mainly a counter that uses an integer sequence as a loop. In range 10 is run from 0 to 9. If in range 1 10 is written, it starts from 1 to 9.
It should be noted that the number at the end of range is not executed, that is, 10 is not executed, and it ends at 9. If you want to execute 10, write in range 11.
If there is only one number N after the in range, it is an integer sequence from 0 to the N-1.
Of course, since it is a sequence, you can also add step, that is, the sequence interval. If you want to add a step, you must write both the start and end numbers, and then write the step at the end. It can be either a positive number or a negative number, for example, a negative step:
2. In:
The usage of in is relatively free. You can understand the content after in as all elements of a list variable. This loop is equivalent to traversing each element. The element here can be a string or a value.
Therefore, you can directly write the elements as I did, or directly put a list variable. For example:
The running result is as follows:
The specific use of in or in range depends on the specific needs. I only provide examples. You can choose a specific method on your own.
For example, if you write in 1 2 3 4 5 6 7 8 9, it is better to directly use in range 1 10. The results are the same. Lazy people like me are certainly willing to use the latter.
3. Dual Loop
The for loop previously written to vbs can be written as follows:
For I = 1 to 10
For j = 1 to 8
Do something
Next J
Next I
However, at present, the For Loop in RF does not support this function directly, but indirectly.
First, add another user keyword, for example, my forj
Then call this forj in the previous for loop to achieve the above effect.
The running result is too long. Just paste the last part.
If you want to use the value of the variable $ {I} In forj, you can add an input parameter to forj and pass $ {I} to it. Let's do it by yourself.
Robot framework -- 09 branch and loop usage