簡體   English   中英

如何將參數直接從json文件傳遞到機器人框架中的關鍵字?

[英]How to pass arguments directly from json file to keyword in robot framework?

我有一個像這樣的Json文件.PFB代碼:

"properties " : {
    "xyz" : {
        "username" : "abc@gmail.com",
        "password" : "abc@123",
        "phonenumber" : "1235",
    },
      "ABC" : {
        "username" : "abc@gmail.com",
        "password" : "abc@123",
        "phonenumber" : "1345",
    },

關鍵字如下:

Do operation for properties
  [Arguments]  ${username}  ${password}  ${phonenumber}
  Log  ${username}
  Log  ${password} 
  Log  ${phonenumber}

我的問題是:

1)json文件包含很多東西,但我只需要從文件中獲取屬性。我將如何從整個json文件中獲取屬性,並將參數如username,password,phonenumber直接傳遞給上述關鍵字。

2)如何為此邏輯編寫關鍵字,以便我們僅更改json文件以添加除xyz,abc之外的更多屬性,我們將添加任意數量的屬性,它將自動獲取,並且關鍵字為所有屬性提供所需的結果,無論如何我們正在json文件中進行修改。

如果我正確理解您的問題,那么您的問題摘要是:a)如何以這種形式在Robotframework中讀取和解析json文件,以及b)將每條記錄的屬性傳遞給此關鍵字。

可以使用Get File從文件系統Get File

可以使用python的json模塊讀取一個json文件,更具體地說是loads()方法-它需要一個字符串,並返回一個python對象。

您的“ json”樣本是完全無效的json,因此讓我們假設“屬性”位於文件內部某處(深度3級)。

${the file as string}=    Get File    c:\\the\\path\\to\\the\\file.json
${parsed}=    Evaluate    json.loads("""${the file as string}""")    json
${properties}=    Set Variable    ${parsed["top"]["child"]["properties"]}

現在變量properties是一個字典,具有這兩個鍵-“ ABC”和“ xyz”; 您只需對其進行迭代,然后將每個子詞典的子關鍵字傳遞給關鍵字。

FOR    ${key}    IN    @{properties}
  ${sub dict}=    Get From Dictionary    ${properties}    ${key}
  Do operation for properties    ${sub dict}[username]    ${sub dict}[password]    ${sub dict}[phonenumber]
END

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM