简体   繁体   English

使用Node.js exec子进程运行终端命令后如何恢复夜视测试

[英]How to resume nightwatch tests after running a terminal command using nodejs exec child process

I have written nightwatch tests and one of the test needs a terminal command to be executed. 我已经编写了睡表测试,并且其中一项测试需要执行终端命令。 I have written the terminal command in the custom command section of nightwatch. 我已经在守夜的自定义命令部分中编写了终端命令。 But when I execute my tests, after executing the terminal command, the execution stops. 但是,当我执行测试时,执行完终端命令后,执行将停止。

What I want is when the terminal command is executed, the control should be returned to nightwatch and it should resume the remaining tests. 我想要的是在执行终端命令时,控件应返回到守夜班,并且应恢复其余测试。 Is it possible? 可能吗?

Here is the code I have written: 这是我编写的代码:

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
})

The above code fires the custom command which is written below: 上面的代码触发下面编写的自定义命令:

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);
  });
}

What happens is after the custom command is executed, the test just halts and it never proceed with next tests. 发生的情况是在执行自定义命令后,测试只会停止,并且永远不会继续进行下一个测试。 What I want is after the custom command is executed and the child process exits, the 'some other test' written above should be executed. 我想要的是在执行自定义命令并且退出子进程之后,应该执行上面编写的'some other test'

I have also tried execSync but it is not working as expected. 我也尝试了execSync但是它没有按预期工作。

const ls = 'ls';

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

Sorry, if I have not explained my problem properly and thanks in advance. 抱歉,如果我没有正确解释我的问题,请提前致谢。

I've used this method to kick off a console app that fetched emails from exchange before. 我已经使用这种方法启动了一个控制台应用程序,该应用程序以前曾从交换中获取电子邮件。 I used spawnSync to do it though. 我使用spawnSync做到了。 Given your snippet above I think you could get it working by doing something like the following: 鉴于上面的代码段,我认为您可以通过执行以下操作来使其工作:

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;
}

From the node docs : The child_process.spawnSync() method is generally identical to child_process.spawn() with the exception that the function will not return until the child process has fully closed. 节点docs来看: child_process.spawnSync()方法通常与child_process.spawn()相同, child_process.spawn()之处在于该函数在子进程完全关闭之前不会返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nodejs:来自 child_process 的 Exec 返回 bin/sh:1:找不到命令但在终端中手动写入时有效? - Nodejs: Exec from child_process returns bin/sh: 1: command not found but works when writing manually in terminal? 如何获取在nodejs中使用child_process执行的命令的输出? - How to get the output of command executed using child_process in nodejs? 如何使用 child_process.exec 在 NodeJS 中运行多行命令 - How to run multiline commands in NodeJS with child_process.exec 使用child_process.exec发送返回404的命令 - Using child_process.exec to send a command returning 404 使用节点child_process.exec执行top命令时,如何解决错误? - How to resolve the error while executing top command using node child_process.exec? 在 aws 节点 10 lambda 中执行子进程后,节点生成子进程不执行命令 - Node spawn child process doesn't execute the command after exec child process in aws node 10 lambda 如何在单独的 cmd 窗口或终端中启动 nodejs 子进程 - How to start a nodejs child process in separate cmd window or terminal 如何在NodeJS子进程中创建终端实例? - How do you create a terminal instance within a NodeJS child process? NodeJS:child_process.exec 不执行函数 - NodeJS: child_process.exec not executing function nodejs child_process exec'java -version' - nodejs child_process exec 'java -version'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM