【问题标题】:How can I iterate over the test data using webdriverio with mocha?如何使用带有 mocha 的 webdriverio 迭代测试数据?
【发布时间】:2019-05-21 10:50:33
【问题描述】:

我有一个基于 JSON 的测试数据,我如何迭代这个测试数据以便为每个 cred 对象运行测试?

cred: {
        nameValue: 'ant',
        emailValue: 'ant@gmail.com',
        passwordValue: 'ant',
    },
cred: {
        nameValue: 'bat',
        emailValue: 'bat@gmail.com',
        passwordValue: 'bat',
     },

【问题讨论】:

    标签: mocha.js webdriver-io


    【解决方案1】:

    你的测试数据 JSON 文件应该是这样的,

    [
           {
            "nameValue": "ant",
            "emailValue": "ant@gmail.com",
            "passwordValue": "ant"
        },
         {
            "nameValue": "bat",
            "emailValue": "bat@gmail.com",
            "passwordValue": "bat"
         } 
    ]
    

    现在您可以通过索引访问它们(就像在数组中一样)

            const testDataObject = require("path to testData json");
    
                 // to loop on all elements
                 testDataObject.forEach(function(element) {
                    it(' test case  def ', function() {
                         console.log("nameValue "+ element['nameValue']+ "emailValue 
                 "+element['emailValue'] + "passwordValue "+element[passwordValue]); 
                    });
                });
    
              // to select any particular index
             it(' test case  def ', function() {
                     console.log("nameValue "+ testDataObject[1]['nameValue']+ "emailValue 
      "+testDataObject[1]['emailValue'] + "passwordValue "+testDataObject[1][passwordValue]); 
                });
            });
    

    并将您的 testData 文件名命名为 Credentials_Valid.json(最佳实践)

    或 你可以这样做

        {
        "cred1":
        {
           "nameValue": "ant",
            "emailValue": "ant@gmail.com",
            "passwordValue": "ant",
        },
        "cred2":
        {
            "nameValue": "bat",
            "emailValue": "bat@gmail.com",
            "passwordValue": "bat",
        }     
    }
    

    并通过

    访问nodejs代码中的测试数据
    console.log( `${testDataObject['cred1']["nameValue"]}` );
    console.log( `${testDataObject['cred1']["emailValue"]}` );
    console.log( `${testDataObject['cred1']["passwordValue"]}` );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多