3

Is there a possible way to read a local file in JavaScript.

MyFolder:

     db.csv

     Parse.js

Trying to fetch the contents of file db.csv in Parse.js, But in vain.

Can you share some links where I can get enough knowledge how to read a file. Running Instruments in Xcode5, with test scripts in .js file where I have to feed in some values from a .csv file.

tshepang
  • 12,111
  • 21
  • 91
  • 136
coder284
  • 831
  • 1
  • 13
  • 34
  • Could you share what exactly you tried, and what failed. **Quick Answer:** If you are doing JavaScript inside a browser you cannot read files from **the local computer**. The browser doesn't allow this. – gideon Nov 15 '13 at 06:44
  • 2
    http://stackoverflow.com/questions/7431268/how-read-data-from-csv-file-using-javascript this may help you out – Shiva Saurabh Nov 15 '13 at 06:44
  • @gideon and Maverick, this is a question about Apple's UIAutomation framework for iOS testing, which (although written in Javascript) provides access to the host shell. – Ian Apr 01 '15 at 21:10

3 Answers3

3

iOS UIAutomation, apple provides an api for running a task on the target's host.

performTaskWithPathArgumentsTimeout

Using this, we can have a bash script to printout the contents of a file that we wanted to fetch in the first case.

Bash script can be as simple as this for this requirement.

 #! /bin/bash
FILE_NAME="$1"
cat $FILE_NAME

Save it as for example FileReader.sh file.

And in your automation script,

    var target = UIATarget.localTarget();
    var host = target.host();
    var result = host.performTaskWithPathArgumentsTimeout(executablePath,[filePath,fileName], 15);
UIALogger.logDebug("exitCode: " + result.exitCode);
    UIALogger.logDebug("stdout: " + result.stdout);
    UIALogger.logDebug("stderr: " + result.stderr);

where in, executablePath is where the command need to be executed.

var executablePath = "/bin/sh";

filePath is the location of the created FileReader.sh file. When executed, outputs the content to standard output (in our requirement). [give full absolute path of the file]

fileName is the actual file to fetch contents from. [give full absolute path of the file] In my case I had a Contents.csv file, which I had to read.

and the last parameter is the timeout in seconds.

Hope this helps others, trying to fetch contents (reading files) for performing iOS UIAutomation.

References:

https://stackoverflow.com/a/19016573/344798

https://developer.apple.com/library/iOS/documentation/UIAutomation/Reference/UIAHostClassReference/UIAHost/UIAHost.html

Community
  • 1
  • 1
coder284
  • 831
  • 1
  • 13
  • 34
1

If the file is on the same domain as the site you're in, you'd load it with Ajax. If you're using Ajax, it's be something like

$.get('db.csv', function(csvContent){
    //process here
});

Just note that the path to the csv file will be relative to the web page you're in, not the JavaScript file.

If you're not using jQuery, you'd have to manually work with an XmlHttpRequest object to do your Ajax call.

And though your question doesn't (seem to) deal with it, if the file is located on a different domain, then you'd have to use either jsonP or CORS.

And, just in case this is your goal, no, you can't, in client side JavaScript open up some sort of Stream and read in a file. That would be a monstrous security vulnerability.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • @Adam, Thanks for the quick help. Right now am not using Ajax, please let me know if at all it is possible using javascript, if you know of. Any inputs regarding the same are welcome. – coder284 Nov 15 '13 at 06:46
  • @coder284 your question is not very clear. Are you running javascript on a server, on a browser, are you looking to read a local file or a file returned from a server, which is it? – gideon Nov 15 '13 at 06:47
  • @coder284 - absolutely not. You can't open up some sort of StreamReader and read in a file in client-side JavaScript – Adam Rackis Nov 15 '13 at 06:47
  • 1
    There is no ajax here, this is the [UIAutomation framework](https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/). – Ian Apr 01 '15 at 21:11
0

This is a fairly simple function in Illuminator's host functions library:

function readFromFile(path) {
    var result = target.host().performTaskWithPathArgumentsTimeout("/bin/cat", [path], 10);

    // be verbose if something didn't go well
    if (0 != result.exitCode) {
        throw new Error("readFromFile failed: " + result.stderr);
    }
    return result.stdout;
}

If you are using Illuminator, this is host().readFromFile(path).

Ian
  • 11,280
  • 3
  • 36
  • 58