来自RobotFramework变量部分的Python方法

2024-10-03 19:19:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为获取属性.py,因为我只有一种方法!在

import configparser

def desiredCapability(platform, key):
    conf= configparser.RawConfigParser()
    if platform.lower() == "android":
      conf.read("somepath")
    elif platform.lower() == "ios":
      conf.read("some path")
    else:
      return None
    strr=conf.get( "main",key)
    return strr    

文件中有变量

^{pr2}$

当我试图调用变量部分中的方法时,它将被视为字符串!我可以毫不费力地调用测试用例部分中的方法!但我希望它出现在变量部分!在


Tags: 方法keypyimportreadreturn属性conf
2条回答

在我看来,您希望能够在给定某个变量元素的情况下加载具有特定内容的变量。按照你选择的方法,这是不可能的,正如@Bryan Oakley解释的那样。在

也就是说,有可能有可变的数据集,我们大多数人每天都在使用它们。通常,用例是有一组可以在多个环境下运行的测试。每个环境都有不同的URL、凭据和其他属性。在

一种方法是使用变量文件从命令行加载一组变量。Documentation on Variable files给出了几种方法:

  1. 在Python中定义一个Python文件并引用它。在
  2. 在YAML变量文件中定义变量。在
  3. 在Python类或Python函数中定义变量。在

前两个包含固定变量,最后一个可以接受参数并根据输入返回值。在下面的示例中,我们使用Python函数返回${name}变量,该变量具有给定的特定输入值。在

变量_文件.py

def get_variables(platform=None, key=None):

    if platform.lower() == "android":
        variables = {'name' : 'android'}

    elif platform.lower() == "ios":
        variables = {'name' : 'ios'}

    else:
        variables = {'name' : 'No Device'}    

    return variables

变量_文件.robot

^{pr2}$

在本例中,变量文件被引用为Variables ${EXECDIR}/variable_file.py ios,参数为ios。这将导致${name}变量保存值ios。在

不能调用Variables表中的关键字。从robot framework user guide

The most common source for variables are Variable tables in test case files and resource files. Variable tables are convenient, because they allow creating variables in the same place as the rest of the test data, and the needed syntax is very simple. Their main disadvantages are that values are always strings and they cannot be created dynamically. If either of these is a problem, variable files can be used instead.

相关问题 更多 >