当前位置:网站首页>Automated test - uiautomator2 framework application - automatic clock in
Automated test - uiautomator2 framework application - automatic clock in
2022-06-29 07:54:00 【qazw9600】
explain
- Recently, the company has changed the way to punch in , Switch to mobile APP Location punch , It is similar to Ali's pin punch .
- In order to apply what you have learned , Special programming to achieve automatic clock out .
Frame description
- UIAutomator yes Android Automated testing framework provided by , Support most Android system operation , Is used to do UI Black box test , That is to say, it is a manual test that simulates manual work , for example : Click on the control element to see if the display is as expected , The characteristics are as follows :
- Black box testing , Simulate human operation , You don't need to know the implementation details .
- Not dependent on one APP, Able to span App function .
- Unwanted root jurisdiction .
- The disadvantage is that it only supports SDK 16(Android 4.1) And above , I won't support it Hybird App、WebApp.
- Use java To write , Special operation mode is required .
- The uiautomator2 The frame is UIAutomator Of python Encapsulation framework , Use python To write ( Write simple , convenient ), long-range ( adopt wifi perhaps usb Connecting device ) Independent operation .
Environmental preparation
- The individual is in ubuntu18.04 The configuration of uiautomator2 Environmental Science ,windows It's the same thing , Previously installed .
- Depend on the environment
- install adb; Used to identify and connect equipment
- Python3,pip3 Environmental Science ; Script running environment and Uiautomator2 Library installation tool
- Libraries installed
pip3 install uiautomator2 // Automated test library
- Mobile environment configuration
python3 -m uiautomator2 init // It will automatically install the tools used on the mobile phone , User consent is required to install
- Be careful :
- Many blogs on the Internet , The tool used is pip and python, Use pip It can also be installed , But it's installed uiautomator2 Relatively old , You may encounter many exceptions ,pip The installation may be python2 The library of , Use python Also similar , It's better to use python3 Tools in the environment , Relatively new , Better compatibility , Generally, there are not too many exceptions .
- Environment validation script ; After the environment is configured , Test whether the library is available
* Script
import uiautomator2 as u2
d = u2.connect('xxxx') # equipment ip Or device name (adb devices Shown in )
print(d.info)
* According to normal python Run in script mode , Normally, some information of the device will be printed .
Frame usage
- Because the framework is UI Black box testing , Need to identify and locate interface elements , Can be operated accordingly , You need to use the interface element identification tool , as follows :
pip3 install weditor # install
python3 -m weditor # Tools run
- weditor After the tool runs , The web interface will pop up automatically , After the device is connected, the device will be displayed in real time UI Interface , And display relevant information of interface elements , for example : Can be used to programmatically define resource id etc. .
coded
- After you can identify and define interface elements, you can use uiautomator2 Framework of the api Operate on interface elements , The operation can be automated .
- uiautomator2 Framework interface document [ To be added ].
Automatic clock in
- analysis : Similar punch in app Both implement device binding , Communication encryption and other anti cheating means , Want to start from these aspects , It's hard , Only by the simplest and most unsolved means ( Simulate user operation ) Realization .
- Punch code example :
* Use the timing library schedule, Realize timing operation
schedule.clear()
schedule.every(1).days.at("08:45").do(clockIn)
* Punch in
d = u2.connect('c746dd057d28') # Connecting device
while 1:
try:
d.app_start("com.weaver.emobile7")# open app
d(resourceId="com.weaver.emobile7:id/layout_three").wait(timeout=3.0)
d(resourceId="com.weaver.emobile7:id/layout_three").click() # Click menu tab 3
d(text="app016").wait(timeout=1.0)
d(text="app016").click() # Click attendance item , Enter the clock in interface
except Exception as e:
d(resourceId="com.android.systemui:id/home").click()
try:
d.app_stop("com.weaver.emobile7") # Abnormal shutdown app
except Exception as e:
pass
continue
try:
d(text=" Clock in ").wait(timeout=3.0)
a = d(text=" Clock in ").get_text() == ' Clock in '
except Exception as e:
pass
else:
try:
d(text=" Clock in ").click()
time.sleep(1)
except:
pass
try:
a = d(text=" Update clock in time ").get_text() == ' Update clock in time '
except Exception as e:
try:
d.click(0.54, 0.826) # The coordinates of the clock control , Avoid that the above code does not recognize the clock control , Click fixed coordinates directly
except:
pass
d(resourceId="com.android.systemui:id/back").click()
else:
d(text=" Update clock in time ").click()
exit(0) # The update clock in time appears , Indicates that the clock was punched successfully
time.sleep(1)
- matters needing attention :
- exception handling ; The interface display may not be so timely , Need to delay or wait for processing , The absence of an interface element will cause a script operation timeout exception , End of script run , You need to add exception handling .
- stability ; The current is through usb Connect the phone ,adb Operating mobile phone , Whether this set can be used for a long time without error , It should be more stable if it can run on the mobile phone , But it is troublesome to read other people's blogs .
- The mobile phone is abnormal ; The mobile phone automatically stops the screen , You need to enter the password after the screen is stopped , Personal early alarm clock may affect the interface display , Need to be closed .
- Network anomalies ; Some clocks app Need to connect to the corporate network , To prevent detection , You should not always be connected to the corporate network , It needs to be started regularly .
边栏推荐
猜你喜欢

Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case

Prompt during packaging: property 'sqlsessionfactory' or 'sqlsessiontemplate'‘

Postman pre request

面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?

Viewing application and installation of Hana database license

VSLAM特征之线特征&面特征

Using cdockablepane to realize floating window in MFC

Vulnhub's DC8 target

VMware vcenter/ESXI系列漏洞总结

【工控老马】基于PLC的花样喷泉设计原理详解
随机推荐
VSLAM特征之线特征&面特征
Gateway controller communication protocol
1031 Hello World for U
Compiling principle: the king's way
【域渗透提权】CVE-2020-1472 NetLogon 权限提升漏洞
qtcreator设置字符集
产品经理应该学习墨刀还是Axure?
ES中配置ext.dic文件不生效的原因
【量化投资系统】问题记录及解决方法
Reasons why the ext.dic file configured in ES does not take effect
358. K distance interval rearrange string sorting
程序调试 - Debug/Release 版本
【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
1031 Hello World for U
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
Handle series - install spotbugs and use them quickly
Qtcreator set character set
498. diagonal traversal (simulation)
Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
719. 找出第 K 小的数对距离(二分)