I have a C# service running on Windows Server 2012 R2 that executes Newman test suites via the command line. The command the service executes works perfectly when run directly in the command prompt, but is not working from the service. To add insult to injury, the very same command DOES work from the service running locally on my dev machine (Windows 10 Pro). I am certain I am running the same command in all instances, as the service outputs the CLI's StandardOutput into a file, the contents of which I paste straight into the command prompt.
The error I receive:
module.js:471
throw err;
^
Error: Cannot find module 'C:\Users\Adam.Aldridge\AppData\Roaming\npm\node_modules\newman\bin\newman.js'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:393:7)
at startup (bootstrap_node.js:150:9)
at bootstrap_node.js:508:3
The command I'm running (all paths are the same in every instance):
"C:\Program Files\NodeJS\node.exe" C:\Users\MyName\AppData\Roaming\npm\node_modules\newman\bin\newman.js run https://api.getpostman.com/collections/MyCollectionURI -r cli,junit --reporter-junit-export D:\TestHarnessServiceLogs\XML\.xml -e https://api.getpostman.com/environments/MyEnvironmentURI --disable-unicode
C# to build and run command:
//Build over multiple lines to make it vaguely readable, then string replace away the newlines so it runs as one command
string runTestCmd = $@"{_nodeExecutablePath}
{_newmanDotJsFile} run
{collectionPath}
-r cli,junit
--reporter-junit-export {_junitReportPathWithFilename}
-e {environmentPath}
--disable-unicode"
.Replace(Environment.NewLine, " ")
.Replace("\t", "");
File.WriteAllText(@"D:\TestHarnessServiceLogs\Command.txt", runTestCmd);
//Launch hidden CLI
using (Process p = new Process())
{
p.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = _nodeVarsBatPath;
p.Start();
//Execute commands
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine(runTestCmd);
}
}
//Parse the various outputs
output = p.StandardOutput.ReadToEnd();
error = p.StandardError.ReadToEnd();
exitCode = p.ExitCode.ToString(); //Always returns 0, think this is because it evaluates success of creating the process, not what happens inside it
File.WriteAllText(@"D:\TestHarnessServiceLogs\output.txt", output);
File.WriteAllText(@"D:\TestHarnessServiceLogs\error.txt", error);
File.WriteAllText(@"D:\TestHarnessServiceLogs\exitCode.txt", exitCode);
}
Newman is installed globally in both environments, and some relevant AppSettings below (names modified slightly for brevity):
"_newmanDotJsFile": "C:\\Users\\MyName\\AppData\\Roaming\\npm\\node_modules\\newman\\bin\\newman.js",
"_nodeVarsBatPath": "\"C:\\Program Files\\NodeJS\\nodevars.bat\"",
"_nodeExecutablePath": "\"C:\\Program Files\\NodeJS\\node.exe\"",
How can an identical command find the newman module and run fine from the CLI but not from the service?
via Heydiddly
No comments:
Post a Comment