

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 IDT 內容
<a name="idt-context"></a>

當 IDT 執行測試套件時，測試套件可以存取一組資料，用於判斷每個測試的執行方式。此資料稱為 IDT 內容。例如，由 `userdata.json` 檔案中的測試執行器提供的使用者資料組態，可用於在 IDT 內容中測試套件。

IDT 內容可以視為唯讀 JSON 文件。測試套件可以使用標準 JSON 資料類型，例如物件、陣列、數字等，從 擷取資料並將資料寫入內容。

## 內容結構描述
<a name="idt-context-schema"></a>

IDT 內容使用下列格式：

```
{
    "config": {
        <config-json-content>
        "timeoutMultiplier": timeout-multiplier
    },
    "device": {
        <device-json-device-element>
    },
    "devicePool": {
        <device-json-pool-element>
    },
    "resource": {
        "devices": [
            {
                <resource-json-device-element>
                "name": "<resource-name>"
            }
        ]
    },
    "testData": {
        "awsCredentials": {
            "awsAccessKeyId": "<access-key-id>",
            "awsSecretAccessKey": "<secret-access-key>",
            "awsSessionToken": "<session-token>"
        },
        "logFilePath": "/path/to/log/file"
    },
    "userData": {
        <userdata-json-content>
    }
}
```

`config`  
來自 [`config.json` 檔案](set-custom-idt-config.md#config-json-custom)的資訊。`config` 欄位也包含下列其他欄位：    
`config.timeoutMultiplier`  
測試套件所使用的任何逾時值的乘數。此值由 IDT CLI 的測試執行器指定。預設值為 `1`。

`device`  
為測試執行選取的裝置的相關資訊。此資訊等同於所選裝置[`device.json`檔案中](set-custom-idt-config.md#device-config-custom)的`devices`陣列元素。

`devicePool`  
為測試執行選取之裝置集區的相關資訊。此資訊等同於在所選裝置集區`device.json`檔案中定義的最上層裝置集區陣列元素。

`resource`  
檔案中資源裝置的相關資訊`resource.json`。    
`resource.devices`  
此資訊等同於 `resource.json` 檔案中定義的`devices`陣列。每個`devices`元素都包含下列其他欄位：    
`resource.device.name`  
資源裝置的名稱。此值會設定為 `test.json` 檔案中的`requiredResource.name`值。

`testData.awsCredentials`  
測試用來連線至 AWS 雲端的 AWS 登入資料。此資訊是從 `config.json` 檔案取得。

`testData.logFilePath`  
測試案例寫入日誌訊息的日誌檔案路徑。測試套件會在檔案不存在時建立此檔案。

`userData`  
[`userdata.json` 檔案中](set-custom-idt-config.md#userdata-config-custom)測試執行器提供的資訊。

## 存取內容中的資料
<a name="accessing-context-data"></a>

您可以使用 JSON 檔案的 JSONPath 表示法，以及使用 `GetContextValue`和 `GetContextString` APIs 的可執行文字來查詢內容。用於存取 IDT 內容的 JSONPath 字串語法會有所不同，如下所示：
+ 在 `suite.json`和 中`test.json`，您可以使用 `{{query}}`。也就是說，請勿使用根元素`$.`來啟動表達式。
+ 在 中`test_orchestrator.yaml`，您使用 `{{query}}`。

  如果您使用已棄用的狀態機器，則在 中`state_machine.json`，您可以使用 `{{$.query}}`。
+ 在 API 命令中，您可以使用 `query`或 `{{$.query}}`，視命令而定。如需詳細資訊，請參閱 SDKs中的內嵌文件。

下表說明典型 JSONPath 表達式中的運算子：


| Operator  | Description  | 
| --- |--- |
| \$1 | The root element. Because the top-level context value for IDT is an object, you will typically use \$1. to start your queries. | 
| .childName | Accesses the child element with name childName from an object. If applied to an array, yields a new array with this operator applied to each element. The element name is case sensitive. For example, the query to access the awsRegion value in the config object is \$1.config.awsRegion. | 
| 【開始：結束】 | Filters elements from an array, retrieving items beginning from the 入門 index and going up to the end index, both inclusive. | 
| 【index1、 index2、...、 indexN】 | Filters elements from an array, retrieving items from only the specified indices. | 
| 【？(expr)】 | Filters elements from an array using the expr expression. This expression must evaluate to a boolean value. | 

若要建立篩選條件表達式，請使用下列語法：

```
<jsonpath> | <value> operator <jsonpath> | <value> 
```

在此語法中：
+ `jsonpath` 是使用標準 JSON 語法的 JSONPath。
+ `value` 是使用標準 JSON 語法的任何自訂值。
+ `operator` 是下列其中一個運算子：
  + `<` （小於）
  + `<=` （小於或等於）
  + `==` （等於）

    如果表達式中的 JSONPath 或 值是陣列、布林值或物件值，則這是您唯一可以使用的支援二進位運算子。
  + `>=` （大於或等於）
  + `>` （大於）
  + `=~` （規則表達式比對）。若要在篩選條件表達式中使用此運算子，表達式左側的 JSONPath 或值必須評估為字串，而右側必須是遵循 [RE2 語法](https://github.com/google/re2/wiki/Syntax)的模式值。

您可以使用格式為 \$1\$1*query*\$1\$1 的 JSONPath 查詢，做為`test.json`檔案中 `args`和 `environmentVariables` 欄位的預留位置字串，以及`suite.json`檔案中 `environmentVariables` 的欄位。IDT 會執行內容查詢，並將查詢的評估值填入欄位。例如，在 `suite.json` 檔案中，您可以使用預留位置字串來指定環境變數值，這些值會隨每個測試案例而變更，IDT 會將每個測試案例的正確值填入環境變數。不過，當您在 `test.json`和 `suite.json` 檔案中使用預留位置字串時，下列考量適用於您的查詢：
+ 在所有小情況下，您必須每次在查詢中出現 `devicePool`金鑰。也就是說，請`devicepool`改用 。
+ 對於陣列，您只能使用字串陣列。此外，陣列使用非標準`item1, item2,...,itemN`格式。如果陣列只包含一個元素，則會將其序列化為 `item`，使其與字串欄位不區分。
+ 您無法使用預留位置從內容擷取物件。

由於這些考量，我們建議您盡可能使用 API 來存取測試邏輯中的內容，而不是 `test.json` 和 `suite.json` 檔案中的預留位置字串。不過，在某些情況下，使用 JSONPath 預留位置來擷取單一字串以設定為環境變數可能會更方便。