Actually I want to get user input from user whoever runs the script. I do not want to hardcode the testdata path in the script. for example when I run a script to test angularjs using protractor and javascript. User should be able to give path of the testdata, so that I can use that variable inside the script.
Asked
Active
Viewed 367 times
1 Answers
1
You can do this by passing in a params.testData
value from the command line.
protractor conf.js --params.testData=D:\path\to\testdata.xlsx
Then in your test you will reference it using the global browser.params
object. You will also need to use fs
to read the file and process the data. Honestly, it would probably be easier if you created a .json
file for the test data instead of an .xlsx
but it looks there are libraries out there to help you parse an xlsx document already if you have to stick with that. Check this answer for some examples.
This code will not work as is but the basic idea will be something like this:
before(() => {
const testDataPath = browser.params.testData;
fs.readFile(testDataPath, (err, data) => {
if(err) { // fail? };
const testData = data;
// do some other stuff with test data ...
});
}
You are going to need to do some additional processing of the data from the .xlsx
file to get it in the correct format but this should hopefully help get you on the right path.

tehbeardedone
- 2,833
- 1
- 15
- 23
-
Please, I want this to be implemented in the spec file. how can we do that. – rgurram28 Jan 11 '18 at 14:23
-
I gave an example of how to use it in your spec file. `const testData = browser.params.testData;` Is this not what you mean? I'm not sure what you mean by "path of the test data". – tehbeardedone Jan 11 '18 at 14:39
-
Updated my answer with an example of using it in a spec file. – tehbeardedone Jan 11 '18 at 14:45
-
for example I am having an excel file with username and password in a path D:/testdata/testdata.xlsx. So other user who runs this script should be able to give the path in the console and the spec file should use this variable and run the scripts. – rgurram28 Jan 11 '18 at 15:12
-
Ok, I'll update my answer with an example in a few minutes. – tehbeardedone Jan 11 '18 at 15:23