Qtp 简明教程
QTP - Environment Variables
QTP 环境变量是可由所有操作、函数库和恢复场景访问的特殊类型的变量。Windows 中有内置的环境变量,可供在该特定系统上运行的所有应用程序使用,但 QTP 环境变量在运行时仅可供该特定测试脚本使用。
QTP environment variables are special types of variables that can be accessed by all actions, function libraries, and recovery scenarios. There are in-built environment variables for Windows that are available to all the applications running on that particular system, but QTP environment variables are only available to that particular test script during run-time.
Types of Environment Variables
Built-in Environment Variables −提供一系列环境参数,这些参数可提供诸如测试名称、操作名称、测试路径、本地主机名、操作系统名称、类型及其版本之类的信息。可以通过导航到“文件”→“测试设置”→“环境”选项卡来访问环境变量名称。
Built-in Environment Variables − provides a range of environment parameters that can provide information such as test name, action name, the test path, local host name, the operating system name, type and its version. The Environment Variable names can be accessed by navigating to "File" → "Test Settings" → "Environment" Tab.
User defined Internal − 在“环境”选项卡窗口中选择“用户定义”可以保存用户定义变量。单击“+”按钮可输入“Parameter Name”(参数名称)和“Value”(值),如下所示:
User defined Internal − User defined variables can be saved by selecting "User Defined" in the Environment Tab Window. The "+" button is clicked to enter Parameter Name and Value as shown below −
User Defined External − 用户定义变量可以作为一个 .xml 文件存储在外部文件中,并且可以加载到测试中,如下面的图片所示。在运行时,它也可以动态地加载,如在下面的示例之一中所解释的。
User Defined External − User Defined Variables can be stored in an external file as a .xml file and can be loaded into the test as shown in the figure given below. It can also be loaded dynamically during run-time as explained below in one of the examples.
Environment Variables – Supported Methods
1. ExternalFileName Property − 返回“测试设置”对话框“环境”选项卡中指定的已加载外部环境变量文件的名称。如果没有加载外部环境变量文件,此属性将返回一个空字符串。
1. ExternalFileName Property − Returns the name of the loaded external environment variable file specified in the Environment tab of the Test Settings dialog box. If no external environment variable file is loaded, this property returns an empty string.
x = Environment.ExternalFileName
print x
2. LoadFromFile Method − 在运行时动态加载指定的(.xml)环境变量文件。使用此方法时,无需手动将环境变量添加到“环境”选项卡中。
2. LoadFromFile Method − Loads the specified environment variable file (.xml) dynamically during run time. When using this method, the environment variables need not be added manually into the Environment Tab.
Environment.LoadFromFile "D:\config.xml"
b = Environment.Value("Browser")
print b
3. Value Property − 检索环境变量的值。我们还可以使用此属性设置用户定义内部环境变量的值。
3. Value Property − Retrieves the value of environment variables. We can also set the value of user-defined internal environment variables using this property.
' Get the Value of the InBuilt Environment Variables
a = Environment.Value("OS")
print a
b = Environment.Value("ActionName")
print b
'Loaded from External File
Environment.LoadFromFile "D:\config.xml"
c = Environment.Value("Browser")
print c