iOS远程真机之非越狱iOS设备的远程控制实现原理

10 篇文章 70 订阅 ¥9.90 ¥99.00

       通过iOS设备控制PC可能较为常见,App Store也有不少类似的应用,但是通过PC控制iOS相信大家很难在网上找到解决方案,能找到的也大部分是需要依赖越狱来实现。

       安卓提供了强大的adb工具,能轻松实现类似的功能。但iOS由于系统的封闭性,大部分功能非越狱无法逾越系统的权限。

       今天给大家带来的是基于苹果官方提供的UI测试框架实现的非越狱机器远程控制实现原理。

XCUITest

       XCUITest是Aplle自Xcode7开始引入的自动化测试框架,而且在Xcode8中,原先的UIAutomation框架废弃无法再用。

       相比较UIAutomation,XCUITest使用简便程度有了很大提高。

这里写图片描述

       选择UITestCase,会生成一个Case文件如下:

#import <XCTest/XCTest.h>
@interface testUI : XCTestCase
@end
@implementation testUI
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
// In UI tests it is usually best to stop immediately when a failure occurs.
self.continueAfterFailure = NO;
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
[[[XCUIApplication alloc] init] launch];
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// Use recording to get started writing UI tests.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
@end

       根据提示在testExample部分修改添加代码。

       此时可以使用xcode自带的录制功能,点击鼠标使得光标处于testExample的方法下,再点击红色的录制按钮。
这里写图片描述

       项目会自动编译部署到指定的设备上(真机或模拟器均可),并自动启动。此时操作app的同时,Xcode会记录操作并自动转化成代码。

这里写图片描述

       也可以手动根据自己需求调整或新增操作代码。

       完毕后点击测试即可直接执行自动化测试。

这里写图片描述

设备控制

       第一次执行测试会在设备端桌面生成一个类似xcuitesdemoUITests的APP,该APP无法执行执行,只能通过xcode的test启动。

       启动时会有个瞬间的黑屏,然后app进入后台,同时启动待测APP,而后依次开始各项测试任务。

       这里启动的哪个APP是由UITest项目的配置决定的,也就是说如果工程拥有多个APP,可以选择启动不同的APP。

这里写图片描述

       当然也可以选择None,但启动测试的时候会出现以下的问题。

这里写图片描述

       默认的XCUIApplication头文件中,只有如下2个方法:

@interface XCUIApplication : XCUIElement
/*!
* Launches the application. This call is synchronous and when it returns the application is launched
* and ready to handle user events. Any failure in the launch sequence is reported as a test failure
* and halts the test at this point. If the application is already running, this call will first
* terminate the existing instance to ensure clean state of the launched instance.
*/
- (void)launch;
/*!
* Terminates any running instance of the application. If the application has an existing debug session
* via Xcode, the termination is implemented as a halt via that debug connection. Otherwise, a SIGKILL
* is sent to the process.
*/
- (void)terminate;

       但是可以从私有API的Header里能挖掘出比较多的方法。

+ (instancetype)appWithPID:(pid_t)processID;
- (void)dismissKeyboard;
- (BOOL)setFauxCollectionViewCellsEnabled:(BOOL)arg1 error:(id *)arg2;
- (void)_waitForViewControllerViewDidDisappearWithTimeout:(double)arg1;
- (void)_waitForQuiescence;
- (void)terminate;
- (void)_launchUsingXcode:(BOOL)arg1;
- (void)launch;
- (id)application;
- (id)deion;
- (id)lastSnapshot;
- (id)query;
- (void)clearQuery;
- (void)resolveHandleUIInterruption:(BOOL)arg1;
- (id)initPrivateWithPath:(id)arg1 bundleID:(id)arg2;
- (id)init;

       通过InitPrivateWithPath方法可以以下述方法启动任意其他已安装的APP,并对其他APP进行操作。

       比如下述代码可以启动safari,并打开http://mtc.baidu.com/

NSString *appBundleID = @"com.apple.mobilesafari";
XCUIApplication* app = [[XCUIApplication alloc] initPrivateWithPath:nil bundleID:appBundleID];
[app launch];
[app.otherElements[@"URL"] tap];
[app typeText:@"mtc.baidu.comn"];

WebDriverAgent

       WebDriverAgent(https://github.com/facebook/WebDriverAgent)是Facebook基于XCUITest推出的iOS的移动测试框架,支持目前市面上所有iOS9以上的设备。

       该框架通过在设备端启动一个HTTP Server提供一系列API接受操作指令来代替固定的操作代码,除了启动应用、点击和滑动页面元素,WebDriverAgent还提供截图、页面元素查询等功能,iOS的appium测试框架就是基于WebDriverAgent实现的。

这里写图片描述

WebDriverAgent 安装使用完全指南

远程控制iOS设备

       基于上述技术,我们把功能具体化成一个Web服务。

       通过轮询的方式获取当前屏幕内容,并将屏幕上的鼠标点击以及滑动操作转化成具体的操作指令,最终即可达到通过Web页面控制手机设备的效果。

这里写图片描述

转自:http://www.sohu.com/a/147345910_744135

  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值