简体   繁体   English

如何在ui自动化中读写文件(Javascript)?

[英]How to read and write to a file (Javascript) in ui automation?

I want to identify few properties during my run and form a json object which I would like to write to a ".json"file and save it on the disk. 我想在运行期间识别一些属性,并形成一个json对象,我想将其写入“ .json”文件并将其保存在磁盘上。

var target = UIATarget.localTarget();
var properties = new Object();
var jsonObjectToRecord = {"properties":properties}
jsonObjectToRecord.properties.name = "My App"
UIALogger.logMessage("Pretty Print TEST Log"+jsonObjectToRecord.properties.name);
var str = JSON.stringify(jsonObjectToRecord)
UIALogger.logMessage(str);
// -- CODE TO WRITE THIS JSON TO A FILE AND SAVE ON THE DISK --

I tried : 我试过了 :

// Sample code to see if it is possible to write data
// onto some file from my automation script
function WriteToFile()
 {

    set fso = CreateObject("Scripting.FileSystemObject");  
    set s = fso.CreateTextFile("/Volumes/DEV/test.txt", True);
    s.writeline("HI");
    s.writeline("Bye");
    s.writeline("-----------------------------");
    s.Close();
 }

AND

function WriteFile()
{
    // Create an instance of StreamWriter to write text to a file.
    sw = new StreamWriter("TestFile.txt");
    // Add some text to the file.
    sw.Write("This is the ");
    sw.WriteLine("header for the file.");
    sw.WriteLine("-------------------");
    // Arbitrary objects can also be written to the file.
    sw.Write("The date is: ");
    sw.WriteLine(DateTime.Now);
    sw.Close();
}

But still unable to read and write data to file from ui automation instruments 但是仍然无法从ui自动化仪器读取数据并将数据写入文件

Possible Workaround ?? 可能的解决方法? To redirect to the stdout if we can execute a terminal command from my ui automation script. 如果可以从ui自动化脚本中执行终端命令,则重定向到标准输出。 So can we execute a terminal command from the script ? 那么我们可以从脚本执行终端命令吗?

Haven't Tried : 1. Assuming we can include the library that have those methods and give it a try . 尚未尝试:1.假设我们可以包含具有这些方法的库并进行尝试。

Your assumptions are good, But the XCode UI Automation script is not a full JavaScript. 您的假设很好,但是XCode UI Automation脚本不是完整的JavaScript。

I don't think you can simply program a normal browser based JavaScript in the XCode UI Automation script. 我认为您不能在XCode UI Automation脚本中简单地编写基于普通浏览器的JavaScript。

set fso = CreateObject("Scripting.FileSystemObject");

Is not a JavaScript, it is VBScript which will only work in Microsoft Platforms and testing tools like QTP. 它不是JavaScript,而是VBScript,只能在Microsoft平台和QTP等测试工具中使用。

Scripting.FileSystemObject

Is an ActiveX object which only exists in Microsoft Windows 是仅在Microsoft Windows中存在的ActiveX对象

Only few JavaScript functions like basic Math, Array,...etc..Are provided by the Apple JavaScript library, so you are limited to use only the classes provided here https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/ Apple JavaScript库仅提供了很少的JavaScript函数,如基本Math,Array等。因此,您只能使用此处提供的类https://developer.apple.com/library/ios/documentation / DeveloperTools / Reference / UIAutomationRef /

If you want to do more scripting then Try Selenium IOS Driver http://ios-driver.github.io/ios-driver/ 如果要执行更多脚本编写,请尝试Selenium IOS驱动程序http://ios-driver.github.io/ios-driver/

Hey so this is something that I was looking into for a project but never fully got around to implementing so this answer will be more of a guide of what to do than step by step copy and paste. 嘿,这是我一直在寻找一个项目的东西,但从未完全实施它,因此,此答案将比分步复制和粘贴更多地是操作指导。

First you're going to need to create a bash script that writes to a file. 首先,您将需要创建一个写入文件的bash脚本。 This can be as simple as 这可以很简单

!/bin/bash
echo $1 >> ${filename.json}

Then you call this from inside your Xcode Instruments UIAutomation tool with 然后从Xcode Instruments UIAutomation工具内部调用

var target = UIATarget.localTarget();
var host = target.host();

var result = host.performTaskWithPathArgumentsTimeout("your/script/path", ["Object description in JSON format"], 5);

Then after your automation ends you can load up the file path on your computer to look at the results. 然后,在自动化结束后,您可以加载计算机上的文件路径以查看结果。

EDIT: This will enable to write to a file line by line but the actual JSON formatting will be up to you. 编辑:这将允许逐行写入文件,但是实际的JSON格式由您决定。 Looking at some examples I don't think it would be difficult to implement but obviously you'll need to give it some thought at first. 查看一些示例,我认为实现起来并不困难,但是显然您首先需要考虑一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM