Robot Framework Usage Tips

Source: Internet
Author: User

1, the use of variables

Variables can be set on the command line, individual variable settings use the--variable (-V) option, and the variable file selection uses the--variablefile (-V) option.
A variable that is set through the command line is a global variable and is valid for all of its execution cases. They will overwrite variables with the same name in the variable table or
A variable with the same name introduced into the test data through a variable file.
The syntax for setting a single variable is--variable name:value, "name" is a variable name that does not use ${}, and "value" is the value it assigns.
You can use this option more than once to set multiple variables. Only scalar quantities can use this syntax and only assign a value string. Many special characters are in the life
It is difficult to make representations in rows, but you can escape them with escaped characters using the-escape option.
--variable Example:value
--variable host:localhost:7272--variable User:robot
--variable escaped:qquotes_and_spacesq--escape quot:q--escape space:_

The basic syntax for using variable files on the command line is--variablefile path/to/variables.py

Variables created using Set test Variable can be valid anywhere within the scope of the test case. For example, you are in a user keyword
Creates a variable that is valid at the test case level and in all other user-defined keywords in the current test. For other tests
Example will not see the variable.
Variables created with set Suite Variable can be valid anywhere within the scope of the current test set. Use this keyword to create a variable with the
Use the settings table of the test data file to set the variable and introduce the variable file the same. Other keywords, including possible sub-test sets, will not
You can see the variables created by this keyword.
Variables created using Set Global Variable keyword are valid in all test cases and test sets. Variables created with this keyword
Use the command-line options-variable or-variablefile to create the same variables. Because the keyword can change the value of a variable anywhere, all
Need to use caution

Valid operating system built-in variables
Variable explanation
${curdir} provides an absolute path for the current test file to be stored. The variable is case-sensitive.
${tempdir} Gets the absolute path of the operating system Temp folder. The UNIX system is in/TMP, and the Windows system is in C:\Documents and
Settings\<user>\local Settings\Temp.
${execdir} Gets the absolute path of the test execution start directory. The variable is added in Robot Framework2.1.

Spaces and empty variables
You can use ${space} and ${empty} to create spaces and empty variables. These variables are useful, otherwise you need to use backslashes to escape spaces or
Empty cells. You can use this syntax ${space*5} when you need to use more than one space. As the following example, should be equal uses the same
But using this syntax is simpler and clearer than using backslashes.

Some of the automatic variables can be used in test cases. These variables will have different values when they are executed, and they are not at any time
and effective.
Variable explanation Available
${test name} Gets the current test case
@{test tags} contains tags in the current test set, sorted alphabetically by test case
${test STATUS}
Gets the status of the current test case, one of PASS or FAIL. Only in test cases
Used in the Teardown
Test
Teardown
${test Message} Possible error information for the current test case. Can only be used in Teardown of test cases
Test
Teardown
Variable explanation Available
The name of the test case before ${prev test, or an empty string "when no test case is executed
Over ". Can be used from anywhere
Everywhere
${prev TEST
STATUS}
The state of the previous test case, Pass,fail or an empty string, "when there is no
Use cases have been executed. " Can be used from anywhere
Everywhere
${prev TEST
MESSAGE}
Possible error message for the previous test case. can be used anywhere Everywhere
The full name of the current test set ${suite name}. Can be used from anywhere. Everywhere
${suite SOURCE}
The absolute path to the test set file or directory, Robot Framework2.5 new variable. OK
Used anywhere.
Everywhere
${suite Status} The state of the current test set, PASS or FAIL. Can only be used in the teardown of a test set.
Suite
Teardown
${suite message} All messages for the test set, including statistics. Can only be used in the teardown of a test set.
Suite
Teardown
${output FILE}
The absolute path of the current output file. When the output file is split, there will be different values. Can be in
Any place to use.
Everywhere
${log FILE}
The absolute path of the current log file or the string none "when there is no log file". When the loss
It is possible to have different values when the out is split.
Everywhere
${report file} The absolute path or string none of the current report files "when no report file is generated." Everywhere
The absolute path or string none of the ${summary file} digest files "when there is no digest file." Everywhere
${debug file} The absolute path or string none of the debug files "when there is no debug file." Everywhere
${output DIR} absolute path to output directory Everywhere

2. Use case Execution part

Specify test Data execution
The test case for RF can be created in a file or directory, followed by the name of the file or pathname that needs to execute the use case after running the script. The path can be
The absolute path, more generally, is the relative path. A file or directory constitutes a top-level test set that, if not overridden by--name option,
It will use the file or directory name as its own name. The different executions will be described in the following example. Only Pybot are used in this section
To, but the Jybot script uses a similar usage.
Pybot test_cases.html
Pybot path/to/my_tests/
Pybot C:\robot\tests.txt
We can run multiple files or directories by using a space partition to give the path to multiple files at once. RF can be produced automatically.
A top-level test set, and the specific file or directory becomes its child test set. The name of the resulting test set will be the name of the child test set and the "&"
Composition For example, in the first example below, the top test set name is My Tests & Your Tests. However, sometimes the automatically generated test
The name of the set will be very long and complex. Generally, it is better to overwrite the name with-NAME option, as shown in the second example:
Pybot my_tests.html your_tests.html
Pybot--name Example path/to/tests/pattern_*.html

Using Parameters
When parameters are used, the parameters must be in the middle of the run script and the source data. For example:
Pybot-l Debug My_tests.txt
Jybot--include Smoke--variable host:10.0.0.42 path/to/tests/

Setting parameter values
Most parameters require a parameter value, which is followed by the name of the parameter. Both long and short parameters accept parameter values separated by spaces, for example: "--include
Tag "or"-I tag ". With long parameters, you can use the equal sign "=" as a delimiter, such as "--include=tag", when using short parameters, divide
The delimiter can be omitted, such as "-itag".
Some parameters can be assigned more than once. For example, "--variable var1:value--variable var2:another" has two variables set. Such as
If a parameter with only one value is assigned multiple times, the last one takes effect.

Simple Regular Expressions
Many parameters can be followed by the parameter values of the regular expression. "*" and "? "Can be substituted for specific characters," * "can match any of the values including null
String, "? "Can match any single character. For example: "--include prefix-*" matches all tags that start with "prefix-", "--include
A??? "matches all tags starting with" a "with 4 characters.

Return code
The run script uses the return code to interact with the state and system of all test cases. If all use case execution starts successfully and there is no serious loss
The return code will be 0, all possible return codes are listed in the table below.
Return code
After the use case has been executed, it is easy to determine the status of all use case execution by a return code. For example, in a bash shell, the return code is stored in a
The variable "$?" in the body. In the Windows system, the return code is stored in the variable% ERRORLEVEL%. If you are using external work
Tool to run the use case, consult the relevant documentation for information on how to get the return value.

Start of Use case:

In the first example, use a different browser to do the same WEB test and merge the test results. Using shell scripts to implement this functionality is
It's very simple, just put the commands you need to use in a row.
#!/bin/bash
Pybot--variable browser:firefox--name Firefox--log none--report none--output out/fx.xml Login
Pybot--variable browser:ie--name IE--log none--report none--output out/ie.xml Login
Rebot--name Login--splitoutputs 1--outputdir out--output login.xml out/fx.xml out/ie.xml
It is also not very complicated to use batch files in Windows to accomplish these functions, and it is important to note that Pybot and Rebot are processed by batch
When you run other batch files, you must use call. When the first batch file finishes, the test will also end.
@echo off
Call Pybot--variable Browser:firefox--name Firefox--log none--report none--output out\fx.xml Login
Call Pybot--variable browser:ie--name IE--log none--report none--output out\ie.xml Login
Call Rebot--name Login--splitoutputs 1--outputdir out--output login.xml out\fx.xml out\ie.xml

Executing test sets and test cases
Test cases are always executed in a test set. The test set produced by the catalog contains a set of sub-tests consisting of test case files or directories,
Test case files consist of a test set that directly contains the test cases. By default, all test cases are executed and can be
"--test", "--suite," "--include", and "--exclude" to select Use case execution. Test set if there is no use case, it will not be executed.
RF is performed from the top-level test set. If the test set contains a use case, it is executed sequentially. If the test set contains test sets again, follow the
The depth-first order is executed sequentially. When a single use case is executed, the keywords are executed sequentially. Usually any one of the keywords in the use case fails,
The use case will stop executing, but we can do that after the failure. The following chapters will cover the exact sequence of execution and
Setups and teardowns.

Robot Framework Usage Tips

Related Article

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.