当前位置: 首页 > 工具软件 > CodeceptJS > 使用案例 >

codeceptjs 如何添加custom helper

轩辕源
2023-12-01

codeceptjs 如何添加custom helper

  1. create a custom_steps.js
 'use strict';
const fs = require('fs');
let Helper = codecept_helper;

class Custom extends Helper {
    countRecords(entity) {
        var contents = fs.readFileSync(".\\output\\" + entity + ".json", "utf-8");
        console.log(contents);
        contents = JSON.parse(contents);
        var idCount = 0;
        for (var i in contents) {
            var Id = contents[i]["Product"]["Id"];
            console.log("Id" + Id);
            idCount += 1;
        }
        return idCount;
    }
}


module.exports = Custom
  1. 在codeceptjs helpers: 里面添加extends helper
  helpers: {
        WebDriver: {
            smartWait: 5000,
            url: 'http://localhost:8180/',
            browser: 'chrome',
            windowSize: "maximize",
            desiredCapabilities: {
                chromeOptions: {
                    "prefs": {
                        "profile.default_content_settings.popups": "0",
                        "download.default_directory": "D:\\automation\\authoring\\workdir\\data-authoring-e2e\\output"
                    }
                }
            }
        },
        Custom: {
            require: "./step_definitions/custom_steps.js"
        },
  1. 直接在scenarios 里面引用
 let idCount = await I.countRecords(entity);
    var assert = require('assert');
    assert.equal(idCount, count);
 类似资料: