【问题标题】:How can a Postman environment variable value be passed into an nodejs script executing Newman?如何将 Postman 环境变量值传递到执行 Newman 的 nodejs 脚本中?
【发布时间】:2020-06-08 10:03:18
【问题描述】:

示例:在命令行上执行 newman 时,用户会这样做:

newman run CollectionName.json --env-var baseurl="myhost/url" --env-var user="admin" --env-var password="admin123"

在使用这样的脚本时,如何将诸如 --env-var user="admin" 之类的参数传递给 newman?

// myNodeScript.js
const newman = require('newman');
const fs = require('fs');
xml2js = require('xml2js');
newman.run({
  collection: require("./PostmanCollections/CollectionName.json"),
  folder: "Sanity",
  globals: require("./postmanVariables/QAWorkspace.postman_globals.json"), 
  environment: require("./postmanVariables/A1Env.postman_environment.json"),
  exportEnvironment: "myEnv.json",
  exportGlobals: "myGlobals.json",
  reporters: "cli",
}).on('start', function (error, summary) { // ...

【问题讨论】:

    标签: node.js postman newman


    【解决方案1】:

    @the_ccalderon 的上一个答案是正确的,但为了使这个答案更完整,对于像我这样偶然发现这里的人,你会做这样的事情:

    // myNodeScript.js
    const newman = require('newman');
    
    environmentVariables = [];
    environmentVariables.push(
      {
        enabled: true,
        key: "foo",
        value: "bar",
        type: 'any'
      }
    );
    newman.run({
      collection: require("./PostmanCollections/CollectionName.json"),
      // ...
      envVar: environmentVariables,
      // ...
    });
    

    我正在围绕 Newman 编写一个包装脚本,它在运行我的 Newman 之前解析我自己的一些 CLI 参数(使用 yargs 这样做)并且可以验证它是否有效。

    【讨论】:

      【解决方案2】:

      在您的postman_environment.json 中,您可以指定以下内容:

      {
        "id": "a14232bb-48d3-3494-7e6f-f4f34e6331a4",
        "name": "testEnv",
        "values": [
          {
            "enabled": true,
            "key": "baseurl",
            "value": "myhost/url",
            "type": "text"
          },
          {
            "enabled": true,
            "key": "user",
            "value": "admin",
            "type": "text"
          }
        ],
        "timestamp": 1504039485918,
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2017-08-29T20:44:53.396Z",
        "_postman_exported_using": "Postman/5.1.3"
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-30
        • 2022-01-28
        • 2019-01-23
        • 2021-12-02
        • 1970-01-01
        • 2016-09-24
        • 1970-01-01
        • 2015-06-08
        • 1970-01-01
        相关资源
        最近更新 更多