poco常用语法合集

pcoo常用的的语法:

androidpoco定义方法

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
dev = connect_device("android:///" + devices)
Androidpoco = AndroidUiautomationPoco(device=dev, use_airtest_input=True, screenshot_each_action=False)

unitypcoo定义方法方法

from poco.drivers.unity3d import UnityPoco
dev = connect_device("android:///" + devices)
poco = UnityPoco(device=dev)

cocos-luapoco定义方法

from poco.drivers.std import StdPoco
poco = StdPoco()

cocos-js poco定义方法

from poco.drivers.cocosjs import CocosJsPoco
poco = CocosJsPoco()

iospoco定义方法

from poco.drivers.ios import iosPoco
poco = iosPoco()
StdPoco定义方法
from poco.drivers.std import StdPoco
from poco.utils.device import VirtualDevice
poco = StdPoco(15004, VirtualDevice('localhost'))

启动apk

start_app(packname)  # 启动app

杀死apk进程

stop_app(packagename)

最简单的操作就是点击(click),也可以长按(long click),按多久都行,下面例子展示点击和长按各自的效果。

poco('bg_mission').click()
poco('bg_mission').click('center')
poco('bg_mission').click([0.5, 0.5])    # equivalent to center
poco('bg_mission').focus([0.5, 0.5]).click()  # equivalent to above expression

下面例子展示如何使用swipe

joystick = poco('movetouch_panel').child('point_img')
joystick.swipe('up')
joystick.swipe([0.2, -0.2])  # swipe sqrt(0.08) unit distance at 45 degree angle up-and-right
joystick.swipe([0.2, -0.2], duration=0.5)

循环点击某个父节点下的所有子节点

for Off in poco("AchievementTrophyParent").offspring("off"):
    Off.click()

等待某个元素出现,不出现则报错,默认120s

try:
  poco("BtnStart").wait_for_appearance(timeout=120)
except:
  print("none")

等待某个元素消失,不消失,则报错,默认120s

try:
  poco("BtnStart").wait_for_disappearance(timeout=120)
except:
  print("none")

判断某个元素是否出现在当前屏幕上

if poco("Return").exists():

对于以列表形式出现的控件操作

for item in range(len(poco("TeamDlg(Clone)").offspring("Dungeons").child("Panel").child())):
  item1 = "item" + str(item)
  poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).click()

更改UI元素的属性值。并非所有属性都可以转换为文本。如果改变了不可变属性或不存在的属性,将引发InvalidOperationException异常

poco("Bg").setattr("text","123456")

# text:你想改变的这个控件的属性名称,一般情况下只有text是可以被改变的
# 132456:你想更改后的值
# 感觉可以作为输入方法来用,   我没用过

双击

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).double_click()

长按两秒

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).long_click(duration=2.0)

拖动

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).drag_to(target,duration=2.0)
#target:控件节点或者坐标,坐标用[]OR()

获取控件的相对坐标,可以用来判断是否在屏幕中

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).get_position()

等待控件三秒,不出现则报错

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).wait(timeout=3)

获取控件的text属性值,如果有的话

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).get_text()

输入字符串,如果是input框

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).set_text("1234563")

输入字符串,如果不是input框

text(“123456”)

获取控件的name属性

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).get_name()

获取元素的大小

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).get_size()

获取UI元素的包围框参数

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).get_bounds()

获取UI的各个属性 arrt

复制代码

print(poco("Bg").attr("type"))
print(poco("Bg").attr("name"))
print(poco("Bg").attr("visible"))
print(poco("Bg").attr("pos"))
print(poco("Bg").attr("size"))
print(poco("Bg").attr("scale"))
print(poco("Bg").attr("anchorPoint"))
print(poco("Bg").attr("zOrders"))
print(poco("Bg").attr("clickable"))
print(poco("Bg").attr("components"))
print(poco("Bg").attr("texture"))
print(poco("Bg").attr("_instanceId"))
print(poco("LabelStatus").attr("text"))

复制代码

从层次结构中重新查询或重新选择UI元素

poco("TeamDlg(Clone)").offspring("Dungeons").offspring(item1).invalidate()

判断多个控件同时出现,默认120s

A = poco("ProfFrame1")
B = poco("ProfFrame2")
C = poco("ProfFrame3")
poco.wait_for_all([A,B,C],timeout=120)

判断多个控件其中一个出现,默认120s

A = poco("ProfFrame1")
B = poco("ProfFrame2")
C = poco("ProfFrame3")
poco.wait_for_any([A,B,C],timeout=120)

冻结当前ui元素,获取一个UI副本

with poco.freeze() as freeze_poco:
freeze_poco("DailyActivityDlg(Clone)").offspring("RightView").offspring("Icon").click()

使用poco点击坐标点,>1表示绝对坐标,<1表示相对坐标

poco.click([0.5,0.5])

使用poco右键点击坐标点,>1表示绝对坐标,<1表示相对坐标  未实现

poco.click([0.5,0.5])  # 右键点击未实现

def rclick(self, pos):
     raise NotImplementedError

使用poco双击坐标点,>1表示绝对坐标,<1表示相对坐标  未实现

poco.double_click([0.5,0.5]) # 右键点击未实现

def double_click(self, pos):
    raise NotImplementedError

poco滑动坐标点>1表示绝对坐标,<1表示相对坐标  

poco.swipe([0.5,0.5],[0.1,0.1])

poco长按坐标点两秒,坐标(x, y)取值范围为0到1

poco.long_click([0.5,0.5],duration=2.0)

poco在屏幕中间从下向上滑动,percent=屏幕的百分比,duration=滑动的时间

poco.scroll(direction="vertical", percent=0.3, duration=2.0)

poco在屏幕中间从右向左滑动,percent=屏幕的百分比,duration=滑动的时间

poco.scroll(direction="horizontal", percent=0.3, duration=2.0)

以上两个方法改了只有下上和右左,改了源码,增加反方向滑动操作

复制代码

    def scroll(self, direction='vertical', percent=0.6, duration=2.0):
        """
        Scroll from the lower part to the upper part of the entire screen.

        Args:
            direction (:py:obj:`str`): scrolling direction. "vertical" or "horizontal"or"up" or "about"
            percent (:py:obj:`float`): scrolling distance percentage of the entire screen height or width according to
             direction
            duration (:py:obj:`float`): time interval in which the action is performed
        """

        if direction not in ('vertical', 'horizontal',"up","about"):
            raise ValueError('Argument `direction` should be one of "vertical" or "horizontal". Got {}'
                             .format(repr(direction)))

        start = [0.5, 0.5]
        half_distance = percent / 2
        if direction == 'vertical':
            start[1] += half_distance
            direction = [0, -percent]
        if direction == 'up':
            start[1] -= half_distance
            direction = [0, percent]
        if direction == 'about':
            start[0] -= half_distance
            direction = [percent, 0]
        else:
            start[0] += half_distance
            direction = [-percent, 0]

        return self.swipe(start, direction=direction, duration=duration)

复制代码

poco用手势缩照片的操作,

复制代码

"""
direction=压缩OR扩张“in”表示压缩,“out”表示扩张
percent=从整个屏幕的压缩范围或扩大范围
duration=执行操作的时间间隔
dead_zone=压缩内圆半径
"""
poco.pinch(direction='in', percent=0.6, duration=2.0, dead_zone=0.1)

复制代码

poco平移操作 未实现

poco.pan(duration=2.0)

生成分解的手势步骤,可以用来解锁星锁,坐标(x, y)取值范围为0到1

复制代码

poco.start_gesture([0.233, 0.506]).to([0.775, 0.505]).hold(1).to( [0.233, 0.755]).up()
poco = Poco(...)
ui1 = poco('xxx')
ui2 = poco('yyy')

# touch down on ui1 and hold for 1s
# then drag to ui2 and hold for 1s
# finally release(touch up)
ui1.start_gesture().hold(1).to(ui2).hold(1).up()
 

复制代码

poco截图

poco.snapshot()

获取屏幕的分辨率

poco.get_screen_size()

 

poco驱动程序(特定引擎的pooc实现)

以下示例显示了如何为Unity3D初始化poco实例。记住要通过运行的游戏将Android设备连接到PC / mac,或者启动并保持Unity游戏在PC / mac上的活动状态。

复制代码

# import unity poco driver from this path
from poco.drivers.unity3d import UnityPoco

# then initialize the poco instance in the following way
poco = UnityPoco()

# for windows
# poco = UnityPoco(('localhost', 5001), unity_editor=True)

# now you can play with poco
ui = poco('...')
ui.click()

复制代码

对于cocos2dx-lua,游戏类似于Unity3d驱动程序。

复制代码

# import standard poco driver
from poco.drivers.std import StdPoco
from airtest.core.api import connect_device

# connect a device first, then initialize poco object
device = connect_device('Android:///')
poco = StdPoco(10054, device)

# now you can play with poco
ui = poco('...')
ui.click()

复制代码

 按照序号(顺序)进行选择总是按照空间排布顺序,先从左往右,再像之前那样一行一行从上到下,如下图中的数字标号,就是索引选择的序号。索引选择有个特例,一旦进行选择后,如果UI的位置发生了变化,那么下标序号仍然是按照选择的那一瞬间所确定的值。即,如果选择时1号UI现在去到了6号的位置,那么还是要用 poco(...)[1] 来访问,而不是6.如果选择了之后,某个UI消失了(从界面中移除或者隐藏了),那么如果再访问那个UI则可能会发生异常,其余的UI仍可继续访问。

items = poco('main_node').child('list_item').offspring('item')
print(items[0].child('material_name').get_text())
print(items[1].child('material_name').get_text())

直接用节点属性没法选出你所想要的UI时,还可以通过UI之间的渲染层级关系进行选择,例如父子关系、兄弟关系、祖先后代关系。

poco('main_node').child('list_item').offspring('item')

下面代码片段展示如何迭代遍历一组UI

# traverse through every item
items = poco('main_node').child('list_item').offspring('item')
for item in items:
    item.child('icn_item')

下面的例子展示如何通过代码获取UI的各种属性

mission_btn = poco('bg_mission')
print(mission_btn.attr('type'))  # 'Button'
print(mission_btn.get_text())  # '据点支援'
print(mission_btn.attr('text'))  # '据点支援' equivalent to .get_text()
print(mission_btn.exists())  # True/False, exists in the screen or not

 所有UI相关的操作都默认以UI的 anchorPoint 为操作点,如果想自定义一个点那么可以使用 focus 方法。调用此方法将返回 新的 设置了默认 焦点 的UI,重复调用则以最后一次所调用的为准。focus 所使用的是局部坐标系,因此同样是UI包围盒的左上角为原点,x轴向右,y轴向下,并且包围盒长宽均为单位1。很显然中心点就是 [0.5, 0.5] 。下面的例子会展示一些常用的用法。

poco('bg_mission').focus('center').click()  # click the center

将 focus 和 drag_to 结合使用还能产生卷动(scroll)的效果,下面例子展示了如何将一个列表向上卷动半页。

scrollView = poco(type='ScollView')
scrollView.focus([0.5, 0.8]).drag_to(scrollView.focus([0.5, 0.2]))

你可能感兴趣的:(airtest,poco,测试工程师,webgl,列表)