My objective is to reuse the collections developed in Postman in a program using Newman. This is going to be part part of Unit Testing or Deployment Script in CI/CD Jenkins Pipeline. I decided to use C# and Selenium, but I can change my mind if you believe there is a better option. This means I can develop the Unit Testing component using C#+NodeJS+Edge.js, also develop the deployment script in C# by invoking Postman Collections, and then integrate all such components using Jenkins Pipeline.
I figured out you can use NodeJS to Invoke Postman Collections using Newman Library. I also found out you can use NuGet Edge.js to script NodeJS from C#. I can use Command Line from C# but I was afraid to find difficulties to capture the response for the request invocations from the Newman command line. That us why I was looking for a more reliable way to integrate Newman with C#.
I successfully used vs code to write the sample program to script Node.js from C#. I use the following code block directly from https://github.com/tjanczuk/edge:
using System;
using System.Threading.Tasks;
using EdgeJs;
class Program
{
public static async Task Start()
{
var func = Edge.Func(@"
return function (data, callback) {
callback(null, 'Node.js welcomes ' + data);
}
");
Console.WriteLine(await func(".NET"));
}
static void Main(string[] args)
{
Start().Wait();
}
}
The above is working successfully with .NET Framework 4.5.2 and 4.8.0
Also, I successfully wrote a program in Node.js to invoke a GET request using Newman:
const newman = require('newman'); // require newman in your project
const theCollec = '.\\collections\\vs code test.postman_collection.json';
//call newman.run to pass `options` object and wait for callback
newman.run({
collection: require(theCollec),
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
Now, I am trying to include the Newman code block in the C# program, but it is not working. I am getting the following errors when I run the program using dotnet run
:
using System;
using System.Threading.Tasks;
using EdgeJs;
namespace TestNodeJSNET
{
class Program
{
public static async Task Start()
{
var func = Edge.Func(@"
const newman = require('newman'); // require newman in your project
return function (data, callback) {
const theCollec = '.\\collections\\vs code test.postman_collection.json';
// call newman.run to pass `options` object and wait for callback
newman.run({
collection: require(theCollec),
reporters: 'cli'
}, function (err) {
if (err) { throw err; }
console.log('collection run complete!');
});
callback(null, 'Node.js welcomes ' + data + theCollec);
}
");
Console.WriteLine(await func(".NET"));
}
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Start().Wait();
}
}
}
I am getting the following errors:
Unhandled Exception: System.AggregateException: One or more errors occurred. ---> System.Exception: C:\Projects\TestNodeJSNET\node_modules\ws\lib\websocket.js:460
...options
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Projects\TestNodeJSNET\node_modules\ws\index.js:3:19)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at TestNodeJSNET.Program.<Start>d__0.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at TestNodeJSNET.Program.Main(String[] args)
See the snapshot below of the code that caused the error. Note that the error above is thrown during run-time after I tried using other libraries such as const ws=require("ws")
. I will get the same error if I use const newman = require('newman');
but in a different script file.
It is clear that there Newman script is using the new spread operator ...
which probably Edge.js didnât like. I think one solution is to use a version of Newman module that doesnât use ...
operator or transpile such script using Babel to a lower JavaScript version.
I appreciate your help to resolve this issue.