Poco framework for APP automation

Today I would like to introduce Poco, an automated testing framework. Its script writing method is very concise and efficient, and its element locator is more efficient. Its essence is based on a third-party python library. It is also very convenient to debug and can greatly improve automated testing. Efficiency, save time.

(1) Background

1. Introduction to Poco

Poco is a cross-platform automated testing framework based on the principle of UI control recognition, suitable for Android, iOS native and various mainstream game engine applications. Since Airtest is based on the principle of image recognition, the combination of Poco+Airtest is more suitable for hybrid applications . Especially for the unrecognizable situation of some element controls in the flutter app, the airtest framework is used, and the Poco framework is used for automated testing of the element controls.

2. The essence of Poco

Like the Airtest framework, Poco is actually a third-party library of Python. If you need to write Poco scripts locally, you need to install the Pocoui library first.

pip install pocoui

3. Official Documentation

https://airtest.doc.io.netease.com/IDEdocs/poco_framework/1_poco_info/

(2) Using the Poco framework

1. View control elements

After AirtestIDE connects to the device under test, then select the corresponding device in the Poco auxiliary window to view

picture

Remarks: When selecting the mode, the top of the script editing window will allow us to choose whether to insert the corresponding initialization code, we can choose Yes

2. How to view controls

Type 1: Freeze Mode

Enter mode : Click the freeze button below

Note : Click the arrow area in the figure below to enter the freeze mode to view the element control. Move the cursor to the element position on the right and click the corresponding element control to automatically locate the page level. The log window can view the element properties of the current level . Even if the actual connected device under test has left the current page, the freeze mode will not be affected, which is equivalent to freezing on this page all the time, which is convenient for viewing elements

Exit method : click the freeze button again

picture

The second: view mode

Enter the mode : click the view button below

Note : Click the arrow area in the figure below, and you will enter the view mode to view the element controls. The biggest difference from the freeze mode just now is that you can view the controls at this time. If we operate the controls, the device screen will also change accordingly.

Exit method : click the view button again

picture

3. How to position elements

basic locator

Since AirtestIDE has built-in Poco, it can be used directly by importing it. The basic locator is poco (node ​​name or node attribute)

# -*- encoding=utf8 -*-
__author__ = "86150"
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

# 单个条件
poco(text="中古屋")

# 多个条件
poco('com.addcn.android.house591:id/grid_item_text',text="中古屋")

picture

relative selector

It is to select through the hierarchical relationship between elements, such as parent-child relationship, brother relationship, ancestor and descendant relationship, etc. Poco controls provide us with various methods for positioning using the hierarchical relationship of the tree:

  • child node: child
  • All child nodes: children
  • Descendant node: offspring
  • Parent node: parent
  • Brother node: sibling
poco("plays").child("playBasic").offspring("star_single")

Explanation: This method is cumbersome. Generally, you can choose to use this method when attributes and nodes cannot be located.

regular expression

It is a rare way to match regular expressions, but it is also a very useful way. According to the following figure, the way to locate through attributes is:

# 1.text属性方式定位
poco(text="中古屋")
# 换成正则表达式定位
poco(textMatches="正则表达式")
poco(textMatches=".*中古屋")

# 2.name属性方式定位
poco(name='com.addcn.android.house591:id/grid_item_text')
# 换成正则表达式定位
poco(nameMatches="正则表达式")
poco(nameMatches=".*grid_item_text")

Note: As long as you can use poco(xx=expected attribute value) to select the control, you can use poco(xxMatches=regular expression of expected attribute value) for matching and positioning. The official recommendation is to use attributes or regular expressions for positioning first. Simpler and more efficient

4. Ways to manipulate elements

click

# 单击元素
	poco(text="中古屋").click()
	# 长按元素
	poco(text="中古屋").long_click()

slide

Poco supports sliding operations on controls. We need to locate the control first, and then specify it to slide in a certain direction.

# 向下滑动0.2个单位距离
poco("com.addcn.android.house591:id/grid_item_text").swipe([0,0.2])

# 向上滑动0.2个单位距离
poco("com.addcn.android.house591:id/grid_item_text").swipe([0,-0.2])

# 向下滑动0.1个单位距离
poco("com.addcn.android.house591:id/grid_item_text").swipe("down")

# 向上滑动0.1个单位距离
poco("Handle").swipe("up")

Get the attribute value of the element

The control attributes retrieved from the poco auxiliary window of the IDE can basically be read through the attr interface

picture

print("name:"+poco(text="中古屋").attr("name"))
print("type:"+poco(text="中古屋").attr("type"))
print("texture:"+poco(text="中古屋").attr("text"))
print("texture:"+poco(text="中古屋").attr("texture"))

# log日志输出
name:com.addcn.android.house591:id/grid_item_text
type:android.widget.TextView
texture:中古屋
[Finished]

Set the attribute value of the element

Usually, you need to enter text content for a given element, you can use the set_text() method or the setatrr() method

from airtest.core.api import *
auto_setup(__file__)
from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

# 先激活输入光标
poco("com.addcn.android.house591:id/et_edittext").click()

# 再执行输入动作
poco("com.addcn.android.house591:id/et_edittext").set_text("123")

poco("com.addcn.android.house591:id/et_edittext").setattr('text',"456")

Compared with Appium scripts, Poco scripts are more concise, efficient, and more convenient to use, especially for hybrid applications, Flutter APP and other applications that can be used in conjunction with the Airtest framework, which can well solve the problem that some element controls cannot be positioned. At present, I think it is very easy to use, and I can publish an application combining the two test frameworks in the next issue~

Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】
insert image description here

Software Testing Interview Documentation

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_67695717/article/details/131493993