Robot framework -- 06 User Key Word

Source: Internet
Author: User

From: http://blog.csdn.net/tulituqi/article/details/7906130

 

When we do automated cases, the most commonly used keywords are user keywords. When it comes to keywords, it can be divided into test database keywords (or system keywords) and user keywords. The former is generally obtained by loading the Library, and the user keywords are generally obtained by loading resouce.

Take the previous case as an example:

The import variables and log in the figure are both Test Library keywords, And the search test keyword is the user keyword. As long as the ride version is higher, we can see that their colors are different.

 

Next, we will create a new case and introduce the usage of user keywords.

1. Create a new user keyword without arguments.

Under setting, documentation is not much said. Let's take a look at the following four settings. In fact, the previous article briefly introduced them. Let's talk about them here.

Arguments: set input parameters

Teardown: The action when the setting is complete. For example, if you write close all browsers, it indicates what keywords will be executed after the user keyword is executed.

Timeout: Set the timeout time. For example, 1 minute indicates 1 minute. If the keyword is executed for more than 1 minute, it is considered as failed.

Return Value: Set the return value.

 

For a long time, I think that user keywords are like a function. They include the input parameter arguments, return value, and teardown.

So we also need to use user keyword like a function.

 

2. input the arguments Parameter

Some parameter settings have been used in the previous case layering. You can refer to the previous two articles. Here we will mention the setting of multiple parameters and optional parameters.

 

0) variable type

By referring to the variable type, there are two types of variables in RF, one is scalar, which can be understood as single-value variables, and the other is list variable and list variables, or it can be understood as an array variable. The scalar variable starts with the $ symbol, and the list variable starts with the @ symbol. Here is a brief introduction. I will write a special article on the relationship between two variables later.

 

1) required parameters

Add four parameters for the keyword, which are separated by |. (If you want to use | as a value rather than a separator in the default value, use \ | to represent the value of |.

 

If you add parameters in this way, all these parameters are required. Let's create a case3 and call the keyword test. Here, the four grids after the keyword test are all red, the prompt parameter is required.

 

2) optional parameters

Suppose we want to set the 2nd parameters to an optional parameter. The optional parameter is to add the default value to the parameter, and the default value is =. If you want to leave it blank by default, only the equal sign is required.

(The default value does not need to be described in detail. If you do not specify a value for this optional parameter when calling this keyword, the default value is used as the parameter value)

If we add this directly, click OK and an error will be reported. It means that the required parameter cannot be followed by the optional parameter.

That is to sayIf a parameter is set to an optional parameter, all parameters following it must be optional and cannot be required.

Here we have 2nd parameters. If you want to select them, either set the default values for parameters 3 and 4, or adjust the 2nd parameters to the end. So that no error will be reported.

 

3) List variable parameters

The list variable can also be used as a parameter, but the list variable can only be placed in the last bit. If it is placed above, an error is reported.

I changed arg4 to a list variable such as @ {arg4}. When saving the list variable, the system will prompt that it can only be used as the last parameter.

Why is it the last one? In English, the last argument, no s ....

If you have to try to put the list variable in the last two, an error will also be reported.

 

Why? First, the list variable is variable, that is, the number of list members is uncertain. If list is used as a parameter, several members are equivalent to several single-value parameters. In fact, list provides a variable number of parameters. Since it is variable, if it is placed above (see the figure in the first example), he cannot determine which parameters are passed to arg4 and which one is to arg2. Similarly, if you put two lists, it will not work (see the figure in the second example), because it is impossible to tell which input parameters are for arg4 and which are for arg2.

And there is no difference between two variables and one variable, soThe final limit is that only one list parameter is allowed and must be placed at the end.

 

Note:No space is allowed between the variable name and equal sign. If there is space, the system reports a parameter syntax error..

 

Finally, let's take a look at the list parameter instances. The other instances should not be complicated. You just need to practice them yourself.

For convenience, I reduced the number of parameters by adding only two parameters, $ {arg1} |@{ arg2}, and then used a fail, the purpose is to print the value on the running interface.

 

Then we will pass several values to him in case3.

I also explained the keywords. You can see that there is a * Before agr2, which means that he can accept any parameter. in Python, this is actually the way to process multiple parameters.

Run the case and check the printed content.

 

Continue to introduce teardown and return value in user keyword.

 

3. the teardown test completes recycling and cleaning.

If you have used JUnit, you should remember that JUnit has a setup, a test, and a teardown case. Similarly, our testcase has these two cases, you can check the setting of case by yourself.

The teardown function of the user keyword is similar. If it is teardown of case, it is to clear the case at the end of the case; the teardown of user keyword is used to clear the data that will be collected after the user keyword is called.

Of course, it is not necessarily data collection and cleaning that can be adjusted based on your own needs. It is either a system keyword or a user keyword. In short, it can be understood that a keyword is required after the keyword is completed. The parameters must be separated by |.

Here I will rebuild the keyword Test Written in the previous article to demonstrate the role of teardown.

The effect is to first assign the value of arg1 to temp, and then assign the value of the third item in the arg2 list to arg1 (this is a small bug, if there are fewer than three parameters, an error is returned. This is only an example. Ignore it.) then, the log prints the values of arg1 and arg2.

In teardown, I used the set suite variable method to overwrite arg1 with the Temp value and set arg1 to a suite-level variable, then I call the keyword test in case3 and print the arg1 value with log. (You can also use set global variable. If you do not use these two variables, the arg1 variable cannot be found in case3)

 

Next, let's take a look at the case to see the report.

As mentioned above, first assign the 111 value of arg1 to temp, then assign the 3rd value of arg2 to arg1, and finally assign the Temp value to arg1.

Let's just clear this. Let's change the parameter value inside the keyword and restore the value back when teardown.

 

Teardown can only write one line of statements. If you want to execute multiple lines of statements, it is best to write another keyword and then call this new keyword in teardown.

 

4. Return Value

I don't need to explain this. I just want to explain the usage, because we can use single-value variables or list variables when passing in parameters, therefore, single-value variables and list variables can also be used in return value.

Here is an example.

 

1) single value variable return

I put arg1 in the return value, because I 'd like to see whether it is "return" or "teardown" first, but I think it should be "return" and "verify" by the way.

In case3, we use the variable arg5 to obtain the returned value and print it out. Other codes do not move.

After running, let's take a look at the log printing effect.

It seems that I guess it is wrong. It is returned only after teardown. You can see that the place I marked is that I first assigned a value of 111 to arg1, then the arg1 is returned to arg5, so the arg5 value is also 111. I thought it should be 444, so you should pay attention to it here.

 

This type of single-value variable returns a common one, which is rarely seen in the following several cases, but also useful.

 

2) Multiple Single-value variables are returned.

When setting the return value, you may have seen that multiple variables can be returned and separated by |.

2.1) Here we continue to transform and add a variable value to return.

 

Similarly, we also need to make the corresponding changes in case3. Of course, we can also do it without modifying it. Here we will be more free. Let's take a look. The figure is the same as case3 above. Let's look at the execution result directly.

Although we use $ {arg5}, because two values are returned, it is automatically converted to list (this is also the variable swap we will talk about in the next article ).

 

2.2) Some people may think that the returned list is here. We 'd better use the list variable to receive it, so we should change the first $ {arg5} to @ {arg5}, run it again, and check the result.

The difference is basically not big, but it is different from the display of $ {arg5} and @ {arg5.

 

2.3) there is another way, because we know there are two return values, so we can use two variables to get the value.

Run the command to check the log.

In this way, you can directly use the corresponding variables.

 

The first two methods are almost the same, and the third method is better to know the number of returned values, so that they can correspond one by one. What if they are different?

A: The number of returned values is greater than the number of value variables.

In fact, there is a similar in 2.1, $ {arg5} will be automatically converted to list. This is a variable with only one value. Try multiple variables.

In case3, two variable values are used. Run the command to check the result.

He will first give the first value to arg5, and then give the subsequent value to arg6, so arg6 becomes the list variable.

Therefore, we can conclude that if the number of multiple value variables is less than the number of returned values, he will first assign values to the previous value variables one by one, these variables are still single-value variables, and the last variable is converted to the list variable to receive the remaining values.

 

B: The number of returned values is less than the number of value variables.

Change the return value to only one $ {arg1}, and run the command to check the result.

In this case, an error is reported, because only one value is returned, and here we expect multiple values, such as list variables of list-like.

 

Therefore, it is important to know the number of returned values. If you are not sure about the number of returned values, it is best to use the list variable or a single variable to take values, as in the following case, avoid the case where the number of returned values is less than the number of variable values.

 

3) List variable return

Because the list variable itself is not sure how many Members there are, it is best to use the list variable or a single variable for this return value (that is, the usage of 2.1 and 2.2 ). It doesn't matter if one or more lists are returned in the return value, because it will be assembled into a large list.

Slightly transform a complicated one:

In the return value, we put a list variable and a single-value variable plus a list variable. In case3, we can use one variable.

Run the command to view the result.

Robot framework -- 06 User Key Word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.