[英]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.