使用與此類似的代碼,我能夠成功生成一個令牌并授權下面的請求。這兩種操作都發生在一個測驗用例中。我嘗試創建一個新的測驗用例,只復制第二個請求代碼,當然它說未經授權。所以我的問題是,既然我可以在第一個測驗用例中生成一個令牌,我必須添加什么才能使用/傳遞已經生成的令牌用于第二個、第三個、第四個測驗用例?
${data}= Create Dictionary Token_Name=TestTokenname grant_type=<grant type> client_Id=<your Id> Client_Secret=<Your client secret> scope=<your scpe>
${headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= RequestsLibrary.Post Request OA2 identity/connect/token data=${data} headers=${headers}
BuiltIn.Log To Console ${resp}
BuiltIn.Log To Console ${resp.status_code}
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} Testtokenname
${accessToken}= evaluate $resp.json().get("access_token")
BuiltIn.Log to Console ${accessToken}
${token}= catenate Bearer ${accessToken}
BuiltIn.Log to Console ${token}
${headers1}= Create Dictionary Authorization=${token}
RequestsLibrary.Create Session GT <Your Server URL> verify=${True}
${resp}= RequestsLibrary.Get Request GT <Your API URL> headers=${headers1}
Should Be Equal As Strings ${resp.status_code} 200 ```
uj5u.com熱心網友回復:
在您的代碼中,獲取令牌并將其存盤在變數中后,立即添加以下代碼行。使用此關鍵字設定的變數在所有后續測驗套件、測驗用例和用戶關鍵字中全域可用。
Set Global Variable ${token}
以下鏈接中的更詳細說明 https://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set Global Variable
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/478008.html
