如何在使用 nodejs exec 子进程运行终端命令后恢复夜巡测试



我写了夜巡测试,其中一个测试需要终端命令才能执行。我已经在守夜人的自定义命令部分编写了终端命令。但是当我执行测试时,在执行终端命令后,执行停止。

我想要的是当执行终端命令时,控件应该返回到夜巡,并且应该恢复剩余的测试。可能吗?

这是我编写的代码:

it('Should have Stream URL, Stream Key and Status', function (browser) {
      const sessionsPage = browser.page.sessions()
      const sourcesPanel = sessionsPage.section.sources
      sourcesPanel
        .assert.containsText('@stream_details', 'Stream URL')
        .assert.containsText('@stream_details', 'Stream key')
        .assert.containsText('@stream_details', 'Status')
})
it(' ---- START STREAM ----', function (browser) {
      const sessionsPage = browser.page.sessions()
      sessionsPage.fire_custom_command()
})
it('Some other test', function (browser) {
      // test code
})

上面的代码触发了下面编写的自定义命令:

exports.command = function() {
  var spawn = require('child_process').spawn;
  //kick off process of listing files
  var child = spawn('ls', ['-l', '/']);
  //spit stdout to screen
  child.stdout.on('data', function (data) {   process.stdout.write(data.toString());  });
  //spit stderr to screen
  child.stderr.on('data', function (data) {   process.stdout.write(data.toString());  });
  child.on('close', function (code) { 
    console.log("Finished with code " + code);
  });
}

发生的情况是,在执行自定义命令后,测试将停止,并且永远不会继续下一个测试。我想要的是执行自定义命令并且子进程退出后,应该执行上面写的'some other test'

我也尝试过execSync但它没有按预期工作。

const ls = 'ls';
    const execSync = require('child_process').execSync;
    var cmd = execSync(ls);

抱歉,如果我没有正确解释我的问题,请提前感谢。

我以前使用此方法启动了一个从 Exchange 获取电子邮件的控制台应用程序。不过,我曾经用spawnSync来做到这一点。鉴于您上面的代码片段,我认为您可以通过执行以下操作来使其工作:

function() {        
    const spawnSync = require('child_process').spawnSync;                
    const result = spawnSync('ls', ['-l', '/']);   
    if(result.output[1]) {
        console.log('stdout: ', result.output[1].toString());
    }
    if(result.output[2]) {
        console.log('stderr: ', result.output[2].toString());
    }
    if(result.status !== 0) {
        return false;
    }
    return true;
}

来自节点文档:child_process.spawnSync() 方法通常与 child_process.spawn() 相同,只是在子进程完全关闭之前,函数不会返回。

最新更新