2

I'm having the strangest issue and hope someone else has seen this and maybe even has a fix.

Previously, I had in my grunt.js file a set of parameters to use in my protractor tests. Simple example below:

params: {
  userId: "abc",
  someData: ["abc"]
}

And this worked no problem. Then as my data grew I felt externalizing it to another file would be better, so I did and loaded the file with grunt.file.readJSON.

params: grunt.file.readJSON('my/external/file.json');

And the contents of the file:

{
   "userId": "abc",
   "someData": ["abc"]
}

Checking against params.userId works perfectly. However if I check the length of params.someData I get a length of 3 instead of 1. The array is changed. However, if I make one more change to someData as such:

{
   "userId": "abc",
   "someData": ["abc", "def"]
}

Then the array shows two elements as expected. Any ideas? How can I preserve the single element array with a string?

jonbcampos
  • 191
  • 10
  • It is ok when I test it. `grunt.file.readJSON` just does `JSON.parse` on a content of the file, so there should not be something related to a file parsing. Does `grunt.file.readJSON('my/external/file.json')` return you bad data or it is being corrupted later? – Michael Radionov Sep 24 '15 at 22:35
  • Also, do you use `grunt-protractor-runner`? – Michael Radionov Sep 24 '15 at 22:38
  • yes I am using grunt-protractor-runner. It is showing corrupted when I am running the protractor test. If I add a `console.log()` on the loaded data I see the issue. What is strange is there was another area I was having an issue with pulling the contents of an object: `{ label: "a", description: "abc"}` In protractor when I `console.log`ed out the code I would get an array that would always be equal to `[object Object]`. Obviously I see what is happening I just don't know why and how to fix it. – jonbcampos Sep 25 '15 at 14:56
  • I wonder if everything is ok before it gets to Protractor, could you just `console.log(grunt.file.readJSON('my/external/file.json'))` in Gruntfile? It should print a result to a terminal. I'm curious about it, because you said that it worked before you've moved the options to another file. – Michael Radionov Sep 25 '15 at 15:05
  • Great hint. I just reran with the following code and it is correct. That means the issue in somewhere in protractor/grunt-protractor-runner `grunt.registerTask('testData', function(arg){ var data = grunt.file.readJSON("sources/protractor/local/settings.json"); grunt.log.write(data.testData.length); });` returns 1 < correct – jonbcampos Sep 25 '15 at 15:18
  • Any hint how to find out if/where it is in protractor? – jonbcampos Sep 25 '15 at 15:19
  • I guess the problem is somewhere in `grunt-protractor-runner`, because it [converts params object to a CLI arguments](https://github.com/teerapap/grunt-protractor-runner/blob/v2.1.0/tasks/protractor_runner.js#L81) for Protractor and then Protractor reads them like from a terminal. All these conversions might cause the issue. – Michael Radionov Sep 25 '15 at 15:21
  • boo. Great find, but boo. Any hints as to a better grunt/protractor runner? I'll file this as an issue with grunt-protractor-runner but it would be great to determine a solution to. – jonbcampos Sep 25 '15 at 15:25

1 Answers1

1

As you can see from the comments, thanks to Michael Radionov, this is based on grunt-protractor-runner changing the params into command line variables. That said everything becomes a string. This is, tangentially, was also touched on in this stackoverflow q&a:

How can I use command line arguments in Angularjs Protractor?

And within this grunt-protractor-runner issue"

https://github.com/teerapap/grunt-protractor-runner/issues/130

The quick fix, which you're going to hate - I know I do, is to change your json and then parse the json in your protractor file as such.

This:

someObj.data = ["abc"]

Becomes:

someObj.data = "[\"abc\"]"

Now in your protractor file you just extract your data:

var myArray = JSON.parse(someObj.data);

And you'll have the expected data.

Community
  • 1
  • 1
jonbcampos
  • 191
  • 10