• 【iOS免越狱】利用IOS自动化web-driver-agent_appium-实现自动点击+滑动屏幕


    1.目标
    • 在做饭、锻炼等无法腾出双手的场景中,想刷刷抖音

    • 刷抖音的时候有太多的广告

    如何解决痛点

    • 抖音自动播放下一个视频
    •  iOS系统高版本无法 越狱 安装插件
    2.操作环境
    MAC一台,安装 Xcode
    iPhone一台,16 系统以上最佳

     3.流程

    下载最新 web-driver-agent_appium

    xcode 打开

     配置开发者证书

    运行 

    ( 直接command+U运行,这是test模式,不能run,即一定要执行project>Test 才可以校验第三步), 运行后会提示输入mac电脑的密码,提示编译成功,并且在IOS真机上看到了WDA的身影。

    编译成功

    同时,控制台会显示输出IP地址和端口,如下图所示(打开控制台方法:选择view->Debug Area->Activate console打开底部控制台。):关键是最后一句话(ServerURLHere->http://192.168.11.236:8100<-ServerURLHere

    复制url到输入在浏览器中,就会发现网页上显示出你的手机界面,然后,你就可以为所欲为啦。

    不过有些iPhone,据说是国产的原因,直接使用ip不能连接手机(即输入打印出来的url无法显示手机界面),需要进行端口转发(终端执行 “iproxy 8300 8100” 回车后在浏览器中输入http://localhost:8300/出现json字符串即表示成功,同时说明WDA安装成功了)。

    查看当前设备

    地址后面添加/status可以查看当前设备(你使用的哪个设备运行,就是哪个设备的状态)的状态。---http://localhost:8300/status,获取与wda通信session id

     手机桌面app

     iOS15 以上打开屏幕会出现黑色影子,15 一下会闪退,需要 xcode 附加调试才能运行

    第一部完成

    web-driver-agent 就算安装完了。

    开始第二部分

    实现自动点击,和滑动,界面文字获取,等操作...

    实现原理

          本身 web-driver-agent 就是 一个服务器,我们在写app安装到手机,调用本机127.0.0.1服务器接口。

    点击/滑动 接口

    1. + (NSArray *)routes
    2. {
    3. return
    4. @[
    5. [[FBRoute POST:@"/wda/touch/perform"] respondWithTarget:self action:@selector(handlePerformAppiumTouchActions:)],
    6. [[FBRoute POST:@"/wda/touch/multi/perform"] respondWithTarget:self action:@selector(handlePerformAppiumTouchActions:)],
    7. [[FBRoute POST:@"/actions"] respondWithTarget:self action:@selector(handlePerformW3CTouchActions:)],
    8. ];
    9. }

    要调用本地服务器,需要先获取一个sessionId

    访问 http://127.0.0.1:8100/status 可以得到若sessionId字段为空,

    就访问  /session  创建

    POST http://127.0.0.1:8100/session 

    Body 

    NSString *body = @"{\"capabilities\":{\"firstMatch\":[{\"arguments\":[],\"environment\":{},\"eventloopIdleDelaySec\":0,\"shouldWaitForQuiescence\":true,\"shouldUseTestManagerForVisibilityDetection\":false,\"maxTypingFrequency\":60,\"shouldUseSingletonTestManager\":true,\"shouldTerminateApp\":true}],\"alwaysMatch\":{}}}";

    获取session完整代码
    1. - (IBAction)noButtonhttp:(id)sender {
    2. //get请求
    3. //[self reqWithMethod:0];
    4. [xddHttp reqWithMethodxdd2:@"http://127.0.0.1:8100/status" Method:0 HTTPBody:@"" Block:^(NSURLResponse * _Nonnull response, NSDictionary * _Nonnull dic) {
    5. NSString *sessionId = dic[@"sessionId"];
    6. NSLog(@"请求成功-status-sessionId=%@",sessionId);
    7. iPhoneSessionId = sessionId;
    8. if ([sessionId isKindOfClass:[NSNull class]]) // 字符串为空
    9. NSString *body = @"{\"capabilities\":{\"firstMatch\":[{\"arguments\":[],\"environment\":{},\"eventloopIdleDelaySec\":0,\"shouldWaitForQuiescence\":true,\"shouldUseTestManagerForVisibilityDetection\":false,\"maxTypingFrequency\":60,\"shouldUseSingletonTestManager\":true,\"shouldTerminateApp\":true}],\"alwaysMatch\":{}}}";
    10. [xddHttp reqWithMethodxdd2:@"http://127.0.0.1:8100/session" Method:1 HTTPBody:body Block:^(NSURLResponse * _Nonnull response, NSDictionary * _Nonnull dic) {
    11. NSString *sessionId = dic[@"sessionId"];
    12. NSLog(@"请求成功-session-sessionId=%@",sessionId);
    13. iPhoneSessionId = sessionId;
    14. }];
    15. }
    16. }];
    17. }
    拿到session

    及可以去点击,滑动,获取屏幕信息了。

    我测试每 10 秒手机屏幕自动上滑一次

    创建定时器
    1. +(void)myTimers{
    2. userarr = [self testArr];
    3. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    4. // 在这里执行你的任务
    5. //[self goo:nil];
    6. });
    7. myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(goo:) userInfo:nil repeats:YES];
    8. }
    每秒加一到 10 ,就执行一次滑动屏幕
    1. +(void)go{
    2. GlobalVariableName = @"g0";
    3. userindex = userindex + 1;
    4. NSLog(@"定时器查: %d",userindex );
    5. //if(userindex %3 == 0){
    6. if(userindex == 10){
    7. userindex=0;
    8. NSLog(@"开始滑动: %@",@"-------------------");
    9. NSString*url = [NSString stringWithFormat:@"http://127.0.0.1:8100/session/%@/wda/touch/perform",iPhoneSessionId];
    10. //点击
    11. NSString*body = @"{"actions":[{"action":"tap","options":{"x":131,"y":248}}]}";
    12. //滑动
    13. body=@"{\"actions\":[{\"action\":\"press\",\"options\":{\"x\":111,\"y\":392}},{\"action\":\"wait\",\"options\":{\"ms\":500}},{\"action\":\"moveTo\",\"options\":{\"x\":112,\"y\":161}},{\"action\":\"release\",\"options\":{}}]}";
    14. [xddHttp reqWithMethodxdd2:url Method:1 HTTPBody:body Block:^(NSURLResponse * _Nonnull response, NSDictionary * _Nonnull data) {
    15. NSLog(@"滑动结果: %@",data);
    16. }];
    17. }
    18. }
    实现效果

    web-driver-agent_appium-自动滑动

    手机需要保持后台运行,定时器一直运行

    保持后台运行参考代码

    iOS 关于后台持续运行icon-default.png?t=N7T8https://gitee.com/vww/BgTaskDemo

  • 相关阅读:
    一、初始化个人简历项目
    金融行业借力泛微今承达,合同统一数字化管理、风险全过程把控
    【架构师(第十二篇)】脚手架之命令行交互工具 inquirer.js 使用方法
    为什么很多公司都开始使用Go语言了?
    Day 00 python基础认识与软件安装
    SaaS服务平台软件是什么?
    设计模式——解释器模式
    IntelliJ IDEA - Maven 在控制台Maven编译正常,但是在IDEA中不正常,表现不一致
    数据仓库、数据中台、大数据平台的关系?
    springBoot--web--favicon规则
  • 原文地址:https://blog.csdn.net/qq_21051503/article/details/134053551