Robot framework -- evaluate with 0.1 million Performance

Source: Internet
Author: User
Tags timedelta

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

 

This section focuses on evaluate, a common keyword.

I think this keyword is almost omnipotent in RF. As long as you understand the python syntax, evaluate allows you to directly run Python scripts. In this way, when the library does not provide you with the method you want, you can directly use the Python script to implement the method you want.

Below we will list some common usage examples. This is just an example I have demonstrated. There are more practical uses. You are welcome to study it on your own. If you are not familiar with the python syntax, go to the python help documentation.

 

1. Generate a random number

First, we will introduce this keyword at the same time as the first usage.

Evaluate has two parameters: expression and module. In the dialog box that appears. You can perform logical operations to obtain true or false results, such as Example 1. You can also call basic functions of Python, such as Example 2. You can also call a specific library, for example, 3. You can also call multiple databases, for example, 4.

However, because evaluate can only process one line of code (for personal understanding), it is better to put complicated code into the test library for implementation.

Next we will talk about the first one, which generates a random number and an integer. In fact, it is as simple as the example. It is useless and complex. You can modify the random range as needed.

 

The running result is:

 

2. String

It mainly finds some string collections through reading Python help.

 

The above mainly lists the constants in string, which is equivalent to a set of different characters. What is the purpose? The second line shows a simple application. Check the running result:

Each is a collection of characters. You can find a translation for English.

PS: I don't know what the above Code is like...

 

3. generate random strings

As I said just now, what is the use of so many constants in the 2nd s? The following is a combination.

I first select a constant of a string, then determine its length, set the number of characters to be generated, and then generate the random string I need using loops. In fact, this method is implemented in my qtlibrary, but I just want to try it. The same method can be implemented in RF.

Let's take a look at how similar methods are implemented in my qtlibrary. The core code is similar, and the actual code is slightly different. Although the implementation in RF is a little complicated, it can also be implemented. I have to lament the power of RF. Someone has asked me about the features of RF. My understanding is: "write code like a case, and write a case like a case".

Okay, let's run the case in RF. A lot of logs are added in the middle, and only the preceding and final running results are taken:

 

4. Remove spaces on both sides of the character and other character operations.

Row 3 removes spaces from the string, and row 3 replaces the character.

Lines 7th and 11 are used to handle some Chinese display. I will have the opportunity to elaborate on Chinese processing in the future. Although Python itself is not very good at Chinese processing, sometimes it will still have some problems, however, the 11th lines can still convert some Chinese Unicode codes into Chinese characters (it is strange that some colleagues have the same configuration on their computers, but some errors are always reported in Chinese processing ).

The second row counts the number of "rows" in the string.

The last few lines are a string connection and string replacement.

Other lines may be skipped because the redundant code previously written is not processed. This is written together in the past, so I am too lazy to separate them. Let's take a look.

Well, let's take a look at the running results against the points in the previous column:

We can see that AAA has spaces on both sides at the beginning, and there is no space after processing. Compare the rest of the scripts to see the results:

 

5. Date addition and subtraction

In fact, I discussed the date in the group yesterday. I sorted out three types:

A. If there is no requirement on the date, simply write it to death. I will not list this. It should be difficult. Contact me if necessary.

B. If you need to use the current date for addition and subtraction, you can directly use the get time keyword. Later, you can follow the value you need to return and the addition and subtraction of the date. For example, the first line is to add a day.

C. If a specified date is required for addition or subtraction, the specified date may be obtained from the page. This problem can be solved from row 2nd to row 7th. Of course, I am taking this example now. If you are lazy, you will use the system date. If you get it from the page, that is, replace row 2nd and row 3rd with the script you get on the date.

Some commands are relatively long and cannot be completely displayed. I will write them below.

Line 3 code:

 

[Python]View plaincopyprint?
  1. Datetime. date (INT ('$ {year}'), INT ('$ {month}'), INT ('$ {day}') + datetime. timedelta (days = int ('$ {adddays }'))

 

$ {Adddays} is used for addition and subtraction. It can also be written as-2, which will be reduced by 2 days after calculation. If you want to add or subtract other values, such as hour, minute, and second, modify the days = parameter in timedelta. For more information, see the python help documentation.

By the way, why is it written in this way with quotation marks? First, datetime. several parameters of date must be of the int type, so I used the int variable, but later I found that sometimes some errors occur during int, and I do not remember the cause of the error, but some machines report errors, some do not report, and then add quotation marks to the variables to solve this problem. Please try it yourself.

Line 3 code:

 

[Python]View plaincopyprint?
  1. Time. strftime ("% Y-% m-% d-% H-% m-% s ")

You can format the time display as needed. The following shows the running result:

 

 

6. Execute the doscommand

 

Is it amazing that you can execute the doscommand in RF?

In the hosts file.

Run the following command first:

The results are strange. What does that mean? Based on the results, I only know that 0 is successful, and 1, 2, or so are unsuccessful. Later I checked with du Niang:

Therefore, our first command to delete a directory must have failed. Why? In fact, I put a file in the directory. Anyone who has used the DOS RD Command knows that the directory to delete this command must be empty unless you include the/S parameter, even if/s is included, a confirmation process is required, so/Q is required.

Although the first failed, but the second succeeded, because he returned 0, so let's take a look at the second result:

As you can see, the current directory of DIR is actually the directory where the test script is located.

Then I modified the script and deleted the file first. Of course, you can add the/S/Q Parameter as I mentioned earlier.

Run the command again and check the result:

 

7. Print the system value

This was followed by the questions asked by friends in the group not long ago.

The results of these values are slightly different. You can choose to use them as needed:

 

To sum up, the evaluate method is very useful. You can use it as much as you want. But remember to start with, if it is particularly complicated logic, we should use Test Library for implementation. I hope this article will help you.

Robot framework -- evaluate with 0.1 million Performance

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.