代码之家  ›  专栏  ›  技术社区  ›  AutoTester213

Robot框架-如何在行列表中获取值

  •  1
  • AutoTester213  · 技术社区  · 6 年前

    我正在robot框架中运行一个查询,从数据库中获取一个id,这个查询可以工作,但是我的返回值是 'locationId': ['[('somekindofIdhere', )] 我只需要 somekindofIdhere 价值观,

    我已经试着得到列表的第一个索引

    Verify Location Details
         @{Get_LocationId}=  Query  Select locationId From Location WHERE LocationName = '${Name}'
         ${Response_Location}=  Rest.get  http://sampleurl/sample/locations/@{Get_LocationId}[0]
    

    但这又给了我这个 ('somekindofIdhere', ) () 角色,只得到价值?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Bryan Oakley    6 年前

    查询返回列表列表-外部列表是查询返回的行列表,内部列表是列列表。即使查询在单个行中返回单个列,也会出现这种情况。可以使用robot的嵌入变量语法来引用所需的值:

    ${Response_Location}=  Rest.get  http://sampleurl/sample/locations/${Get_LocationId[0][0]}
    

    下面是一个完整的例子来说明两者的区别。我在模拟数据 @{Get_LocationId} 这样测试就不需要实际的数据库连接。

    *** Test Cases ***   
    Example
        # simulate a list of tuples as returned by a db query    
        ${Get_LocationId}=  evaluate  [('somekindofIdhere',)]
    
        should be equal as strings  ${Get_LocationId}        [('somekindofIdhere',)]
        should be equal as strings  @{Get_LocationId}[0]     ('somekindofIdhere',)
        should be equal as strings  ${Get_LocationId[0]}     ('somekindofIdhere',)
        should be equal as strings  ${Get_Locationid[0][0]}  somekindofIdhere