如何在robotframework中使用循环比较列表中的每个字典元素

时间:2017-12-11 23:38:19

标签: robotframework

如何比较Web服务响应中每个字典列表项与存储在文件中的json数据,这些数据具有相同的字典列表项。

*** Settings ***
Library  Collections
Library  OperatingSystem
Library  RequestsLibrary

*** Test Cases ***
TestCase1
    # create a HTTP session to a server
    Create Session  country  http://services.groupkt.com/country/get/all
    ${response}=  Get Request  country  /
    log list  ${response.content}
    ${data}=  evaluate    json.loads($response.content)    json
    ${RestResponse}=  get from dictionary  ${data}  RestResponse
    ${result}=  get from dictionary  ${RestResponse}  result
    : FOR  ${result}  in  @{result}
    \   log to console  ${result}
    # ${result} display each dictionary item in list
    log to console  \n..Read from file
    ${file}     get file    ${CURDIR}${/}/country.json
    #country file is respose of http://services.groupkt.com/country/get/all
    ${filedata}=  evaluate  json.loads($file)  json
    ${RestResponse}=  get from dictionary  ${filedata}  RestResponse
    ${resultfiles}=  get from dictionary  ${RestResponse}  result
    : FOR  ${resultfile}  in  @{resultfiles}
    \   log to console  ${resultfile}
# ${resultfile} display each dictionary item in list

这里第一个和第二个循环显示每个字典项目,我想比较来自${result}${resultfile}的每个项目来自两个不同的循环。 我是否需要使用循环遍历,但这对我来说也不清楚。 我看到另一个问题${resultfile}附带了一些字典元素的unicode

1 个答案:

答案 0 :(得分:0)

我在RobotFramework中找不到解决方案所以我只是使用自定义python库来使其工作。

*** Settings ***
Library  Collections
Library  OperatingSystem
Library  RequestsLibrary
Library  compare.py

TestCase1
    # create a HTTP session to a server
    Create Session  country  http://services.groupkt.com/country/get/all
    ${response}=  Get Request  country  /
    ${data}=  evaluate    json.loads($response.content)    json
    ${RestResponse}=  get from dictionary  ${data}  RestResponse
    ${results}=  get from dictionary  ${RestResponse}  result
    log  ${results}
    : FOR  ${result}  in  @{results}
    \   log  ${result}
    # ${result} display each dictionary item in list
    log to console  \n..Read from file
    ${file}     get file    ${CURDIR}${/}/country.json
    #country file is respose of http://services.groupkt.com/country/get/all
    ${filedata}=  evaluate  json.loads($file)  json
    ${RestResponse}=  get from dictionary  ${filedata}  RestResponse
    ${resultfiles}=  get from dictionary  ${RestResponse}  result
        log   ${resultfiles}
    : FOR  ${resultfile}  in  @{resultfiles}
    \   log   ${resultfile}
    ${ReverseUnicode}  ChangeFromUnicode   ${resultfiles}
    log  ${ReverseUnicode}
    #compare display all dictionary element which is not matching 
    ${Compare}   compareList  ${ReverseUnicode}   ${results}
    log to console   ${Compare}


    here is Python library code  #compare.py
    import  ast
def ChangeFromUnicode(responselist):
    Returnlist= []
    for listelement in responselist:
        Ndictionary = {}
        for element1, element2 in listelement.iteritems():
            Ndictionary[element1.encode("ascii",'ignore')] = element2.encode("ascii",'ignore')
            Returnlist.append(Ndictionary)
    return Returnlist
def compareList(arg1,arg2):
    list1 = ast.literal_eval(arg1)
    list2 = ast.literal_eval(arg2)
    list3 = []
    for i, j in zip(list1, list2):
        if i != j:
            list3.append(i)