无法在运行时更新量角器配置

时间:2018-04-20 14:12:21

标签: selenium-webdriver jasmine protractor

我正在尝试在运行时更新量角器配置,因为正在从外部Excel文件中读取spec文件。

以下是我的配置:

export let config = {
  allScriptsTimeout: RunConfig.allScriptsTimeout,
  capabilities: {
    browserName: RunConfig.browser
  },
  directConnect: true,
  baseUrl: RunConfig.baseUrl,
  framework: RunConfig.framework,
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: RunConfig.defaultTimeoutInterval,
    print: function () {
    }
  },
  onPrepare() {
    bot.fullScreen();
    Xlsx.readExcel();
    jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
  },
  beforeLaunch() {
    let spec =  new Promise((resolve) => {
      resolve({
        specs: Xlsx.readSpecs()
      });
    });
  },
  resultJsonOutputFile: RunConfig.resultFile
}

根据this,它应该在beforeLaunch中更新配置

==========================

ReadSpec功能

readSpecs() {
   fs.readFile(RunConfig.runManager, (err, buf) => {
      if(!err && buf) {
        let wb = XLSX.read(buf, { type: 'buffer' });
        let sheet = wb.Sheets[sheetNames.specs]; //reading specs
        let specArr = XLSX.utils.sheet_to_json(sheet);
        let spec;
        specArr.forEach(element => {
          spec.push(element.spec);
        });
        return spec;
      } else {
        return [];
      }

    });
  }

2 个答案:

答案 0 :(得分:1)

我在内联添加说明:

XLSX = require('xlsx');

// change readSpecs() to sync style
readSpecs() {
    let wb = XLSX.readFile(RunConfig.runManager);
    let sheet = wb.Sheets[sheetNames.specs]; //reading specs
    let specArr = XLSX.utils.sheet_to_json(sheet);
    let spec;
    specArr.forEach(element => {
        spec.push(element.spec);
    });
    return spec;
}


export let config = {

    // specify value for `specs`, and it require `Xlsx.readSpecs()` 
    // does not return promise-like object.
    specs: Xlsx.readSpecs(),

    onPrepare() {
        bot.fullScreen();
        // you need to move Xlsx.readExcel(); to the top
        // when protractor run into onPrepare() function,
        // it means protractor runner had accepted your passed-in config
        // the runner won't accept any changes to the config once it accepted.
        // Xlsx.readExcel(); 

        jasmine.getEnv().addReporter(new SpecReporter({
            spec: {
                displayStacktrace: true
            }
        }));
    },

    // beforeLaunch() is unnecessary    
    // beforeLaunch() {
    //     let spec = new Promise((resolve) => {
    //         resolve({
    //             specs: Xlsx.readSpecs()
    //         });
    //     });
    // },
};

beforeLaunch()无法更改配置。没有钩子可以在量角器读取之前更改配置。唯一的方法是为specs提供预先知道的值,而不允许类似承诺的值。

如果您的Xlsx.readExcel()Xlsx.readSpecs()返回承诺,则必须使用其他方式来实现它。

enter image description here

答案 1 :(得分:0)

您没有使用本地变量spec来处理beforeLaunch函数中的任何内容。您发布的链接中的示例是返回已解决的承诺。你需要摆脱局部变量并返回这样的承诺:

beforeLaunch() {
  return new Promise((resolve) => {
    resolve({
      specs: Xlsx.readSpecs()
    });
  });
}