javascript cucumber.js的步骤实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript cucumber.js的步骤实现相关的知识,希望对你有一定的参考价值。

const defineSupportCode = require("cucumber").defineSupportCode;

let exec = require("child_process").exec;
let fs   = require("fs");

let openshift_password = process.env.OPENSHIFT_PASSWORD || "developer";
let openshift_url      = process.env.OPENSHIFT_URL      || "https://127.0.0.1:8443";
let build_id           = process.env.BUILD_NUMBER       || "1";
let build_name         = process.env.JOB_NAME           || "cucumber";

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

defineSupportCode(function({Before, After, Given, When, Then}){
  let memento  = {};
  let workdir  = "/tmp/" + build_name + "-" + build_id;
  let exec_opt = {};

  Before(function(){
    if(!fs.existsSync(workdir)){
      fs.mkdirSync(workdir);
    }

    exec_opt["env"]      = JSON.parse(JSON.stringify(process.env));
    exec_opt.env["HOME"] = workdir;
  });

  Given('I am logged in Openshift as {string}', function (username, callback) {
    /* Native call to perform login into Openshift API through the Openshift Client Tools */
    exec("oc login --insecure-skip-tls-verify -u '" + username + "' -p '" + openshift_password + "' '" + openshift_url + "' ", exec_opt, function(error, stdout, stderr){
      if(error){
        callback(error);
      } else {
        callback();
      }
    });
  });

  Given('access to {string} namespace', function (namespace, callback) {
    /* Test access to namespace */
    exec("oc project " + namespace, exec_opt, function(error, stdout, stderr){
      if(error){
        callback(error);
      } else {
	callback();
      }
    });
  });

  When('query the pods of {string} replication controller', function (rc, callback) {
    /* Get all pods managed by the replication controller */
    exec("oc get pods -l app=" + rc + " -o json", exec_opt, function(error, stdout, stderr){
      if(error){
        callback(error);
      } else {
        memento[rc] = JSON.parse(stdout);
        callback();
      }
    });
  });

  Then('all pods managed by {string} replication controller should have the status equals to {string}', function (rc, podstatus, callback) {
    let pods = memento[rc];
    let errors = [];
    /* Check the status of all pods */
    pods.items.forEach(function(pod){
      if(pod.status.phase.toLowerCase() !== podstatus.toLowerCase()){
        errors.push({
	  pod   : pod.metadata.name,
          status: pod.status.phase
        });
      }
    });

    if(errors.length !== 0){
      callback(new Error(JSON.stringify(errors)));
    } else {
      callback();
    }
  });
});

以上是关于javascript cucumber.js的步骤实现的主要内容,如果未能解决你的问题,请参考以下文章

CucumberJS 资源

使用 ZombieJS 的 Cucumber.js 场景中的回调问题

Node.js BDD。黄瓜,小黄瓜还是黄瓜? [关闭]

无法在 Windows 7 64 位上安装 WebdriverIO Cucumber JS 框架

如果我必须进行REST调用并将响应值与UI进行比较,那么步骤定义将如何构建?

带有量角器的黄瓜 HTML 报告