3

I am working on a TestCafe script where I capture an endpoint URL that needs to serve as an input for a shell/bash command.

const inputText = await Selector('textarea[name="url-input"]').value;
console.info(`This is my URL of interest ${inputText}`)

Then I want to use inputText to execute a bash command, say (for simplicity)

echo inputText

How can this be accomplished from my testcafe script? I couldn't find a related post of documentation on this.

I did find a related post on Javascript that uses process.createChildProcess('command');, but I'm still trying to make this solution work. See docs here

// on declarations
const { exec } = require('child_process');

// inside the test
exec('echo "The \\$HOME variable is $HOME"');
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
thenewjames
  • 966
  • 2
  • 14
  • 25

1 Answers1

3

I got it to work with the following, from this 1 and this 2.

// on declarations
const { exec } = require('child_process');

// inside the test
const inputText = await Selector('textarea[name="url-input"]').value;
exec(inputText,
    function (error, stdout, stderr) {
      console.log('stdout: ' + stdout);
      console.log('stderr: ' + stderr);
      if (error !== null) {
        console.error('exec error: ' + error);
      }
    });
thenewjames
  • 966
  • 2
  • 14
  • 25