esi*_*ver 7 javascript iphone integration-testing ios ios-ui-automation
我正在尝试使用Apple的UIAutomation为具有服务器端组件的iOS应用程序编写单元测试.为了在各种状态下设置测试服务器(以及模拟通过我的服务器进行通信的两个客户端),我想在基于javascript的测试中发出HTTP get请求.
任何人都可以提供一个示例,说明如何直接从UIAutomation javascript测试中发出HTTP GET请求,或者如何从我的UIAutomation javascript测试中调用shell脚本?
FWIW,UIAutomation运行时中缺少所有浏览器提供的大多数核心对象.例如,尝试使用XMLHTTPRequest,您将收到一个异常报告,它无法找到该变量.
谢谢!
伙计们,
我能够通过向iOS客户端发送HTTP请求来处理并在UIAlertView中返回结果来解决这个问题.请注意,所有iOS代码修改都包含在#if DEBUG条件编译指令中.
首先,设置您的客户端,以便在设备震动时发送通知.阅读这篇文章了解更多信息.
接下来,在您的iOS主应用程序委托中添加以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(deviceShakenShowDebug:)
name:@"DeviceShaken"
object:nil];
Run Code Online (Sandbox Code Playgroud)
然后添加一个看起来像这样的方法:
- (void) deviceShakenShowDebug:(id)sender
{
if (!self.textFieldEnterDebugArgs)
{
self.textFieldEnterDebugArgs = [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 260.0, 25.0)] autorelease];
self.textFieldEnterDebugArgs.accessibilityLabel = @"AlertDebugArgsField";
self.textFieldEnterDebugArgs.isAccessibilityElement = YES;
[self.textFieldEnterDebugArgs setBackgroundColor:[UIColor whiteColor]];
[self.tabBarController.selectedViewController.view addSubview:self.textFieldEnterDebugArgs];
[self.tabBarController.selectedViewController.view bringSubviewToFront:self.textFieldEnterDebugArgs];
}
else
{
if ([self.textFieldEnterDebugArgs.text length] > 0)
{
if ([self.textFieldEnterDebugArgs.text hasPrefix:@"http://"])
{
[self doDebugHttpRequest:self.textFieldEnterDebugArgs.text];
}
}
}
}
- (void)requestDidFinishLoad:(TTURLRequest*)request
{
NSString *response = [[[NSString alloc] initWithData:((TTURLDataResponse*)request.response).data
encoding:NSUTF8StringEncoding] autorelease];
UIAlertView *resultAlert =
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Request Loaded",@"")
message:response
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",@"")
otherButtonTitles:nil] autorelease];
resultAlert.accessibilityLabel = @"AlertDebugResult";
[resultAlert show];
}
Run Code Online (Sandbox Code Playgroud)
此代码将在摇动后向顶部视图控制器添加UITextField,在任何导航栏或其他UI元素上方拍打.UIAutomation或您的用户可以手动在此UITextField中输入URL.当您再次摇动设备时,如果文本以"http"开头,它将在代码中发出HTTP请求(练习让读者实现doDebugHttpRequest).
然后,在我的UIAutomation JavaScript文件中,我定义了以下两个函数:
function httpGet(url, delayInSec) {
if (!delayInSec) delay = 1;
var alertDebugResultSeen = false;
var httpResponseValue = null;
UIATarget.onAlert = function onAlert(alert) {
httpResponseValue = alert.staticTexts().toArray()[1].name();
alert.buttons()[0].tap();
alertDebugResultSeen = true;
}
var target = UIATarget.localTarget();
var application = target.frontMostApp();
target.shake(); // bring up the input field
application.mainWindow().textFields()["AlertDebugArgsField"].setValue(url);
target.shake(); // send back to be processed
target.delay(delayInSec);
assertTrue(alertDebugResultSeen);
return httpResponseValue;
}
function httpGetJSON(url, delayInSec) {
var response = httpGet(url, delayInSec);
return eval('(' + response + ')');
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的javascript文件中,我可以打电话
httpGet('http://localhost:3000/do_something')
Run Code Online (Sandbox Code Playgroud)
它将执行HTTP请求.如果我想从服务器返回JSON数据,我打电话
var jsonResponse = httpGetJSON('http://localhost:3000/do_something')
Run Code Online (Sandbox Code Playgroud)
如果我知道这将是一个长时间通话,我打电话
var jsonResponse = httpGetJSON('http://localhost:3000/do_something', 10 /* timeout */)
Run Code Online (Sandbox Code Playgroud)
我已成功使用这种方法几周了.
尝试performTaskWithPathArgumentsTimeout
UIATarget.host().performTaskWithPathArgumentsTimeout("/usr/bin/curl", "http://google.com", 30);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2313 次 |
最近记录: |