The use of automated testing framework Airtest automatic steal energy Alipay friends

Before we use Android Studio of junit tests to achieve through automatic thumbs up function live, post here realize live on automatic thumbs up function .

However, the test is based on Android native control. Today we are saying Alipay forest ants WebView page is achieved, so the original method does not apply.

But, however, when I visited the Technical Forum and suddenly found such a thing Airtest , see the official website propaganda operation when, excuse me there is no culture: FML, fiercely. Then directly downloaded began to tinker with, was just thinking about trying to steal energy then can use this to try. Specific page shown below, or Chinese, ha ha ha, as to how this will not repeat, Bel cooked AS are used, this is not to use every minute of it. In addition to the IDE scripts using Python, when I was in contact with the tool before going to learn Python, so the code is written by one side while learning, slag slag Code also hope forgive me.
Here Insert Picture Description

1, the energy charge

The right side of the screen to select the device you can charge the energy sphere, can be found on the left you click on a node (red mark position) is located in the J_barrier_free node. If your energy ball can charge you the name of this node is " to collect energy x g ", as shown red box marked. If the energy of the ball is not charged , then the node name to it is empty , the yellow box marked as FIG. Other nodes, for example, will be displayed to the right of the map and accomplishments such as:
Here Insert Picture Description
Here Insert Picture Description

So we can J_barrier_free whether there is a node " to receive energy x-gram " child nodes to determine whether energy can be charged:
So collect energy (ie energy ball click) code is as follows:

# =========================偷取能量=========================
def steal():
    # 睡眠,否则获取不到当前页面节点
    sleep(5)
    
    ## 找到J_barrier_free节点下所有子节点(包含了能量球以及其他元素)
#     list_node = poco("android:id/content").offspring("com.alipay.mobile.nebula:id/h5_fragment").offspring("J_barrier_free").wait(5).children()
    list_node = poco("J_barrier_free").wait(5).children()

    ## 遍历出所有可以收取的能量球节点,点击节点实现收集能量
    for node in list_node:
        name = node.get_name()
        if  name.find("收集能量") >= 0:
            node.click()

    # 返回上一页
    back_btn = poco("com.alipay.mobile.nebula:id/h5_tv_nav_back")
    if back_btn.exists():
        back_btn.click()
# =========================偷取能量=========================

:sleep(5) ,从好友列表页进入能量页后,需要延迟一点时间等节点都加载出来,否则会获取不到节点信息。

2、获取可以收取能量的好友列表

分两种情况:为啥要分情况呢,因为这俩页面中item的元素还不一样。

2.1、“蚂蚁森林”页面

在该页面上只有十个好友,上部分是你的能量球,然后往下滑屏是你的十个好友。在这个页面中,分析页面元素的话点击右上角的 “绿色小手” 按钮(如下左图),然后可以看到该元素在Poco辅助窗里显示为 “可收取”节点(如下右图),那么此时我们就可以像上面判断能量球是否可收取那样,判断页面节点下有没有 “可收取”这样的节点就可以判断到哪个好友有能量可以给我们偷了。代码类似上文,此处不再给出。
Here Insert Picture DescriptionHere Insert Picture Description

2.2、“好友排行榜”页面

在该页面中,我们找到一个有能量可以偷取的好友,然后点击该item右上角的“绿色小手”按钮,你会发现怎么点也点击不到,在Poco辅助窗中根本查看不到2.1中的“可收取”节点。
Here Insert Picture DescriptionHere Insert Picture Description
那么这里也有两种办法来做:

  • 循环点击列表中好友,挨个进去执行一遍偷取能量的代码,完了之后执行上滑屏幕操作,继续循环
  • 循环匹配“绿色小手”图片,挨个进去执行一遍偷取能量的代码,完了之后执行上滑屏幕操作,继续循环

2.2.1、循环点击好友:

这一步中呢我们需要注意几点,首先,你需要获取到你的排名,因为在这个页面中你点击自己的话是不会打开自己的蚂蚁森林页面的,如果继续往下执行,代码会出错。所以我们需要先获取到自己的排名,怎么获取:
Here Insert Picture DescriptionHere Insert Picture Description
点击自己的item后,可以看到在J_rank_list_self节点下的android.view.View下有几个属性,4是我们要的排名,而其他都是字符串,所以我们把这几个属性遍历一遍,使用isdigit()方法如果能转换成数字,那么这个就是我们要的排名了,所以代码如下:

# =========================获取我的排名=========================
def index_my():
    node_my = poco("android:id/content").offspring("com.alipay.mobile.nebula:id/h5_fragment").offspring("J_rank_list_self").child("android.view.View").children()
    for node in node_my:
        index_my = node.get_name()
        if index_my.isdigit():
            print(index_my)
            return index_my
# =========================获取我的排名=========================

排名也知道了,然后遍历的时候顺序获取排名,然后挨个点击进去进行偷取,执行到自己的时候跳过。还要注意下,前三名好友不显示排名取而代之的是图片,所以针对前三名我们直接在“J_rank_list”节点中,顺序执行前三个就好了,代码如下:


# =========================获取列表item高度=========================
def height_item(list_friend):
#     list_friend = poco("J_rank_list").children()
    h = list_friend[1].get_position()[1] - list_friend[0].get_position()[1]
    return round(h,3)
# =========================获取列表item高度=========================


list_friend = poco("J_rank_list").children()
index = 1
index_my = index_my()
height_item = height_item(list_friend)

while True:
    if index < 4:
        if index == index_my:
            index += 1
            continue
        list_friend[index].click()
        steal()
        index += 1
    else:
        if index == index_my:
            index += 1
            continue
        friend = poco(str(index+1))
        if friend.exists():
            poco(str(index)).click()
            steal()
            index += 1
        else:
            if poco("J_rank_list_more").exists():
                break
            poco.swipe([0.5,0.8],[0.5,0.8-height_item*5])

: 向上滚动的代码有问题,会突然下拉,然后再滚动上去。

2.2.2 匹配图片

这个是最好理解的了,在Airtest辅助窗中,点击touch按钮,他会让你选择设备屏幕上的区域,你框选“绿色小手”按钮即可,然后代码会自动生成,见下图右侧:
Here Insert Picture Description Here Insert Picture Description
其实到这里已经明朗了,循环判断屏幕上是否有“绿色小手”按钮,有的话就执行点击,然后进去偷取能量,没有的话就滚动屏幕,翻到下一页继续执行。直到出现最底部的“没有更多了”节点出现,循环终止。因为滚动屏幕的代码有问题,这里暂时不给出代码。

发布了40 篇原创文章 · 获赞 47 · 访问量 7万+

Guess you like

Origin blog.csdn.net/u010976213/article/details/86598114