protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Passing a function as argument to addMockModule

Open magicode118 opened this issue 11 years ago • 3 comments

I am having issues passing a function as an argument to addMockModule. If anyone has any idea of why this is happening, please reply to this StackOverflow question: http://stackoverflow.com/questions/33285982/passing-a-function-as-argument-to-addmockmodule

magicode118 avatar Oct 22 '15 16:10 magicode118

Okay so apparently the issue is that addMockModule is calling executeScript to deal with the arguments in order to bridge the Angular/Protractor context. exectuteScript is serializing this data to string. Any possibility that this is fixed or a workaround is found? Thanks.

magicode118 avatar Oct 23 '15 09:10 magicode118

Just as an update, for now I am deserializing it manually, like this:

var startBody = x.indexOf('{') + 1; var endBody = x.lastIndexOf('}'); var startArgs = x.indexOf('(') + 1; var endArgs = x.indexOf(')');

var myFun = new Function(x.substring(startArgs, endArgs), x.substring(startBody, endBody));

It works, at least on simple functions :)

magicode118 avatar Oct 23 '15 09:10 magicode118

You can pass additional arguments to the script, as noted in the varArgs section here: http://angular.github.io/protractor/#/api?view=Protractor.prototype.addMockModule

We should improve the documentation noting that this will be executed in the browser context.

juliemr avatar Nov 18 '15 03:11 juliemr