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]))

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM