【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服务器接口。

点击/滑动 接口

objectivec 复制代码
+ (NSArray *)routes
{
  return
  @[
    [[FBRoute POST:@"/wda/touch/perform"] respondWithTarget:self action:@selector(handlePerformAppiumTouchActions:)],
    [[FBRoute POST:@"/wda/touch/multi/perform"] respondWithTarget:self action:@selector(handlePerformAppiumTouchActions:)],
    [[FBRoute POST:@"/actions"] respondWithTarget:self action:@selector(handlePerformW3CTouchActions:)],
  ];
}
要调用本地服务器,需要先获取一个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完整代码
objectivec 复制代码
- (IBAction)noButtonhttp:(id)sender {
    //get请求
    //[self reqWithMethod:0];
    
    [xddHttp reqWithMethodxdd2:@"http://127.0.0.1:8100/status" Method:0 HTTPBody:@"" Block:^(NSURLResponse * _Nonnull response, NSDictionary * _Nonnull dic) {
        
        NSString *sessionId = dic[@"sessionId"];
        NSLog(@"请求成功-status-sessionId=%@",sessionId);
        iPhoneSessionId = sessionId;
        if ([sessionId isKindOfClass:[NSNull class]]) // 字符串为空
        
            NSString *body = @"{\"capabilities\":{\"firstMatch\":[{\"arguments\":[],\"environment\":{},\"eventloopIdleDelaySec\":0,\"shouldWaitForQuiescence\":true,\"shouldUseTestManagerForVisibilityDetection\":false,\"maxTypingFrequency\":60,\"shouldUseSingletonTestManager\":true,\"shouldTerminateApp\":true}],\"alwaysMatch\":{}}}";
            
            [xddHttp reqWithMethodxdd2:@"http://127.0.0.1:8100/session" Method:1 HTTPBody:body Block:^(NSURLResponse * _Nonnull response, NSDictionary * _Nonnull dic) {
                NSString *sessionId = dic[@"sessionId"];
                NSLog(@"请求成功-session-sessionId=%@",sessionId);
                iPhoneSessionId = sessionId;
        }];
            
        }
    }];
    
    
}
拿到session

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

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

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

web-driver-agent_appium-自动滑动

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

保持后台运行参考代码

iOS 关于后台持续运行https://gitee.com/vww/BgTaskDemo

相关推荐
嗯诺13 小时前
黑马头条APP手工测试项目
appium
测试界吖吖2 天前
软件测试 | APP测试 —— Appium 的环境搭建及工具安装教程
自动化测试·软件测试·功能测试·程序人生·职场和发展·appium
Coco(学习)13 天前
Wimdows使用Appium IOS自动化
运维·appium·自动化
PUTAOAO16 天前
jenkins+python+appium 本地(简洁版)
python·appium·jenkins
月光水岸New17 天前
【APP自动化】Appium 环境搭建
运维·appium·自动化
杰哥在此19 天前
Python知识点:如何使用Appium进行移动应用测试
开发语言·python·面试·appium·编程
一禅(OneZen)21 天前
【L1.第四章】 Appium Inspector 自动化用例录制
python·selenium·appium·自动化·pytest
一禅(OneZen)21 天前
【L1.第五章】 Appium Capablility 配置参数详细解析
python·selenium·appium·自动化·pytest·web
Feng.Lee22 天前
学习一下Appium 常用方法介绍
学习·appium
沐曦可期24 天前
appium学习记录
android·python·selenium·jdk·appium