How to fetch file content (basically read) a local file in javascript for UIAutomation iOS

て烟熏妆下的殇ゞ 提交于 2019-12-07 17:58:47

问题


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.


回答1:


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




回答2:


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.




回答3:


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).



来源:https://stackoverflow.com/questions/19995044/how-to-fetch-file-content-basically-read-a-local-file-in-javascript-for-uiauto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!