简体   繁体   中英

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.

I have also tried execSync but it is not working as expected.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM