代码之家  ›  专栏  ›  技术社区  ›  Sree

在使用相对路径并将测试数据从json文件映射到请求时面临挑战

  •  0
  • Sree  · 技术社区  · 8 年前

    在使用相对路径和从JSON文件映射测试数据时,我遇到了一些问题。我有一个JSON POST请求和一个JSON格式的测试数据文件。

    这是我正在使用的测试数据。

        {
      "name": "Test Data",
      "description": "Information's mainly related with Users",
      "testData": [
        {
          "Scenario1": {
            "givenName": "Joseph",
            "familyName": "George",
            "addressType": "Current",
            "lineOne": "BNRA-222, Kowdiar lane",
            "cityName": "Trivandrum",
            "countryID": "India",
            "postcode": "695006"
          }
        },
        {
          "Scenario2": {
            "givenName": "Sreenath",
            "familyName": "Bhasi",
            "addressType": "Current",
            "lineOne": "HSE-123, Karyavatom",
            "cityName": "Trivandrum",
            "countryID": "India",
            "postcode": "695552"
          }
        }
      ]
    }
    

    这是功能文件

        Feature: Test using the Data from a JSON file
    
        Background:
        * def baseJsonRequest = read('../requests/jsonrequest.json')
        * def baseData = read('../data/sampledata.json')
        * def endPointURL = endPointURI + path
    
       Scenario: A sample scenario to test the data parametrization
    
        Given url endPointURL
        And request baseJsonRequest
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.givenName = baseData.testData[*].Scenario1.givenName
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.familyName = baseData.testData[*].Scenario1.familyName
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.addressType = baseData.testData[*].Scenario1.addressType
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.lineOne = baseData.testData[*].Scenario1.lineOne
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.cityName = baseData.testData[*].Scenario1.cityName
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.countryID = baseData.testData[*].Scenario1.countryID
        * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson.residenceAddress.postcode = baseData.testData[*].Scenario1.postcode
    

    我的问题是:

    1. 我无法给出双方的相对路径。相对路径将返回一个json数组。例如,我不能使用$。。场景1.givenName,这让我写更长的路径。
    2. 在每个场景中都包含这种映射实际上是困难的。我们如何实现这方面的参数化解决方案。有什么更好的方法?我可以使用特征文件调用数据读取并将信息传递给另一个特征吗。如果可能的话,我需要参数化。怎么做?
    3. 或者我需要使用java类来读取JSON文件吗?
    1 回复  |  直到 8 年前
        1
  •  1
  •   Peter Thomas    8 年前

    是的,当您在JsonPath中有一个通配符时,它就会返回一个数组。无论如何,这里有两点应该很有帮助:

    • 可以将重复的嵌套路径移动到表中- set
    • 您可以通过分配给变量来引用JSON的嵌套块

    所以这应该是一条路:

    * def first = get[0] baseData.testData[*].Scenario1
    * set baseJsonRequest.autoRequest.applicants.applicant.specifiedPerson
      | path                         | value             | 
      | familyName                   | first.familyName  |
      | residenceAddress.addressType | first.addressType |
    

    我会尽量不使用通配符,例如。

    * def first = $baseData.testData[0].Scenario1
    

    希望这有帮助!