Open earloc opened 3 years ago
Not sure, if this is actually possible in a practical way, though.
This may be worth a look at: https://github.com/sebastienros/jint
Should be doable to hookup some clr-proxies which mimic Postman's pm
- object e. g. for reading and writing variables and globals within scripts.
Not sure about an approach how to bridge test-execution results back into the clr-world. Could be done via proxies, too - but smells way more complex.
Just added a working prototype using above mentioned lib. Works like a charm.
Down the rabbit-hole:
With the current approach, by providing clr-proxies to tha JavaScript-Engine for the execution of e.g. postman´s-prerequest-scripts, everything possibly available on postman´s pm
-object would need to be provided on clr-side somehow.
this would include e.g. a more or less complete proxy/binding for https://www.chaijs.com/api/assert/
The following prerequest-script in postman:
pm.variables.set("firstName", "Arthur")
pm.variables.set("surname", "Dent")
pm.variables.set("unused", "towel")
pm.test("test", () => {
log("helloworld");
});
can be successfuly interpreted by a demo-testcase, utilizing some proxies:
var localVariables = new MutableVariableContext(new { foo = "bar" });
var folder = sut.FindFolder(folderName);
var request = folder.FindRaw(requestName, localVariables);
request.Events.PreRequestScript.Should().NotBeNullOrEmpty();
var pm = new PostmanEmulator(localVariables);
var engine = new Engine()
.SetValue("pm", pm)
.SetValue("log", new Action<string>(output.WriteLine));
engine.Execute(string.Join(Environment.NewLine, request.Events.PreRequestScript));
localVariables.Variables.Should().BeEquivalentTo(new Dictionary<string, string>()
{
{"foo", "bar"},
{"firstName", "Arthur"},
{"surname", "Dent"},
{"unused", "towel"}
}, "those values should be merged together after executing the scripts" );
}
class PostmanEmulator {
public PostmanEmulator(MutableVariableContext variables)
{
this.Variables = variables;
}
public void Test(string name, JsValue javaScript)
{
javaScript.Invoke();
}
public MutableVariableContext Variables { get; }
}
Would be cool, if we somehow could leverage existing assertions made within postman after firing requests. This way, the need for custom parsing and asserting on responses could be relaxed in a way, that drastically reduces boilerplate in the test-code