Home > WeChat Applet > Mini Program Development > WeChat Mini Program Automated Intimate Contact

WeChat Mini Program Automated Intimate Contact

高洛峰
Release: 2017-02-09 14:04:46
Original
3308 people have browsed it

The mini program to get started with is the official test demo of WeChat. Similar to Android Api Demos, the official mini program also shows the usage of various controls and the expansion capabilities of common interfaces. After adding the developer's WeChat account, you can open the WeChat applet by scanning the QR code.


1. Mini program runtime analysis


1. First, start WeChat and check WeChat What processes are there.

shell@HWNXT:/ $ ps | grep u0_a539
u0_a539   6688  533   1751392 84976 SyS_epoll_ 0000000000 S com.tencent.mm:push
u0_a539   7593  533   2228476 252492 SyS_epoll_ 0000000000 S com.tencent.mm
u0_a539   8047  533   1984672 854121 SyS_epoll_ 0000000000 S com.tencent.mm:tools
u0_a539   8117  533   1770972 86280 SyS_epoll_ 0000000000 S com.tencent.mm:sandbox
shell@HWNXT:/ $
Copy after login

There are four processes in total. Take a look at the process currently displaying the WeChat screen. Judging from the name, it should be com.tencent.mm.

shell@HWNXT:/ $ dumpsys activity top | grep ACTIVITY
  ACTIVITY com.tencent.mm/.ui.LauncherUI 44c445f pid=7593
Copy after login

Checked through the command, the current top process is 7593, which is indeed com.tencent.mm.


#2. Next, take a look at the process changes after launching the official WeChat applet demo.

u0_a539   6688  533   1750852 84420 SyS_epoll_ 0000000000 S com.tencent.mm:push
u0_a539   7593  533   2223164 272116 SyS_epoll_ 0000000000 S com.tencent.mm
u0_a539   9853  533   2092872 117492 SyS_epoll_ 0000000000 S com.tencent.mm:tools
u0_a539   9896  533   2351160 212336 SyS_epoll_ 0000000000 S com.tencent.mm:appbrand0
shell@HWNXT:/ $
Copy after login

There is one more process, com.tencent.mm:appbrand0. Which process does the WeChat applet run in?

Look at the top process:

shell@HWNXT:/ $ dumpsys activity top | grep ACTIVITY
  ACTIVITY com.tencent.mm/.plugin.appbrand.ui.AppBrandUI 15a772 pid=9896
shell@HWNXT:/ $
Copy after login

The current top process is 9896, which is indeed com.tencent.mm:appbrand0.

It can be seen that in order to ensure the resources and independence of the mini program, WeChat has opened a separate process for the mini program.


#3. Are WeChat applet and opening a webpage in WeChat implemented by the same module?

Open a webpage in WeChat, and then check the process:

shell@HWNXT:/ $ ps | grep u0_a539
u0_a539   6688  533   1751212 86184 SyS_epoll_ 0000000000 S com.tencent.mm:push
u0_a539   7593  533   2187672 263352 SyS_epoll_ 0000000000 S com.tencent.mm
u0_a539   8047  533   2336360 224436 SyS_epoll_ 0000000000 S com.tencent.mm:tools
Copy after login

The process has not changed, look at the top process:

shell@HWNXT:/ $ dumpsys activity top | grep ACTIVITY
  ACTIVITY com.tencent.mm/.plugin.webview.ui.tools.WebViewUI 1502038 pid=14685
Copy after login

The webpage is actually at com.tencent.mm It is opened in the :tools process, and the activities of the two are different. The applet is .plugin.appbrand.ui.AppBrandUI, and the web page is .plugin.webview.ui.tools.WebViewUI.

It seems that there is a difference between WeChat applet and a simple web page.


2. The screen composition of the mini program


Use UIAutomator to analyze the components that make up the WeChat mini program screen

WeChat Mini Program Automated Intimate Contact

Through UIAutomator analysis of the screen, it is found that the WeChat applet Demo is composed of three parts, Top ActionBar, and Tencent's own WebView in the middle, which should be Tencent's self-developed X5 The core, below is a Bottom ActionBar, which displays the content part of the applet in X5 WebView.


It can be seen that the page display of the WeChat applet uses the H5 hybrid display solution of Android native controls and WebView, which is equivalent to a very common H5 hybrid application on the market. .


3. How to do automated testing of WeChat mini programs


Currently, the Android automated testing framework is mainly divided into 6 categories :


Robolectric is commonly used in unit testing. The specific implementation plan is to implement a set of Android code that can be run by the JVM, and then intercept the android-related code when the unit test is running. Call, go to their implementation code to execute the calling process, and enrich a lot of extended interfaces based on the android standard classes. This indeed greatly facilitates the unit testing process, but for our testing students who focus on the functional level, it is indeed There are some numb claws, and the practical significance is not very great.

Monkey is a stability testing tool that comes with the Android system. Many manufacturers also use it as a stability acceptance measurement tool for built-in products. Although it is easy to use, convenient and fast, as its name suggests, Monkey After all, a monkey cannot complete the testing process of determining functional use cases. Unfortunately, let’s wait for the monkey to evolve into a human.

UIAutomator is one of the few automated testing frameworks officially supported by Android. The earliest released version is API Level 17. As a control-based automation framework, UIAutomator has a clear interface and is easy to use. Based on UIAutomator, the famous Appium open source automation framework has also been developed, and its position in the industry is very strong. However, the prerequisite for using UIAutomator is that you can use UIAutomatorViewer to view the attribute information of our pre-operation controls. From the above analysis, we have seen that the parent container of some controls in the mini program is weview. This webview is not a standard structure and should be the X5 self-developed by Tencent. Kernel. The idea of ​​using appium UIAutomator to run automation has since been abandoned.

There is also an Android genotype testing solution like Instrumentation that can be considered. The famous Robotium automated testing framework was born here. However, after some understanding, I gradually understood that whether Instrumentation or robotium, you need product source code. Or sign. The test project is usually placed in the same project directory as the product source code. So the question is, who can give me the source code of WeChat? Signature is also fine. Hey, brother, do you have it? Hey, hey, can anyone hear me? !@#@%&^

In the early days, there was an automated testing capability achieved through system privilege escalation and injection, such as Baidu’s café and Alibaba’s arthrun. Most of them copied the xposed architecture model and had strong system control capabilities. . However, where are these frameworks today? It turns out that because Android root is becoming more and more difficult, version 6.0 has become almost impossible so far. Therefore, this type of open source framework stopped maintaining it around 2014. It is unreliable and needs to be replaced. Seek other solutions.

基于图像识别也有一些自动化测试框架,例如sikuli还有testin的自动化工具,然而小生之所以直接就把这类框架pass掉是因为这种测试脚本基本不具备扩展性,系统ui风格变更,想要做断言验证,以及日后用例维护等等,想都不敢想。


铁鞋踏破仍无路,靓女帅哥也踌躇啊,忽然间灵感一现,腾讯自家会不会有什么奇葩产品可用啊,知行合一谷歌百度,搜索腾讯自动化立马看到腾讯优测的介绍,到官网里翻了一下找到一款叫XTest的自动化测试工具,看到简介目前只支持Android平台,想想前面历程这般痛苦,还要啥自行车啊。于是乎赶忙下载了一套热气腾腾的XTest,安装完毕,一切准备就绪,关门,放XTest。在经过一番折腾后基本领悟了XTest的使用心法,下面我就从大家平时经常开展的性能测试走起。

一、录制脚本,加入循环等操作


使用XTest录制从体验上确实简单便捷,简单到不用插线不用PC,可以躺着录走着录,即使撩妹都不耽误测试,跟平时操作App无异。对比早期录制脚本又抓控件又摸路径受的罪,幸福感大增。录制很容易上手,就是在录制模式下,按照case跑一遍就OK了,脚本自动生成,这里不做赘述,为了让测试更加充分,我又徒手一口气在复杂路径加了50个循环。真的是徒手,因为就是用手机端的脚本编辑功能就实现了。

WeChat Mini Program Automated Intimate Contact

二、开始回放查看结果


搞定脚本后可进行本地回放或多机联测,由于是基于控件的录制技术,所以回放过程比较顺利。测试结束后在手机/sdcard/kat/result路径下会生成kat_Performance.csv文件,这就是测试过程的性能数据了,具体信息如下:

WeChat Mini Program Automated Intimate Contact

性能数据比较中规中矩,内存,cpu,电池温度,流量,帧率数据都有,页面切换滑动点击均无丢帧现象(不过这也可能跟XTest脚本回放速度比较慢有关,这点是这款工具目前看来一个比较让人吐槽的点)。


仅此结果就能满足小生的欲望么?那是绝对不可能的,没有设备耗电信息怎么能算是一个完整的性能测试呢。


三、导出脚本,追加耗电信息输出


通过前期学习,了解到XTest可以导出脚本进行二次编辑并且支持130多个API作为复杂测试任务的扩展,长话短说我将录制的脚本导出到sublime编辑器,加入电量测试代码(自定义的代码,写的不完美不欢迎吐槽)(o^^o)


1.脚本开头加入电池数据清空代码

-- 电池数据清空

shell('dumpsys batterystats --reset && echo True')
Copy after login

2.脚本结束将电量信息输出到logcat

 --输出耗电结果
    shell("dumpsys batterystats > /sdcard/kat/Result/batterystats.txt && echo True")
--读取txt文件的代码
local f = io.open('/sdcard/kat/Result/batterystats.txt', 'r')
    for line in f:lines() do
        print(line)
        shell('log -p i -t getbatterystats "' .. line .. '" && echo True')
    end
    f:close()
Copy after login

3.重新启动测试,测试完成后到logcat日志中收割电量测试结果,目标文件就在/sdcard/kat/result目录下(logcat.txt)如下:

WeChat Mini Program Automated Intimate Contact

好吧,看起来也都正常,我只是想说嗯这个也可以测,因为这个xtest可以摆脱usb线束缚无线回放脚本,这样才能获取到较为精准的电量信息。当然,希望今后类似的专项测试也能有个好的报表展现方式。


PS:注意这是耗电测试,所以充电压脉带,也正是XTest这种可无线测试的自动化引擎,才能方便搞定之前需要频繁插拔usb线才能完成的测试任务。

一、小程序分析


弄完了性能测试,我们切回主题,搞一下小程序,着手开展小程序UI自动化前,我们需要关注一下XTest是否可以轻松撸到小程序的控件


1、小程序的Hybrid控件

小程序对当前已支持的组件给出了演示程序,首先看下这些控件的真面目

WeChat Mini Program Automated Intimate Contact

使用XTest辅助工具对控件抓取可知,在X5 WebView内,控件也是如Android原生控件一样具有属性字段的。

E/Kat: setString=={name:SPAN,type:notFound,X:99,Y:777,X2:171,Y2:831}
E/Kat: name = SPAN;type=notFound; label=text;x=99 y=777 x2=171 y2 = 831
E/Kat: top-result:168,1016,[99,990,72,54],-2,top=[SPAN,text], valid=[SPAN,text],30000000,0,0,weight=0

E/Kat:@0%1:android.widget.FrameLayout%1:android.widget.FrameLayout%1:android. widget.FrameLayout%1:android...


#For example, the resource-id attribute field of the control is "SPAN"; the text attribute field is "text", and the rectangle of the control Coordinate range values ​​and layout hierarchy, these data can be accurately obtained using XTest.


#2. Can special controls also obtain object properties?

WeChat Mini Program Automated Intimate Contact

switch, video, canvas, map and other components can obtain object properties. Based on these data, UI automation control capture can be completed.


2. Mini program testing practice


1. Video interface test


In addition to providing components, the mini program demonstration also shows some interface functions, from which the representative "select video", a relatively complex use case, is extracted for testing: (Interface type: media--video)

WeChat Mini Program Automated Intimate Contact

Enter through the leading path. When you click the + control in the picture, you will enter the system camera. What? What! …….., XTest is out of control, out of control, and cannot record the system camera operation process. What was mentioned in the demo promotion about cross-process, why did it cross the ditch this time? With resentment and doubts, I hooked up with XTest development classmates. They mentioned that the tool itself does support cross-process testing. The premise is that the involved apk needs to be uploaded to the mobile phone through their tool. The specific suggestions given to me are:


1) Replace the default system camera with other camera applications, and then upload this apk to the mobile phone for testing

2) Use automation + manual interaction


I am very curious about the latter, what is automation + manual work? I gave it a try, and with their help, I actually got it done. Specifically, I inserted a statement into the script: complete the interaction process between automation and manual work. After finishing, press the volume button to report the test results, and then the automation will take over the task and continue execution. It seems that the testing industry will also follow the factory assembly line in the future. Thinking about the equipment in the Foxconn factory that is becoming more and more human-like and the people who are becoming more and more machine-like, Xiaosheng shuddered.

WeChat Mini Program Automated Intimate Contact

2. Multi-account distribution test


#See that there are 4 machines running together in the picture above, WeChat Program testing requires account login. XTest itself supports multi-machine joint testing. The WeChat applet login account is managed uniformly by the server and is sent to the mobile phone during runtime to complete the login.

WeChat Mini Program Automated Intimate Contact

See that the four accounts in the picture are the only accounts assigned by the server, and they are different. This ensures that each device can log in smoothly and is controlled by Framework drives multi-machine joint testing.


3. Joint test report display


# After completing the multi-account distribution and multi-machine joint test, you can directly Check the test report on the server side.

WeChat Mini Program Automated Intimate Contact

#The above picture shows the performance data of one device after multi-machine joint testing using Xtest. From the screenshot, you can see that after entering the video interface of the mini program and starting to play (step 6), the red line (CPU) of the curve begins to slope upward. As the video is loaded into the cache (steps 7 and 8), it represents the upstream and downstream traffic. The green line (NetFlow) begins to increase sharply. Well, it is quite consistent with the human macrocognitive theory. If you write it according to the scenario of the script, it should be easy to complete the performance data collection during the stress test, and locate which steps will cause abnormal performance data based on the picture sequence.


# Seeing this, Xiaosheng not only sighed, a set of free tools can achieve such a level, what more can I ask for!

After the above testing process from shallow to deep and from deep to ecstasy, the seemingly unfamiliar and mysterious small program has become so familiar and intimate in the eyes of our test engineers. From performance data acquisition, special test layout, to multi-machine joint testing, multi-account distribution, to the final display of rich report results, XTest seems to be ready for mini programs. Is all this God's will or a coincidence? Or simply, Tencent has already laid out a complete situation for us. This brain-burning suspense has completely exceeded the amount of calculations that the editor's single-core CPU can handle. I only know that I am fully prepared for the mini program test, so that the storm of the mini program will come more violently.

For more articles related to the automated intimate contact of WeChat mini programs, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template