2017-12-11 1 views
0

辞書の同じリスト項目を持つファイルに格納されているjsonデータに対するWebサービス応答から辞書の各リスト項目を比較する方法。ここでrobotframework内のループを使用して辞書内リストの各要素を比較する方法

*** 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}から来ています。 ループオーバーループを使用する必要がありますが、それはまた私にとっては明らかではありません。 と私は私がちょうどそれを動作させるためにカスタムPythonライブラリを使用してRobotFrameworkで解決策を見つけるいけない別の問題${resultfile}辞書要素

+0

最も簡単な解決策は、2つのjsonオブジェクトを比較できるキーワードをpythonに書き込むことです。あなたはそのような解決策に門戸を開いていますか? –

+0

@ブライアン私はRobotframeworkで行うことを好むでしょう。もしRFに制限があるなら、私はPythonのカスタムライブラリソリューションで大丈夫です。 – madhur

+0

あなたの質問にお答えしますか?もしそうなら、関連情報を使って回答を作成できますか? –

答えて

0

のいくつかのUnicodeで来ている参照してください。

*** 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) 
関連する問題