Search code examples
javascriptfile-ioui-automationios-ui-automation

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


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.


Solution

  • 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